@Override public Content handleEntity(final HttpEntity entity) throws IOException { return entity != null ? new Content(EntityUtils.toByteArray(entity), EntityUtils.getContentTypeOrDefault(entity)) : Content.NO_CONTENT; }
data = new byte [0]; } else { data = EntityUtils.toByteArray(entity);
public HttpResponse returnResponse() throws IOException { assertNotConsumed(); try { final HttpEntity entity = this.response.getEntity(); if (entity != null) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity( EntityUtils.toByteArray(entity)); final ContentType contentType = EntityUtils.getContentTypeOrDefault(entity); byteArrayEntity.setContentType(contentType.toString()); this.response.setEntity(byteArrayEntity); } return this.response; } finally { this.consumed = true; } }
@Test public void testCombinedEntityBasics() throws Exception { final HttpEntity httpEntity = mock(HttpEntity.class); when(httpEntity.getContent()).thenReturn( new ByteArrayInputStream(new byte[] { 6, 7, 8, 9, 10 })); final ByteArrayBuffer buf = new ByteArrayBuffer(1024); final byte[] tmp = new byte[] { 1, 2, 3, 4, 5 }; buf.append(tmp, 0, tmp.length); final CombinedEntity entity = new CombinedEntity(httpEntity, buf); Assert.assertEquals(-1, entity.getContentLength()); Assert.assertFalse(entity.isRepeatable()); Assert.assertTrue(entity.isStreaming()); Assert.assertArrayEquals(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, EntityUtils.toByteArray(entity)); verify(httpEntity).getContent(); entity.close(); verify(httpEntity).close(); }
@Override public Content handleEntity(final HttpEntity entity) throws IOException { return entity != null ? new Content(EntityUtils.toByteArray(entity), EntityUtils.getContentTypeOrDefault(entity)) : Content.NO_CONTENT; }
@Test public void testNullEntityToByteArray() throws Exception { try { EntityUtils.toByteArray(null); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }
@Test public void testEmptyContentToByteArray() throws Exception { try (final NullHttpEntity httpentity = new NullHttpEntity()) { final byte[] bytes = EntityUtils.toByteArray(httpentity); Assert.assertNull(bytes); } }
@Test public void testMaxIntContentToByteArray() throws Exception { final byte[] content = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(content)); httpentity.setContentLength(Integer.MAX_VALUE + 100L); try { EntityUtils.toByteArray(httpentity); Assert.fail("IllegalArgumentException should have been thrown"); } catch (final IllegalArgumentException ex) { // expected } }}
public HttpResponse returnResponse() throws IOException { assertNotConsumed(); try { final HttpEntity entity = this.response.getEntity(); if (entity != null) { final ByteArrayEntity byteArrayEntity = new ByteArrayEntity( EntityUtils.toByteArray(entity)); final ContentType contentType = EntityUtils.getContentTypeOrDefault(entity); byteArrayEntity.setContentType(contentType.toString()); this.response.setEntity(byteArrayEntity); } return this.response; } finally { this.consumed = true; } }
@Test public void testKnownLengthContentToByteArray() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentLength(bytes.length); final byte[] bytes2 = EntityUtils.toByteArray(httpentity); 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 testUnknownLengthContentToByteArray() throws Exception { final byte[] bytes = "Message content".getBytes(StandardCharsets.ISO_8859_1); try (final BasicHttpEntity httpentity = new BasicHttpEntity()) { httpentity.setContent(new ByteArrayInputStream(bytes)); httpentity.setContentLength(-1L); final byte[] bytes2 = EntityUtils.toByteArray(httpentity); Assert.assertNotNull(bytes2); Assert.assertEquals(bytes.length, bytes2.length); for (int i = 0; i < bytes.length; i++) { Assert.assertEquals(bytes[i], bytes2[i]); } } }