java.nio.channels.AsynchronousByteChannel Java Examples

The following examples show how to use java.nio.channels.AsynchronousByteChannel. 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: ChannelsTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example #2
Source File: ChannelsTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #3
Source File: RedissonBinaryStreamTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #4
Source File: CompletableAsynchronousByteChannel.java    From tascalate-async-await with BSD 2-Clause "Simplified" License 4 votes vote down vote up
public static CompletableAsynchronousByteChannel adapt(AsynchronousByteChannel original) {
    if (original instanceof CompletableAsynchronousByteChannel) {
        return (CompletableAsynchronousByteChannel)original;
    }
    return new Adapter(original);
}
 
Example #5
Source File: CompletableAsynchronousByteChannel.java    From tascalate-async-await with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private Adapter(AsynchronousByteChannel delegate) {
    this.delegate = delegate;
}
 
Example #6
Source File: RedissonBinaryStream.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Override
public AsynchronousByteChannel getAsynchronousChannel() {
    return new RedissonAsynchronousByteChannel();
}
 
Example #7
Source File: SqlBlob.java    From pgadba with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
  * 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();
 
Example #8
Source File: SqlBlob.java    From pgadba with BSD 2-Clause "Simplified" License 2 votes vote down vote up
/**
 * 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();
 
Example #9
Source File: RBinaryStream.java    From redisson with Apache License 2.0 2 votes vote down vote up
/**
 * Returns async channel object which allows to write and read binary stream.
 * This object isn't thread-safe.
 *
 * @return channel object
 */
AsynchronousByteChannel getAsynchronousChannel();