@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]); } }
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(); } }
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(); } } }
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(); }
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); }
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);  }