ByteArrayEntity extends AbstractHttpEntity { @Override public final void writeTo(final OutputStream outStream) throws IOException { Args.notNull(outStream, "Output stream"); outStream.write(this.b, this.off, this.len); outStream.flush(); } ByteArrayEntity(
            final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding,
            final boolean chunked);  ByteArrayEntity(
            final byte[] b, final int off, final int len, final ContentType contentType, final String contentEncoding);  ByteArrayEntity(
            final byte[] b, final ContentType contentType, final String contentEncoding, final boolean chunked);  ByteArrayEntity(final byte[] b, final ContentType contentType, final String contentEncoding);  ByteArrayEntity(final byte[] b, final ContentType contentType, final boolean chunked);  ByteArrayEntity(final byte[] b, final ContentType contentType);  ByteArrayEntity(
            final byte[] b, final int off, final int len, final ContentType contentType,  final boolean chunked);  ByteArrayEntity(final byte[] b, final int off, final int len, final ContentType contentType); @Override final boolean isRepeatable(); @Override final long getContentLength(); @Override final InputStream getContent(); @Override final void writeTo(final OutputStream outStream); @Override final boolean isStreaming(); @Override final void close();  }
@Test public void testWriteTo() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final ByteArrayEntity entity = new ByteArrayEntity(bytes, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.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(); entity.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 { entity.writeTo(null); Assert.fail("NullPointerException should have been thrown"); } catch (final NullPointerException ex) { } }
@Test public void testWriteToOffLen() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.US_ASCII); final int off = 8; final int len = 7; final ByteArrayEntity entity = new ByteArrayEntity(bytes, off, len, null); ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); byte[] bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(len, bytes2.length); for (int i = 0; i < len; i++) { Assert.assertEquals(bytes[i+off], bytes2[i]); } out = new ByteArrayOutputStream(); entity.writeTo(out); bytes2 = out.toByteArray(); Assert.assertNotNull(bytes2); Assert.assertEquals(len, bytes2.length); for (int i = 0; i < len; i++) { Assert.assertEquals(bytes[i+off], bytes2[i]); } try { entity.writeTo(null); Assert.fail("NullPointerException should have been thrown"); } catch (final NullPointerException ex) { } }