Java Code Examples for java.nio.channels.spi.SelectorProvider#provider()

The following examples show how to use java.nio.channels.spi.SelectorProvider#provider() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TcpSocket.java    From Voovan with Apache License 2.0 6 votes vote down vote up
/**
 * 构造函数
 * @param parentSocketContext 父 SocketChannel 对象
 * @param socketChannel SocketChannel 对象
 */
public TcpSocket(SocketContext parentSocketContext, SocketChannel socketChannel){
	try {
		this.provider = SelectorProvider.provider();
		this.host = socketChannel.socket().getLocalAddress().getHostAddress();
		this.port = socketChannel.socket().getLocalPort();
		this.socketChannel = socketChannel;
		this.socketChannel.configureBlocking(false);
		this.copyFrom(parentSocketContext);
		this.socketChannel().socket().setSoTimeout(this.readTimeout);
		this.connectModel = ConnectModel.SERVER;
		this.connectType = ConnectType.TCP;

		session = new TcpSession(this);
	} catch (IOException e) {
		Logger.error("Create socket channel failed",e);
	}
}
 
Example 2
Source File: EmptyRead.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    SelectorProvider sp = SelectorProvider.provider();
    Pipe p = sp.openPipe();
    Pipe.SinkChannel sink = p.sink();
    Pipe.SourceChannel source = p.source();

    byte[] someBytes = new byte[0];
    ByteBuffer outgoingdata = ByteBuffer.wrap(someBytes);

    int totalWritten = 0;
    int written = sink.write(outgoingdata);
    if (written < 0)
        throw new Exception("Write failed");

    ByteBuffer incomingdata = ByteBuffer.allocateDirect(0);
    int read = source.read(incomingdata);
    if (read < 0)
        throw new Exception("Read EOF");

    sink.close();
    source.close();
}
 
Example 3
Source File: Open.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
static void test3() {
    SelectorProvider sp = SelectorProvider.provider();
    for (int i=0; i<11000; i++) {
        try {
            Pipe p = sp.openPipe();
        } catch (Exception e) {
            // Presumably "Too many open files"
        }
    }
}
 
Example 4
Source File: Open.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Load necessary classes ahead of time
        DatagramChannel dc = DatagramChannel.open();
        Exception se = new SocketException();
        SelectorProvider sp = SelectorProvider.provider();
        Pipe p = sp.openPipe();
        ServerSocketChannel ssc = ServerSocketChannel.open();

        test1();
        test2();
        test3();
        test4();
    }
 
Example 5
Source File: Open.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Load necessary classes ahead of time
        DatagramChannel dc = DatagramChannel.open();
        Exception se = new SocketException();
        SelectorProvider sp = SelectorProvider.provider();
        Pipe p = sp.openPipe();
        ServerSocketChannel ssc = ServerSocketChannel.open();

        test1();
        test2();
        test3();
        test4();
    }
 
Example 6
Source File: AbstractSelectableChannelTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * @tests AbstractSelectableChannel#configureBlocking(boolean)
 */
public void test_configureBlocking_Z() throws Exception {
    MockSelectableChannel mock = new MockSelectableChannel(SelectorProvider
            .provider());
    //default blocking mode is true
    //the implConfigureBlocking is only invoked if the given mode is different with current one
    mock.configureBlocking(true);
    assertFalse(mock.implConfigureBlockingCalled);
    mock.configureBlocking(false);
    assertTrue(mock.implConfigureBlockingCalled);
}
 
Example 7
Source File: Open.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Load necessary classes ahead of time
        DatagramChannel dc = DatagramChannel.open();
        Exception se = new SocketException();
        SelectorProvider sp = SelectorProvider.provider();
        Pipe p = sp.openPipe();
        ServerSocketChannel ssc = ServerSocketChannel.open();

        test1();
        test2();
        test3();
        test4();
    }
 
Example 8
Source File: Open.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static void test3() {
    SelectorProvider sp = SelectorProvider.provider();
    for (int i=0; i<11000; i++) {
        try {
            Pipe p = sp.openPipe();
        } catch (Exception e) {
            // Presumably "Too many open files"
        }
    }
}
 
Example 9
Source File: PipeChannel.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
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();
    }
}
 
