InputStreamEntity extends AbstractHttpEntity { @Override public final void writeTo(final OutputStream outStream) throws IOException { Args.notNull(outStream, "Output stream"); try (final InputStream inStream = this.content) { final byte[] buffer = new byte[OUTPUT_BUFFER_SIZE]; int readLen; if (this.length < 0) { while ((readLen = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, readLen); } } else { long remaining = this.length; while (remaining > 0) { readLen = inStream.read(buffer, 0, (int) Math.min(OUTPUT_BUFFER_SIZE, remaining)); if (readLen == -1) { break; } outStream.write(buffer, 0, readLen); remaining -= readLen; } } } } InputStreamEntity(
            final InputStream inStream, final long length, final ContentType contentType, final String contentEncoding);  InputStreamEntity(final InputStream inStream, final long length, final ContentType contentType);  InputStreamEntity(final InputStream inStream, 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 String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final InputStreamEntity entity = new InputStreamEntity(inStream, bytes.length, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1)); final ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(bytes.length, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message, s); }
@Test public void testWriteToPartialContent() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStream inStream = new ByteArrayInputStream(bytes); final int contentLength = 7; final InputStreamEntity entity = new InputStreamEntity(inStream, contentLength, ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1)); final ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(contentLength, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message.substring(0, contentLength), s); }
@Test public void testWriteToUnknownLength() throws Exception { final String message = "Message content"; final byte[] bytes = message.getBytes(StandardCharsets.ISO_8859_1); final InputStreamEntity entity = new InputStreamEntity(new ByteArrayInputStream(bytes), ContentType.TEXT_PLAIN.withCharset(StandardCharsets.ISO_8859_1)); final ByteArrayOutputStream out = new ByteArrayOutputStream(); entity.writeTo(out); final byte[] writtenBytes = out.toByteArray(); Assert.assertNotNull(writtenBytes); Assert.assertEquals(bytes.length, writtenBytes.length); final String s = new String(writtenBytes, StandardCharsets.ISO_8859_1.name()); Assert.assertEquals(message, s); }
@Test(expected = NullPointerException.class) public void testWriteToNull() throws Exception { final InputStreamEntity entity = new InputStreamEntity(EmptyInputStream.INSTANCE, 0, null); entity.writeTo(null); }