Search in sources :

Example 11 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project jdk8u_jdk by JetBrains.

the class SelectPipe method main.

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");
}
Also used : SelectorProvider(java.nio.channels.spi.SelectorProvider) ByteBuffer(java.nio.ByteBuffer)

Example 12 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project jdk8u_jdk by JetBrains.

the class Transfer method testReadableByteChannel.

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();
}
Also used : FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) SelectorProvider(java.nio.channels.spi.SelectorProvider)

Example 13 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project jdk8u_jdk by JetBrains.

the class EmptyRead method main.

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();
}
Also used : SelectorProvider(java.nio.channels.spi.SelectorProvider) ByteBuffer(java.nio.ByteBuffer)

Example 14 with SelectorProvider

use of java.nio.channels.spi.SelectorProvider in project webpieces by deanhiller.

the class ChannelManagerFactory method createFactory.

/**
 * All Keys(and some values) to put in the map variable can be found
 * as the constants in ChannelManaagerFactory
 */
public static ChannelManagerFactory createFactory(MeterRegistry metrics) {
    SelectorProvider provider = SelectorProvider.provider();
    JdkSelectorImpl selector = new JdkSelectorImpl(provider);
    return createFactory(selector, metrics);
}
Also used : SelectorProvider(java.nio.channels.spi.SelectorProvider) JdkSelectorImpl(org.webpieces.nio.impl.jdk.JdkSelectorImpl)

Aggregations

SelectorProvider (java.nio.channels.spi.SelectorProvider)14 InetAddress (java.net.InetAddress)3 InetSocketAddress (java.net.InetSocketAddress)3 ByteBuffer (java.nio.ByteBuffer)3 Channel (java.nio.channels.Channel)3 SocketChannel (java.nio.channels.SocketChannel)3 FileDescriptor (java.io.FileDescriptor)2 Field (java.lang.reflect.Field)2 DatagramChannel (java.nio.channels.DatagramChannel)2 Selector (java.nio.channels.Selector)2 ServerSocketChannel (java.nio.channels.ServerSocketChannel)2 PrivilegedAction (java.security.PrivilegedAction)2 DefaultSelectStrategyFactory (io.netty.channel.DefaultSelectStrategyFactory)1 EventLoopTaskQueueFactory (io.netty.channel.EventLoopTaskQueueFactory)1 SelectStrategyFactory (io.netty.channel.SelectStrategyFactory)1 RejectedExecutionHandler (io.netty.util.concurrent.RejectedExecutionHandler)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 AbstractSelector (java.nio.channels.spi.AbstractSelector)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1