ByteArrayWritableByteChannel implements WritableByteChannel { @Override public int write(ByteBuffer src) throws IOException { int remain = src.remaining(); for(int i=0; i < remain ; i++){ baous.write(src.get()); } return remain; }  @Override boolean isOpen(); @Override void close(); @Override int write(ByteBuffer src); byte [] getResult();  }
@Test public void testWrite() throws IOException { String content = randomString(); try (ByteArrayWritableByteChannel channel = new ByteArrayWritableByteChannel()) { ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes()); channel.write(byteBuffer); byte[] result = channel.getResult(); Assert.assertEquals(0, byteBuffer.remaining()); Assert.assertEquals(content, new String(result)); } }