ByteBufferEntity extends AbstractHttpEntity implements Cloneable { @Override public void writeTo(OutputStream outstream) throws IOException { Args.notNull(outstream, "Output stream"); WritableByteChannel channel = Channels.newChannel(outstream); channel.write(buffer); outstream.flush(); } ByteBufferEntity(final ByteBuffer buffer, final ContentType contentType);  ByteBufferEntity(final ByteBuffer buffer); @Override boolean isRepeatable(); @Override long getContentLength(); @Override InputStream getContent(); @Override void writeTo(OutputStream outstream); @Override boolean isStreaming(); @Override Object clone();  }
@Test public void testWriteTo() throws Exception { final ByteBuffer bytes = ByteBuffer.wrap("Message content".getBytes(Consts.ASCII)); final ByteBufferEntity httpentity = new ByteBufferEntity(bytes); ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); byte[] bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.capacity(), bytes2.length); bytes.position(0); for (int i = 0; i < bytes2.length; i++) { Assert.assertEquals(bytes.get(i), bytes2[i]); } out = new ByteArrayOutputStream(); httpentity.writeTo(out); bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.capacity(), bytes2.length); bytes.position(0); for (int i = 0; i < bytes.capacity(); i++) { Assert.assertEquals(bytes.get(i), bytes2[i]); } try { httpentity.writeTo(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { } }