java.nio.channels.Pipe Java Examples

The following examples show how to use java.nio.channels.Pipe. 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: PackageUtilTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testPackageUploadWithExplicitPackageName() throws Exception {
  Pipe pipe = Pipe.open();
  File tmpFile = makeFileWithContents("file.txt", "This is a test!");
  final String overriddenName = "alias.txt";

  when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
      .thenReturn(
          ImmutableList.of(
              StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));
  when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());

  List<DataflowPackage> targets =
      defaultPackageUtil.stageClasspathElements(
          ImmutableList.of(makeStagedFile(tmpFile.getAbsolutePath(), overriddenName)),
          STAGING_PATH,
          createOptions);
  DataflowPackage target = Iterables.getOnlyElement(targets);

  verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
  verify(mockGcsUtil).create(any(GcsPath.class), anyString());
  verifyNoMoreInteractions(mockGcsUtil);

  assertThat(target.getName(), equalTo(overriddenName));
  assertThat(target.getLocation(), RegexMatcher.matches(STAGING_PATH + "alias.txt"));
}
 
Example #2
Source File: SelectorTest.java    From cava with Apache License 2.0 6 votes vote down vote up
@Test
void selectorRemovesKeysOnCancelWhenSelecting() throws Exception {
  Pipe pipe = Pipe.open();

  Selector selector = Selector.open();
  SelectableChannel source = pipe.source();
  source.configureBlocking(false);

  SelectionKey key = source.register(selector, OP_READ);
  assertTrue(selector.keys().contains(key));

  key.cancel();
  assertTrue(selector.keys().contains(key));
  assertSame(key, source.keyFor(selector));

  selector.selectNow();
  assertFalse(selector.keys().contains(key));
  assertNull(source.keyFor(selector));
}
 
Example #3
Source File: TestPipe.java    From cs-summary-reflection with Apache License 2.0 6 votes vote down vote up
@Test
public void test1() throws IOException {
    // 1. 获取管道
    Pipe pipe = Pipe.open();

    // 2. 将缓冲区中的数据写入管道
    ByteBuffer buf = ByteBuffer.allocate(1024);

    Pipe.SinkChannel sinkChannel = pipe.sink();
    buf.put("通过单向管道发送数据".getBytes());
    buf.flip();
    sinkChannel.write(buf);

    // 3. 读取缓冲区中的数据
    Pipe.SourceChannel sourceChannel = pipe.source();
    buf.flip();
    int len = sourceChannel.read(buf);
    System.out.println(new String(buf.array(), 0, len));

    sourceChannel.close();
    sinkChannel.close();
}
 
Example #4
Source File: SelectorTest.java    From cava with Apache License 2.0 6 votes vote down vote up
@Test
void selectorRemovesKeysOnChannelCloseWhenSelecting() throws Exception {
  Pipe pipe = Pipe.open();

  Selector selector = Selector.open();
  SelectableChannel source = pipe.source();
  source.configureBlocking(false);

  SelectionKey key = source.register(selector, OP_READ);
  assertTrue(selector.keys().contains(key));

  source.close();
  assertTrue(selector.keys().contains(key));

  selector.selectNow();
  assertFalse(selector.keys().contains(key));
}
 
Example #5
Source File: SelectorTest.java    From cava with Apache License 2.0 6 votes vote down vote up
@Test
void cancelledKeyRemovedFromChannel() throws Exception {
  Pipe pipe = Pipe.open();

  Selector selector = Selector.open();
  SelectableChannel source = pipe.source();
  source.configureBlocking(false);

  for (int i = 0; i < 1000; ++i) {
    assertNull(source.keyFor(selector));

    SelectionKey key = source.register(selector, OP_READ);

    selector.selectedKeys().clear();
    selector.selectNow();

    key.cancel();
    selector.wakeup();

    selector.selectedKeys().clear();
    selector.selectNow();
  }
}
 
Example #6
Source File: MatcherTest.java    From ExpectIt with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a mock input stream which send some data every SMALL_TIMEOUT ms.
 */
@Before
public void setup() throws Exception {
    mock = TestUtils.mockInputStream(text);
    final Pipe pipe = Pipe.open();
    input = new SingleInputExpect(
            pipe.source(),
            pipe.sink(),
            mock.getStream(),
            Charset.defaultCharset(),
            null,
            null,
            DEFAULT_BUFFER_SIZE,
            false);
    executor = Executors.newSingleThreadExecutor();
    input.start(executor);
    mock.waitUntilReady();
}
 
