EntityUtils { private static String toString(final HttpEntity entity, final ContentType contentType, final int maxResultLength) 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; } Charset charset = null; if (contentType != null) { charset = contentType.getCharset(); if (charset == null) { final ContentType defaultContentType = CONTENT_TYPE_MAP.get(contentType.getMimeType()); charset = defaultContentType != null ? defaultContentType.getCharset() : null; } } return toCharArrayBuffer(inStream, contentLength, charset, maxResultLength).toString(); } } 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 public void testNullEntityToString() throws Exception { try { EntityUtils.toString(null); Assert.fail("NullPointerException should have been thrown"); } catch (final NullPointerException ex) { } }
@Test(expected = IllegalArgumentException.class) public void testMaxIntContentToString() 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.toString(entity); }
@Test public void testUnknownLengthContentToString() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), -1, null); final String s = EntityUtils.toString(entity, "ISO-8859-1"); Assert.assertEquals("Message content", s); }
@Test public void testKnownLengthContentToString() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), bytes.length, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1)); final String s = EntityUtils.toString(entity, StandardCharsets.ISO_8859_1); Assert.assertEquals("Message content", s); }
@Test public void testNoCharsetContentToString() throws Exception { final String content = constructString(SWISS_GERMAN_HELLO); final byte[] bytes = content.getBytes(StandardCharsets.ISO_8859_1); final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), ContentType.TEXT_PLAIN); final String s = EntityUtils.toString(entity); }
@Test public void testContentWithContentTypeToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes(StandardCharsets.UTF_8); final BasicHttpEntity entity = new BasicHttpEntity(new ByteArrayInputStream(bytes), ContentType.TEXT_PLAIN.withCharset(StandardCharsets.UTF_8)); final String s = EntityUtils.toString(entity, "ISO-8859-1"); Assert.assertEquals(content, s); }
@Test public void testContentWithInvalidContentTypeToString() throws Exception { final String content = constructString(RUSSIAN_HELLO); final byte[] bytes = content.getBytes(StandardCharsets.UTF_8); final HttpEntity entity = new AbstractHttpEntity("text/plain; charset=nosuchcharset", null) { @Override public InputStream getContent() throws IOException, UnsupportedOperationException { return new ByteArrayInputStream(bytes); } @Override public boolean isStreaming() { return false; } @Override public long getContentLength() { return bytes.length; } @Override public void close() throws IOException { } }; final String s = EntityUtils.toString(entity, "UTF-8"); Assert.assertEquals(content, s); }