EntityUtils { public static byte[] toByteArray(final HttpEntity entity) throws IOException { Args.notNull(entity, "HttpEntity"); final int contentLength = toContentLength((int) Args.checkContentLength(entity)); try (final InputStream inStream = entity.getContent()) { if (inStream == null) { return null; } final ByteArrayBuffer buffer = new ByteArrayBuffer(contentLength); final byte[] tmp = new byte[DEFAULT_BYTE_BUFFER_SIZE]; int l; while ((l = inStream.read(tmp)) != -1) { buffer.append(tmp, 0, l); } return buffer.toByteArray(); } } private  EntityUtils(); static void consumeQuietly(final HttpEntity entity); static void consume(final HttpEntity entity); static byte[] toByteArray(final HttpEntity entity); static byte[] toByteArray(final HttpEntity entity, final int maxResultLength); static String toString(
            final HttpEntity entity, final Charset defaultCharset); static String toString(
            final HttpEntity entity, final Charset defaultCharset, final int maxResultLength); static String toString(
            final HttpEntity entity, final String defaultCharset); static String toString(
            final HttpEntity entity, final String defaultCharset, final int maxResultLength); static String toString(final HttpEntity entity); static String toString(final HttpEntity entity, final int maxResultLength); static List<NameValuePair> parse(final HttpEntity entity); static List<NameValuePair> parse(final HttpEntity entity, final int maxStreamLength);  }
@Test(expected = NullPointerException.class) public void testNullEntityToByteArray() throws Exception { EntityUtils.toByteArray(null); }
@Test(expected = IllegalArgumentException.class) public void testMaxIntContentToByteArray() throws Exception { final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1); final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(content), Integer.MAX_VALUE + 100L, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1)); EntityUtils.toByteArray(entity); }
@Test public void testUnknownLengthContentToByteArray() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), -1, null); final byte[] bytes2 = EntityUtils.toByteArray(entity); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } }
@Test public void testKnownLengthContentToByteArray() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length, null); final byte[] bytes2 = EntityUtils.toByteArray(entity); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } }
@Test public void testByteArrayMaxResultLength() throws IOException { final byte[] allBytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); final Map<Integer, byte[]> testCases = new HashMap<>(); testCases.put(0, new byte[]{}); testCases.put(2, Arrays.copyOfRange(allBytes, 0, 2)); testCases.put(allBytes.length, allBytes); testCases.put(Integer.MAX_VALUE, allBytes); for (final Map.Entry<Integer, byte[]> tc : testCases.entrySet()) { final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(allBytes), allBytes.length, null); final byte[] bytes = EntityUtils.toByteArray(entity, tc.getKey()); final byte[] expectedBytes = tc.getValue(); Assert.assertNotNull(bytes); Assert.assertEquals(expectedBytes.length, bytes.length); for (int i = 0; i < expectedBytes.length; i++) { Assert.assertEquals(expectedBytes[i], bytes[i]); } } }