类java.nio.channels.AsynchronousByteChannel源码实例Demo

下面列出了怎么用java.nio.channels.AsynchronousByteChannel的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: j2objc   文件: ChannelsTest.java
public void testOutputStreamAsynchronousByteChannel() throws Exception {
      AsynchronousByteChannel abc = mock(AsynchronousByteChannel.class);
      OutputStream os = Channels.newOutputStream(abc);
      Future<Integer> result = mock(Future.class);
      ArgumentCaptor<ByteBuffer> bbCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
      final byte[] data = "world".getBytes();

      when(abc.write(bbCaptor.capture())).thenReturn(result);
      when(result.get()).thenAnswer(
          new Answer<Integer>() {
              public Integer answer(InvocationOnMock invocation) {
                  ByteBuffer bb = bbCaptor.getValue();
                  assertEquals(data.length, bb.remaining());
                  byte[] readData = new byte[data.length];
                  // Read the whole thing
                  bb.get(readData);
                  assertTrue(Arrays.equals(data, readData));
                  return data.length;
              }
          });

      os.write(data);

      Mockito.verify(abc).write(isA(ByteBuffer.class));
      Mockito.verify(result).get();
}
 
源代码2 项目: j2objc   文件: ChannelsTest.java
public void testInputStreamAsynchronousByteChannel() throws Exception {
    AsynchronousByteChannel abc = mock(AsynchronousByteChannel.class);
    InputStream is = Channels.newInputStream(abc);
    Future<Integer> result = mock(Future.class);
    ArgumentCaptor<ByteBuffer> bbCaptor = ArgumentCaptor.forClass(ByteBuffer.class);
    final byte[] bytesRead = new byte[10];

    when(abc.read(bbCaptor.capture())).thenReturn(result);
    when(result.get()).thenAnswer(
        new Answer<Integer>() {
            public Integer answer(InvocationOnMock invocation) {
                ByteBuffer bb = bbCaptor.getValue();
                assertEquals(bytesRead.length, bb.remaining());
                // Write '7' bytes
                bb.put(new byte[] {0, 1, 2, 3, 4, 5, 6});
                return 7;
            }
        });

    assertEquals(7, is.read(bytesRead));
    // Only 7 bytes of data should be written into the buffer
    byte[] bytesExpected = new byte[] { 0, 1, 2, 3, 4, 5, 6, 0, 0, 0 };
    assertTrue(Arrays.equals(bytesExpected, bytesRead));

    Mockito.verify(abc).read(isA(ByteBuffer.class));
    Mockito.verify(result).get();
}
 
源代码3 项目: redisson   文件: RedissonBinaryStreamTest.java
@Test
public void testAsyncReadWrite() throws ExecutionException, InterruptedException {
    RBinaryStream stream = redisson.getBinaryStream("test");

    AsynchronousByteChannel channel = stream.getAsynchronousChannel();
    ByteBuffer bb = ByteBuffer.wrap(new byte[]{1, 2, 3, 4, 5, 6, 7});
    channel.write(bb).get();

    AsynchronousByteChannel channel2 = stream.getAsynchronousChannel();
    ByteBuffer b = ByteBuffer.allocate(7);
    channel2.read(b).get();

    b.flip();
    assertThat(b).isEqualByComparingTo(bb);
}
 
public static CompletableAsynchronousByteChannel adapt(AsynchronousByteChannel original) {
    if (original instanceof CompletableAsynchronousByteChannel) {
        return (CompletableAsynchronousByteChannel)original;
    }
    return new Adapter(original);
}
 
private Adapter(AsynchronousByteChannel delegate) {
    this.delegate = delegate;
}
 
源代码6 项目: redisson   文件: RedissonBinaryStream.java
@Override
public AsynchronousByteChannel getAsynchronousChannel() {
    return new RedissonAsynchronousByteChannel();
}
 
源代码7 项目: pgadba   文件: SqlBlob.java
/**
  * Return a {@link java.nio.channels.Channel} that can be used to read bytes from the
  * {@code SqlBlob} beginning at the position. Reading bytes from the returned
  * {@link java.nio.channels.Channel} advances the position.
  *
  * Each call to a read method that fetches bytes from the server creates and
  * submits a virtual {@link Operation} to fetch those bytes. This virtual
  * {@link Operation} is executed in sequence with other {@link Operation}s and
  * may be skipped if an error occurs.
  *
  * @return a read-only byte {@link java.nio.channels.Channel} beginning at the position.
  * @throws IllegalStateException if the {@link Session} that created this
SqlBlob is closed.
  */
 public AsynchronousByteChannel getReadChannel();
 
源代码8 项目: pgadba   文件: SqlBlob.java
/**
 * Return a {@link java.nio.channels.Channel} that can be used to write bytes
 * to this {@code SqlBlob} beginning at the position. Bytes written overwrite
 * bytes already in the {@code SqlBlob}. Writing bytes to the returned
 * {@link java.nio.channels.Channel} advances the position.
 *
 * Each call to a write method that flushes bytes to the server creates and
 * submits a virtual {@link Operation} to flush those bytes. This virtual
 * {@link Operation} is executed in sequence with other {@link Operation}s and
 * may be skipped if an error occurs.
 *
 * ISSUE: Can the app read bytes from a write
 * {@link java.nio.channels.Channel}? If so then maybe remove
 * {@link getReadChannel} and add a read-only flag to this method, renamed
 * {@code getChannel}.
 *
 * @return a writable byte {@link java.nio.channels.Channel} beginning at the
 * position.
 * @throws IllegalStateException if the {@link Session} that created this
 * {@code SqlBlob} is closed.
 */
public AsynchronousByteChannel getWriteChannel();
 
源代码9 项目: redisson   文件: RBinaryStream.java
/**
 * Returns async channel object which allows to write and read binary stream.
 * This object isn't thread-safe.
 *
 * @return channel object
 */
AsynchronousByteChannel getAsynchronousChannel();
 
 类所在包
 同包方法