Example #7
Source File: PackageUtilTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testPackageUploadIsNotSkippedWhenSizesAreDifferent() throws Exception {
  Pipe pipe = Pipe.open();
  File tmpDirectory = tmpFolder.newFolder("folder");
  tmpFolder.newFolder("folder", "empty_directory");
  tmpFolder.newFolder("folder", "directory");
  makeFileWithContents("folder/file.txt", "This is a test!");
  makeFileWithContents("folder/directory/file.txt", "This is also a test!");
  when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
      .thenReturn(
          ImmutableList.of(
              StorageObjectOrIOException.create(
                  createStorageObject(STAGING_PATH, Long.MAX_VALUE))));
  when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());

  defaultPackageUtil.stageClasspathElements(
      ImmutableList.of(makeStagedFile(tmpDirectory.getAbsolutePath())),
      STAGING_PATH,
      createOptions);

  verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
  verify(mockGcsUtil).create(any(GcsPath.class), anyString());
  verifyNoMoreInteractions(mockGcsUtil);
}
 
Example #8
Source File: SingleInputExpect.java    From ExpectIt with Apache License 2.0 6 votes vote down vote up
protected SingleInputExpect(
        final Pipe.SourceChannel source,
        final Pipe.SinkChannel sink,
        final InputStream input,
        final Charset charset,
        final Appendable echoInput,
        final Filter filter,
        final int bufferSize,
        final boolean autoFlushEcho) throws IOException {
    this.input = input;
    this.charset = charset;
    this.echoInput = echoInput;
    this.filter = filter;
    this.bufferSize = bufferSize;
    this.autoFlushEcho = autoFlushEcho;
    this.source = source;
    this.sink = sink;
    source.configureBlocking(false);
    buffer = new StringBuilder();
}
 
Example #9
Source File: BaseAbstractGoogleAsyncWriteChannel.java    From hadoop-connectors with Apache License 2.0 6 votes vote down vote up
private InputStream initializeUploadPipe() throws IOException {
  switch (options.getPipeType()) {
    case NIO_CHANNEL_PIPE:
      Pipe pipe = Pipe.open();
      pipeSink = pipe.sink();
      InputStream pipeSource = Channels.newInputStream(pipe.source());
      return options.getPipeBufferSize() > 0
          ? new BufferedInputStream(pipeSource, options.getPipeBufferSize())
          : pipeSource;
    case IO_STREAM_PIPE:
      PipedInputStream internalPipeSource = new PipedInputStream(options.getPipeBufferSize());
      PipedOutputStream internalPipeSink = new PipedOutputStream(internalPipeSource);
      pipeSink = Channels.newChannel(internalPipeSink);
      return internalPipeSource;
  }
  throw new IllegalStateException("Unknown PipeType: " + options.getPipeType());
}
 
Example #10
Source File: TestPipe.java    From code with Apache License 2.0 6 votes vote down vote up
@Test
public void test1() throws IOException {
    //1.获取管道
    Pipe pipe = Pipe.open();
    //2.将缓冲区数据写入管道
    ByteBuffer buf = ByteBuffer.allocate(1024);

    Pipe.SinkChannel sinkChannel = pipe.sink();
    buf.put("通过单向管道发送数据".getBytes());
    buf.flip();
    sinkChannel.write(buf);

    //3.读取缓冲区中的数据
    Pipe.SourceChannel sourceChannel = pipe.source();
    buf.flip();
    int len = sourceChannel.read(buf);
    System.out.println(new String(buf.array(), 0, len));

    sourceChannel.close();
    sinkChannel.close();

}
 
Example #11
Source File: SelectorTest.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
@Test
void cancelledKeyRemovedFromChannel() throws Exception {
  Pipe pipe = Pipe.open();

  Selector selector = Selector.open();
  SelectableChannel source = pipe.source();
  source.configureBlocking(false);

  for (int i = 0; i < 1000; ++i) {
    assertNull(source.keyFor(selector));

    SelectionKey key = source.register(selector, OP_READ);

    selector.selectedKeys().clear();
    selector.selectNow();

    key.cancel();
    selector.wakeup();

    selector.selectedKeys().clear();
    selector.selectNow();
  }
}
 
