@Test
public void testWriteToBuffered() throws Exception {

    final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII);
    final InputStreamEntity httpentity = new InputStreamEntity(new ByteArrayInputStream(bytes), -1, null);
    final BufferedHttpEntity bufentity = new BufferedHttpEntity(httpentity);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    bufentity.writeTo(out);
    byte[] bytes2 = out.toByteArray();
    Assert.assertNotNull(bytes2);
    Assert.assertEquals(bytes.length, bytes2.length);
    for (int i = 0; i < bytes.length; i++) {
        Assert.assertEquals(bytes[i], bytes2[i]);
    }
    out = new ByteArrayOutputStream();
    bufentity.writeTo(out);
    bytes2 = out.toByteArray();
    Assert.assertNotNull(bytes2);
    Assert.assertEquals(bytes.length, bytes2.length);
    for (int i = 0; i < bytes.length; i++) {
        Assert.assertEquals(bytes[i], bytes2[i]);
    }
    try {
        bufentity.writeTo(null);
        Assert.fail("NullPointerException should have been thrown");
    } catch (final NullPointerException ex) {
        // expected
    }
}