Example 10
Source File: Open.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // Load necessary classes ahead of time
        DatagramChannel dc = DatagramChannel.open();
        Exception se = new SocketException();
        SelectorProvider sp = SelectorProvider.provider();
        Pipe p = sp.openPipe();
        ServerSocketChannel ssc = ServerSocketChannel.open();

        test1();
        test2();
        test3();
        test4();
    }
 
Example 11
Source File: NettyTransport.java    From async-gamequery-lib with MIT License 5 votes vote down vote up
/**
 * <p>A factory method that manufactures {@link EventLoopGroup} based on {@link ChannelType}. If the platform
 * supports
 * Epoll and the channel type is NIO, it will return {@link EpollEventLoopGroup} instead.</p>
 *
 * @param type
 *         The {@link ChannelType} that will determine which {@link EventLoopGroup} will be returned.
 *
 * @return The concrete {@link EventLoopGroup} instance that will be used by the transport.
 */
private EventLoopGroup createEventLoopGroup(ChannelType type) {
    switch (type) {
        case NIO_TCP:
        case NIO_UDP:
            if (Epoll.isAvailable()) {
                log.debug("Using EpollEventLoopGroup");
                return new EpollEventLoopGroup(8, executorService, DefaultSelectStrategyFactory.INSTANCE);
            }
            return new NioEventLoopGroup(8, executorService, SelectorProvider.provider(), DefaultSelectStrategyFactory.INSTANCE);
    }
    return null;
}
 
Example 12
Source File: SelectPipe.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        SelectorProvider sp = SelectorProvider.provider();
        Selector selector = Selector.open();
        Pipe p = sp.openPipe();
        Pipe.SinkChannel sink = p.sink();
        Pipe.SourceChannel source = p.source();

        source.configureBlocking(false);
        sink.configureBlocking(false);

        SelectionKey readkey = source.register(selector, SelectionKey.OP_READ);
        SelectionKey writekey = sink.register(selector, SelectionKey.OP_WRITE);

        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;
        }

        if (selector.select(1000) == 0) {
            throw new Exception("test failed");
        }

        ByteBuffer incomingdata = ByteBuffer.allocateDirect(10);
        int totalRead = 0;
        do {
            int bytesRead = source.read(incomingdata);
            if (bytesRead > 0)
                totalRead += bytesRead;
        } while(totalRead < 10);

        sink.close();
        source.close();
        selector.close();

        for(int i=0; i<10; i++)
            if (outgoingdata.get(i) != incomingdata.get(i))
                throw new Exception("Pipe failed");
    }
 
Example 13
Source File: Transfer.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void testReadableByteChannel(int size) throws Exception {
    SelectorProvider sp = SelectorProvider.provider();
    Pipe p = sp.openPipe();
    Pipe.SinkChannel sink = p.sink();
    Pipe.SourceChannel source = p.source();
    sink.configureBlocking(false);

    ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
    byte[] someBytes = new byte[size + 10];
    generator.nextBytes(someBytes);
    outgoingdata.put(someBytes);
    outgoingdata.flip();

    int totalWritten = 0;
    while (totalWritten < size + 10) {
        int written = sink.write(outgoingdata);
        if (written < 0)
            throw new Exception("Write failed");
        totalWritten += written;
    }

    File f = File.createTempFile("blah"+size, null);
    f.deleteOnExit();
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    FileChannel fc = raf.getChannel();
    long oldPosition = fc.position();

    long bytesWritten = fc.transferFrom(source, 0, size);
    fc.force(true);
    if (bytesWritten != size)
        throw new RuntimeException("Transfer failed");

    if (fc.position() != oldPosition)
        throw new RuntimeException("Position changed");

    if (fc.size() != size)
        throw new RuntimeException("Unexpected sink size "+ fc.size());

    fc.close();
    sink.close();
    source.close();

    f.delete();
}
 
Example 14
Source File: Secrets.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private static SelectorProvider provider() {
    SelectorProvider p = SelectorProvider.provider();
    if (!(p instanceof SelectorProviderImpl))
        throw new UnsupportedOperationException();
    return p;
}
 
