use of java.nio.channels.spi.SelectorProvider 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.spi.SelectorProvider in project baseio by generallycloud.
the class JavaEventLoop method open_selector.
@SuppressWarnings("rawtypes")
private static Selector open_selector(final SelectionKeySet keySet) throws IOException {
final SelectorProvider provider = SelectorProvider.provider();
final Selector selector = provider.openSelector();
Object res = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
return Class.forName("sun.nio.ch.SelectorImpl");
} catch (Throwable cause) {
return cause;
}
}
});
if (res instanceof Throwable) {
return selector;
}
final Class selectorImplClass = (Class) res;
res = AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
try {
Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
Throwable cause = Util.trySetAccessible(selectedKeysField);
if (cause != null) {
return cause;
}
cause = Util.trySetAccessible(publicSelectedKeysField);
if (cause != null) {
return cause;
}
selectedKeysField.set(selector, keySet);
publicSelectedKeysField.set(selector, keySet);
return null;
} catch (Throwable e) {
return e;
}
}
});
if (res instanceof Throwable) {
return selector;
}
return selector;
}
use of java.nio.channels.spi.SelectorProvider in project robovm by robovm.
the class OldSystemTest method test_inheritedChannel.
public void test_inheritedChannel() throws IOException {
Channel iChannel = System.inheritedChannel();
assertNull("Incorrect value of channel", iChannel);
SelectorProvider sp = SelectorProvider.provider();
assertEquals("Incorrect value of channel", sp.inheritedChannel(), iChannel);
}
use of java.nio.channels.spi.SelectorProvider in project jdk8u_jdk by JetBrains.
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.spi.SelectorProvider in project jdk8u_jdk by JetBrains.
the class PipeChannel method main.
public static void main(String[] args) throws Exception {
for (int x = 0; x < 100; x++) {
SelectorProvider sp = SelectorProvider.provider();
Pipe p = sp.openPipe();
Pipe.SinkChannel sink = p.sink();
Pipe.SourceChannel source = p.source();
ByteBuffer outgoingdata = ByteBuffer.allocateDirect(10);
byte[] someBytes = new byte[10];
generator.nextBytes(someBytes);
outgoingdata.put(someBytes);
outgoingdata.flip();
int totalWritten = 0;
while (totalWritten < 10) {
int written = sink.write(outgoingdata);
if (written < 0)
throw new Exception("Write failed");
totalWritten += written;
}
ByteBuffer incomingdata = ByteBuffer.allocateDirect(10);
int totalRead = 0;
do {
int bytesRead = source.read(incomingdata);
if (bytesRead > 0)
totalRead += bytesRead;
} while (totalRead < 10);
for (int i = 0; i < 10; i++) if (outgoingdata.get(i) != incomingdata.get(i))
throw new Exception("Pipe failed");
sink.close();
source.close();
}
}
Aggregations