Example #12
Source File: SelectorTest.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
@Test
void selectorRemovesKeysOnCancelWhenSelecting() throws Exception {
  Pipe pipe = Pipe.open();

  Selector selector = Selector.open();
  SelectableChannel source = pipe.source();
  source.configureBlocking(false);

  SelectionKey key = source.register(selector, OP_READ);
  assertTrue(selector.keys().contains(key));

  key.cancel();
  assertTrue(selector.keys().contains(key));
  assertSame(key, source.keyFor(selector));

  selector.selectNow();
  assertFalse(selector.keys().contains(key));
  assertNull(source.keyFor(selector));
}
 
Example #13
Source File: SelectorTest.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
@Test
void selectorRemovesKeysOnChannelCloseWhenSelecting() throws Exception {
  Pipe pipe = Pipe.open();

  Selector selector = Selector.open();
  SelectableChannel source = pipe.source();
  source.configureBlocking(false);

  SelectionKey key = source.register(selector, OP_READ);
  assertTrue(selector.keys().contains(key));

  source.close();
  assertTrue(selector.keys().contains(key));

  selector.selectNow();
  assertFalse(selector.keys().contains(key));
}
 
Example #14
Source File: PipeInterrupt.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws Exception {

        Thread tester = new Thread("PipeTester") {
            private Pipe testPipe = null;

            @Override
            public void run() {
                for (;;) {
                    boolean interrupted = this.isInterrupted();
                    try {
                        testPipe = Pipe.open();
                        close();
                        if (interrupted) {
                            if (!this.isInterrupted())
                               exc = new RuntimeException("interrupt status reset");
                            break;
                        }
                    } catch (IOException ioe) {
                        exc = ioe;
                    }
                }
            }

            private void close() throws IOException {
                if (testPipe != null) {
                    testPipe.sink().close();
                    testPipe.source().close();
                }
            }
        };

        tester.start();
        Thread.sleep(200);
        tester.interrupt();
        tester.join();

        if (exc != null)
            throw exc;
    }
 
Example #15
Source File: PackageUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testPackageUploadWithFileSucceeds() throws Exception {
  Pipe pipe = Pipe.open();
  String contents = "This is a test!";
  File tmpFile = makeFileWithContents("file.txt", contents);
  when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
      .thenReturn(
          ImmutableList.of(
              StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));

  when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());

  List<DataflowPackage> targets =
      defaultPackageUtil.stageClasspathElements(
          ImmutableList.of(makeStagedFile(tmpFile.getAbsolutePath())),
          STAGING_PATH,
          createOptions);
  DataflowPackage target = Iterables.getOnlyElement(targets);

  verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
  verify(mockGcsUtil).create(any(GcsPath.class), anyString());
  verifyNoMoreInteractions(mockGcsUtil);

  assertThat(target.getName(), endsWith(".txt"));
  assertThat(target.getLocation(), equalTo(STAGING_PATH + target.getName()));
  assertThat(
      new LineReader(Channels.newReader(pipe.source(), StandardCharsets.UTF_8.name())).readLine(),
      equalTo(contents));
}
 
Example #16
Source File: WindowsSelectorImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
WindowsSelectorImpl(SelectorProvider sp) throws IOException {
    super(sp);
    pollWrapper = new PollArrayWrapper(INIT_CAP);
    wakeupPipe = Pipe.open();
    wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();

    // Disable the Nagle algorithm so that the wakeup is more immediate
    SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
    (sink.sc).socket().setTcpNoDelay(true);
    wakeupSinkFd = ((SelChImpl)sink).getFDVal();

    pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
}
 
Example #17
Source File: EchoInputStreamWrapper.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
public static SourceChannel wrap(InputStream in) throws IOException {
    Pipe pipe = Pipe.open();
    new EchoInputStreamWrapper(in, pipe.sink()).start();
    SourceChannel result = pipe.source();
    result.configureBlocking(false);
    return result;
}
 
Example #18
Source File: WindowsSelectorImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
WindowsSelectorImpl(SelectorProvider sp) throws IOException {
    super(sp);
    pollWrapper = new PollArrayWrapper(INIT_CAP);
    wakeupPipe = Pipe.open();
    wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();

    // Disable the Nagle algorithm so that the wakeup is more immediate
    SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
    (sink.sc).socket().setTcpNoDelay(true);
    wakeupSinkFd = ((SelChImpl)sink).getFDVal();

    pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
}
 
