public void testWriteTo() throws Exception {
    String s = "Message content";
    byte[] bytes = s.getBytes(HTTP.ISO_8859_1);
    StringEntity httpentity = new StringEntity(s);
    HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    wrapped.writeTo(out);
    byte[] bytes2 = out.toByteArray();
    assertNotNull(bytes2);
    assertEquals(bytes.length, bytes2.length);
    for (int i = 0; i < bytes.length; i++) {
      assertEquals(bytes[i], bytes2[i]);
    }

    out = new ByteArrayOutputStream();
    wrapped.writeTo(out);
    bytes2 = out.toByteArray();
    assertNotNull(bytes2);
    assertEquals(bytes.length, bytes2.length);
    for (int i = 0; i < bytes.length; i++) {
      assertEquals(bytes[i], bytes2[i]);
    }

    try {
      wrapped.writeTo(null);
      fail("IllegalArgumentException should have been thrown");
    } catch (IllegalArgumentException ex) {
      // expected
    }
  }
 @Override
 public void writeTo(final OutputStream outstream) throws IOException {
   Args.notNull(outstream, "Output stream");
   if (this.buffer != null) {
     outstream.write(this.buffer);
   } else {
     super.writeTo(outstream);
   }
 }
  public void testBasics() throws Exception {
    String s = "Message content";
    StringEntity httpentity = new StringEntity(s, HTTP.ISO_8859_1);
    httpentity.setContentType(HTTP.PLAIN_TEXT_TYPE);
    httpentity.setContentEncoding(HTTP.IDENTITY_CODING);
    HttpEntityWrapper wrapped = new HttpEntityWrapper(httpentity);

    assertEquals(httpentity.getContentLength(), wrapped.getContentLength());
    assertEquals(httpentity.getContentType(), wrapped.getContentType());
    assertEquals(httpentity.getContentEncoding(), wrapped.getContentEncoding());
    assertEquals(httpentity.isChunked(), wrapped.isChunked());
    assertEquals(httpentity.isRepeatable(), wrapped.isRepeatable());
    assertEquals(httpentity.isStreaming(), wrapped.isStreaming());
    assertNotNull(wrapped.getContent());
  }