ByteBufferInputStream extends InputStream { static InputStream of(final ByteBuffer byteBuffer) { if (byteBuffer.hasArray()) { return new ByteArrayInputStream( byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.limit()); } return new ByteBufferInputStream(byteBuffer); } private  ByteBufferInputStream(final ByteBuffer byteBuffer); @Override int read(); @Override int read(final byte[] b, final int off, final int len);  }
@Test public void factoryMethodAccessesBackingArrayDirectlyIfAvailable(){ final InputStream inputStream = ByteBufferInputStream.of(ByteBuffer.wrap(new byte[16])); Assertions.assertThat(inputStream).isInstanceOf(ByteArrayInputStream.class); }