Example 15
Source File: Transfer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static void testReadableByteChannel(int size) throws Exception {
    SelectorProvider sp = SelectorProvider.provider();
    Pipe p = sp.openPipe();
    Pipe.SinkChannel sink = p.sink();
    Pipe.SourceChannel source = p.source();
    sink.configureBlocking(false);

    ByteBuffer outgoingdata = ByteBuffer.allocateDirect(size + 10);
    byte[] someBytes = new byte[size + 10];
    generator.nextBytes(someBytes);
    outgoingdata.put(someBytes);
    outgoingdata.flip();

    int totalWritten = 0;
    while (totalWritten < size + 10) {
        int written = sink.write(outgoingdata);
        if (written < 0)
            throw new Exception("Write failed");
        totalWritten += written;
    }

    File f = File.createTempFile("blah"+size, null);
    f.deleteOnExit();
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    FileChannel fc = raf.getChannel();
    long oldPosition = fc.position();

    long bytesWritten = fc.transferFrom(source, 0, size);
    fc.force(true);
    if (bytesWritten != size)
        throw new RuntimeException("Transfer failed");

    if (fc.position() != oldPosition)
        throw new RuntimeException("Position changed");

    if (fc.size() != size)
        throw new RuntimeException("Unexpected sink size "+ fc.size());

    fc.close();
    sink.close();
    source.close();

    f.delete();
}
 
Example 16
Source File: SelectPipe.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        SelectorProvider sp = SelectorProvider.provider();
        Selector selector = Selector.open();
        Pipe p = sp.openPipe();
        Pipe.SinkChannel sink = p.sink();
        Pipe.SourceChannel source = p.source();

        source.configureBlocking(false);
        sink.configureBlocking(false);

        SelectionKey readkey = source.register(selector, SelectionKey.OP_READ);
        SelectionKey writekey = sink.register(selector, SelectionKey.OP_WRITE);

        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;
        }

        if (selector.select(1000) == 0) {
            throw new Exception("test failed");
        }

        ByteBuffer incomingdata = ByteBuffer.allocateDirect(10);
        int totalRead = 0;
        do {
            int bytesRead = source.read(incomingdata);
            if (bytesRead > 0)
                totalRead += bytesRead;
        } while(totalRead < 10);

        sink.close();
        source.close();
        selector.close();

        for(int i=0; i<10; i++)
            if (outgoingdata.get(i) != incomingdata.get(i))
                throw new Exception("Pipe failed");
    }
 
Example 17
Source File: Secrets.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private static SelectorProvider provider() {
    SelectorProvider p = SelectorProvider.provider();
    if (!(p instanceof SelectorProviderImpl))
        throw new UnsupportedOperationException();
    return p;
}
 
Example 18
Source File: Secrets.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private static SelectorProvider provider() {
    SelectorProvider p = SelectorProvider.provider();
    if (!(p instanceof SelectorProviderImpl))
        throw new UnsupportedOperationException();
    return p;
}
 
Example 19
Source File: SelectPipe.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        SelectorProvider sp = SelectorProvider.provider();
        Selector selector = Selector.open();
        Pipe p = sp.openPipe();
        Pipe.SinkChannel sink = p.sink();
        Pipe.SourceChannel source = p.source();

        source.configureBlocking(false);
        sink.configureBlocking(false);

        SelectionKey readkey = source.register(selector, SelectionKey.OP_READ);
        SelectionKey writekey = sink.register(selector, SelectionKey.OP_WRITE);

        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;
        }

        if (selector.select(1000) == 0) {
            throw new Exception("test failed");
        }

        ByteBuffer incomingdata = ByteBuffer.allocateDirect(10);
        int totalRead = 0;
        do {
            int bytesRead = source.read(incomingdata);
            if (bytesRead > 0)
                totalRead += bytesRead;
        } while(totalRead < 10);

        sink.close();
        source.close();
        selector.close();

        for(int i=0; i<10; i++)
            if (outgoingdata.get(i) != incomingdata.get(i))
                throw new Exception("Pipe failed");
    }
 
Example 20
Source File: NioEventLoopGroup.java    From netty4.0.27Learn with Apache License 2.0 2 votes vote down vote up
/**
 * Create a new instance using the specified number of threads, the given {@link ThreadFactory} and the
 * {@link SelectorProvider} which is returned by {@link SelectorProvider#provider()}.
 */
public NioEventLoopGroup(int nThreads, ThreadFactory threadFactory) {
    this(nThreads, threadFactory, SelectorProvider.provider());
}