WritableMessageDigest implements WritableByteChannel { @Override public int write(ByteBuffer src) throws IOException { final int r = src.remaining(); this.messageDigest.update(src); return r; } WritableMessageDigest(MessageDigest digest); MessageDigest getMessageDigest(); @Override boolean isOpen(); @Override void close(); @Override int write(ByteBuffer src);  }
@Test public void testWrite() throws IOException { final byte[] input = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; final byte[] expected = this.writeableMessageDigest.getMessageDigest().digest(input); final ByteBuffer buf = ByteBuffer.wrap(input); assertEquals(buf.capacity(), this.writeableMessageDigest.write(buf)); final byte[] actual = this.writeableMessageDigest.getMessageDigest().digest(); assertArrayEquals(expected, actual); }