下面列出了怎么用java.nio.channels.AsynchronousByteChannel的API类实例代码及写法,或者点击链接到github查看源代码。
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();
}
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();
}
@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;
}
@Override
public AsynchronousByteChannel getAsynchronousChannel() {
return new RedissonAsynchronousByteChannel();
}
/**
* 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();
/**
* 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();
/**
* Returns async channel object which allows to write and read binary stream.
* This object isn't thread-safe.
*
* @return channel object
*/
AsynchronousByteChannel getAsynchronousChannel();