Example #19
Source File: PipeInterrupt.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws Exception {

        Thread tester = new Thread("PipeTester") {
            private Pipe testPipe = null;

            @Override
            public void run() {
                for (;;) {
                    boolean interrupted = this.isInterrupted();
                    try {
                        testPipe = Pipe.open();
                        close();
                        if (interrupted) {
                            if (!this.isInterrupted())
                               exc = new RuntimeException("interrupt status reset");
                            break;
                        }
                    } catch (IOException ioe) {
                        exc = ioe;
                    }
                }
            }

            private void close() throws IOException {
                if (testPipe != null) {
                    testPipe.sink().close();
                    testPipe.source().close();
                }
            }
        };

        tester.start();
        Thread.sleep(200);
        tester.interrupt();
        tester.join();

        if (exc != null)
            throw exc;
    }
 
Example #20
Source File: PackageUtilTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@Test
public void testPackageUploadWithEmptyDirectorySucceeds() throws Exception {
  Pipe pipe = Pipe.open();
  File tmpDirectory = tmpFolder.newFolder("folder");

  when(mockGcsUtil.getObjects(anyListOf(GcsPath.class)))
      .thenReturn(
          ImmutableList.of(
              StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));
  when(mockGcsUtil.create(any(GcsPath.class), anyString())).thenReturn(pipe.sink());

  List<DataflowPackage> targets =
      defaultPackageUtil.stageClasspathElements(
          ImmutableList.of(makeStagedFile(tmpDirectory.getAbsolutePath())),
          STAGING_PATH,
          createOptions);
  DataflowPackage target = Iterables.getOnlyElement(targets);

  verify(mockGcsUtil).getObjects(anyListOf(GcsPath.class));
  verify(mockGcsUtil).create(any(GcsPath.class), anyString());
  verifyNoMoreInteractions(mockGcsUtil);

  assertThat(target.getName(), endsWith(".jar"));
  assertThat(target.getLocation(), equalTo(STAGING_PATH + target.getName()));
  try (ZipInputStream zipInputStream =
      new ZipInputStream(Channels.newInputStream(pipe.source()))) {
    assertNull(zipInputStream.getNextEntry());
  }
}
 
Example #21
Source File: ChannelsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * This fails on the RI which violates its own promise to throw when
 * read in non-blocking mode.
 */
public void testReaderNonBlocking() throws IOException {
    Pipe.SourceChannel sourceChannel = createNonBlockingChannel("abc".getBytes("UTF-8"));
    try {
        Channels.newReader(sourceChannel, "UTF-8").read();
        fail();
    } catch (IllegalBlockingModeException expected) {
    }
}
 
Example #22
Source File: WindowsSelectorImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
WindowsSelectorImpl(SelectorProvider sp) throws IOException {
    super(sp);
    pollWrapper = new PollArrayWrapper(INIT_CAP);
    wakeupPipe = Pipe.open();
    wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();

    // Disable the Nagle algorithm so that the wakeup is more immediate
    SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
    (sink.sc).socket().setTcpNoDelay(true);
    wakeupSinkFd = ((SelChImpl)sink).getFDVal();

    pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
}
 
