use of java.nio.channels.Channel in project netty by netty.
the class DatagramUnicastIPv6Test method assumeIpv6Supported.
@SuppressJava6Requirement(reason = "Guarded by java version check")
@BeforeAll
public static void assumeIpv6Supported() {
try {
if (PlatformDependent.javaVersion() < 7) {
throw new UnsupportedOperationException();
}
Channel channel = SelectorProvider.provider().openDatagramChannel(StandardProtocolFamily.INET6);
channel.close();
} catch (UnsupportedOperationException e) {
throw new AssumptionViolatedException("IPv6 not supported", e);
} catch (IOException ignore) {
// Ignore
}
}
use of java.nio.channels.Channel in project netty by netty.
the class TestUtils method isSctpSupported.
/**
* Return {@code true} if SCTP is supported by the running os.
*/
public static boolean isSctpSupported() {
String os = System.getProperty("os.name").toLowerCase(Locale.US);
if ("unix".equals(os) || "linux".equals(os) || "sun".equals(os) || "solaris".equals(os)) {
try {
// Try to open a SCTP Channel, by using reflection to make it compile also on
// operation systems that not support SCTP like OSX and Windows
Class<?> sctpChannelClass = Class.forName("com.sun.nio.sctp.SctpChannel");
Channel channel = (Channel) sctpChannelClass.getMethod("open").invoke(null);
try {
channel.close();
} catch (IOException e) {
// ignore
}
} catch (UnsupportedOperationException e) {
// This exception may get thrown if the OS does not have
// the shared libs installed.
System.out.print("Not supported: " + e.getMessage());
return false;
} catch (Throwable t) {
if (!(t instanceof IOException)) {
return false;
}
}
return true;
}
return false;
}
use of java.nio.channels.Channel in project j2objc by google.
the class InheritedChannel method createChannel.
/*
* If standard inherited channel is connected to a socket then return a Channel
* of the appropriate type based standard input.
*/
private static Channel createChannel() throws IOException {
// dup the file descriptor - we do this so that for two reasons :-
// 1. Avoids any timing issues with FileDescriptor.in being closed
// or redirected while we create the channel.
// 2. Allows streams based on file descriptor 0 to co-exist with
// the channel (closing one doesn't impact the other)
int fdVal = dup(0);
// Examine the file descriptor - if it's not a socket then we don't
// create a channel so we release the file descriptor.
int st;
st = soType0(fdVal);
if (st != SOCK_STREAM && st != SOCK_DGRAM) {
close0(fdVal);
return null;
}
// Next we create a FileDescriptor for the dup'ed file descriptor
// Have to use reflection and also make assumption on how FD
// is implemented.
Class[] paramTypes = { int.class };
Constructor ctr = Reflect.lookupConstructor("java.io.FileDescriptor", paramTypes);
Object[] args = { new Integer(fdVal) };
FileDescriptor fd = (FileDescriptor) Reflect.invoke(ctr, args);
// Now create the channel. If the socket is a streams socket then
// we see if tthere is a peer (ie: connected). If so, then we
// create a SocketChannel, otherwise a ServerSocketChannel.
// If the socket is a datagram socket then create a DatagramChannel
SelectorProvider provider = SelectorProvider.provider();
assert provider instanceof sun.nio.ch.SelectorProviderImpl;
Channel c;
if (st == SOCK_STREAM) {
InetAddress ia = peerAddress0(fdVal);
if (ia == null) {
c = new InheritedServerSocketChannelImpl(provider, fd);
} else {
int port = peerPort0(fdVal);
assert port > 0;
InetSocketAddress isa = new InetSocketAddress(ia, port);
c = new InheritedSocketChannelImpl(provider, fd, isa);
}
} else {
c = new InheritedDatagramChannelImpl(provider, fd);
}
return c;
}
use of java.nio.channels.Channel in project jstorm by alibaba.
the class NimbusData method createFileHandler.
public void createFileHandler() {
ExpiredCallback<Object, Object> expiredCallback = new ExpiredCallback<Object, Object>() {
@Override
public void expire(Object key, Object val) {
try {
LOG.info("Close file " + String.valueOf(key));
if (val != null) {
if (val instanceof Channel) {
Channel channel = (Channel) val;
channel.close();
} else if (val instanceof BufferFileInputStream) {
BufferFileInputStream is = (BufferFileInputStream) val;
is.close();
}
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
};
int file_copy_expiration_secs = JStormUtils.parseInt(conf.get(Config.NIMBUS_FILE_COPY_EXPIRATION_SECS), 30);
uploaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
downloaders = new TimeCacheMap<>(file_copy_expiration_secs, expiredCallback);
}
use of java.nio.channels.Channel in project zm-mailbox by Zimbra.
the class Server method shutdown.
/**
* Cleans up and shuts down the server thread.
*/
public synchronized void shutdown() {
shutdown = true;
for (SelectionKey key : selector.keys()) {
try {
key.cancel();
Channel channel = key.channel();
log.debug("server:closing channel %s", channel);
channel.close();
} catch (IOException e) {
log.debug("shutdown", e);
}
}
try {
selector.close();
} catch (IOException e) {
log.debug("shutdown", e);
}
try {
sschannel.close();
} catch (IOException e) {
log.debug("shutdown", e);
}
}
Aggregations