Example #23
Source File: RsyncClient.java    From yajsync with GNU General Public License v3.0 5 votes vote down vote up
private static Pipe[] pipePair()
{
    try {
        return new Pipe[] { Pipe.open(), Pipe.open() };
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #24
Source File: PipeInterrupt.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws Exception {

        Thread tester = new Thread("PipeTester") {
            private Pipe testPipe = null;

            @Override
            public void run() {
                for (;;) {
                    boolean interrupted = this.isInterrupted();
                    try {
                        testPipe = Pipe.open();
                        close();
                        if (interrupted) {
                            if (!this.isInterrupted())
                               exc = new RuntimeException("interrupt status reset");
                            break;
                        }
                    } catch (IOException ioe) {
                        exc = ioe;
                    }
                }
            }

            private void close() throws IOException {
                if (testPipe != null) {
                    testPipe.sink().close();
                    testPipe.source().close();
                }
            }
        };

        tester.start();
        Thread.sleep(200);
        tester.interrupt();
        tester.join();

        if (exc != null)
            throw exc;
    }
 
Example #25
Source File: WindowsSelectorImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
WindowsSelectorImpl(SelectorProvider sp) throws IOException {
    super(sp);
    pollWrapper = new PollArrayWrapper(INIT_CAP);
    wakeupPipe = Pipe.open();
    wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();

    // Disable the Nagle algorithm so that the wakeup is more immediate
    SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
    (sink.sc).socket().setTcpNoDelay(true);
    wakeupSinkFd = ((SelChImpl)sink).getFDVal();

    pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
}
 
Example #26
Source File: PipeInterrupt.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws Exception {

        Thread tester = new Thread("PipeTester") {
            private Pipe testPipe = null;

            @Override
            public void run() {
                for (;;) {
                    boolean interrupted = this.isInterrupted();
                    try {
                        testPipe = Pipe.open();
                        close();
                        if (interrupted) {
                            if (!this.isInterrupted())
                               exc = new RuntimeException("interrupt status reset");
                            break;
                        }
                    } catch (IOException ioe) {
                        exc = ioe;
                    }
                }
            }

            private void close() throws IOException {
                if (testPipe != null) {
                    testPipe.sink().close();
                    testPipe.source().close();
                }
            }
        };

        tester.start();
        Thread.sleep(200);
        tester.interrupt();
        tester.join();

        if (exc != null)
            throw exc;
    }
 
Example #27
Source File: WindowsSelectorImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
WindowsSelectorImpl(SelectorProvider sp) throws IOException {
    super(sp);
    pollWrapper = new PollArrayWrapper(INIT_CAP);
    wakeupPipe = Pipe.open();
    wakeupSourceFd = ((SelChImpl)wakeupPipe.source()).getFDVal();

    // Disable the Nagle algorithm so that the wakeup is more immediate
    SinkChannelImpl sink = (SinkChannelImpl)wakeupPipe.sink();
    (sink.sc).socket().setTcpNoDelay(true);
    wakeupSinkFd = ((SelChImpl)sink).getFDVal();

    pollWrapper.addWakeupSocket(wakeupSourceFd, 0);
}
 
Example #28
Source File: NBCircularIOStream.java    From imhotep with Apache License 2.0 5 votes vote down vote up
public NBCircularIOStream() throws IOException {
    final Pipe pipe = Pipe.open();
    sink = new BufferedWritableSelectableChannel(new PipeSinkWritableSelectableChannel(pipe.sink()));
    final Pipe.SourceChannel source = pipe.source();
    sink.configureBlocking(false);
    source.configureBlocking(true);
    in = Channels.newInputStream(source);
}
 
Example #29
Source File: PipeInterrupt.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void test() throws Exception {

        Thread tester = new Thread("PipeTester") {
            private Pipe testPipe = null;

            @Override
            public void run() {
                for (;;) {
                    boolean interrupted = this.isInterrupted();
                    try {
                        testPipe = Pipe.open();
                        close();
                        if (interrupted) {
                            if (!this.isInterrupted())
                               exc = new RuntimeException("interrupt status reset");
                            break;
                        }
                    } catch (IOException ioe) {
                        exc = ioe;
                    }
                }
            }

            private void close() throws IOException {
                if (testPipe != null) {
                    testPipe.sink().close();
                    testPipe.source().close();
                }
            }
        };

        tester.start();
        Thread.sleep(200);
        tester.interrupt();
        tester.join();

        if (exc != null)
            throw exc;
    }
 
Example #30
Source File: SelectorTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void selectorRemovesKeysOnChannelCloseWhileSelecting() throws Exception {
  Pipe pipe = Pipe.open();

  Selector selector = Selector.open();
  SelectableChannel source = pipe.source();
  source.configureBlocking(false);

  SelectionKey key = source.register(selector, OP_READ);
  assertTrue(selector.keys().contains(key));

  CountDownLatch latch = new CountDownLatch(1);
  Future<?> job = executor.submit(() -> {
    latch.countDown();
    try {
      selector.select();
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  });

  latch.await();
  Thread.sleep(100);

  source.close();
  selector.wakeup();
  job.get();
  assertFalse(selector.keys().contains(key));
}