Index: httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java =================================================================== --- httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java (revision 1434730) +++ httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java (working copy) @@ -48,11 +48,47 @@ private final InputStream content; private final long length; + /** + * Creates an entity with an unknown length. + * Equivalent to {@code new InputStreamEntity(instream, -1)}. + * + * @param instream + * @throws IllegalArgumentException if {@code instream} is {@code null} + * @since 4.4 + */ + public InputStreamEntity(final InputStream instream) { + this(instream, -1); + } + + /** + * Creates an entity with a specified content length. + * + * @param instream + * @param length of the input stream, {@code -1} if unknown + * @throws IllegalArgumentException if {@code instream} is {@code null} + */ public InputStreamEntity(final InputStream instream, final long length) { this(instream, length, null); } /** + * Creates an entity with a content type and unknown length. + * Equivalent to {@code new InputStreamEntity(instream, -1, contentType)}. + * + * @param instream + * @param contentType + * @throws IllegalArgumentException if {@code instream} is {@code null} + * @since 4.4 + */ + public InputStreamEntity(final InputStream instream, final ContentType contentType) { + this(instream, -1, contentType); + } + + /** + * @param instream + * @param length of the input stream, {@code -1} if unknown + * @param contentType for specifying the {@code Content-Type} header, may be {@code null} + * @throws IllegalArgumentException if {@code instream} is {@code null} * @since 4.2 */ public InputStreamEntity(final InputStream instream, final long length, final ContentType contentType) { @@ -68,6 +104,9 @@ return false; } + /** + * @return the content length or {@code -1} if unknown + */ public long getContentLength() { return this.length; } @@ -76,6 +115,13 @@ return this.content; } + /** + * Writes bytes from the {@code InputStream} this entity was constructed + * with to an {@code OutputStream}. The content length + * determines how many bytes are written. If the length is unknown ({@code -1}), the + * stream will be completely consumed (to the end of the stream). + * + */ public void writeTo(final OutputStream outstream) throws IOException { Args.notNull(outstream, "Output stream"); final InputStream instream = this.content; Index: httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java =================================================================== --- httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java (revision 1434730) +++ httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java (working copy) @@ -54,52 +54,74 @@ Assert.assertTrue(httpentity.isStreaming()); } - @Test + @Test(expected = IllegalArgumentException.class) public void testIllegalConstructor() throws Exception { - try { - new InputStreamEntity(null, 0); - Assert.fail("IllegalArgumentException should have been thrown"); - } catch (final IllegalArgumentException ex) { - // expected - } + new InputStreamEntity(null, 0); } @Test + public void testUnknownLengthConstructor() throws Exception { + InputStream instream = new ByteArrayInputStream(new byte[0]); + InputStreamEntity httpentity = new InputStreamEntity(instream); + Assert.assertEquals(-1, httpentity.getContentLength()); + } + + @Test public void testWriteTo() throws Exception { - final byte[] bytes = "Message content".getBytes(Consts.ISO_8859_1.name()); + final String message = "Message content"; + final byte[] bytes = message.getBytes(Consts.ISO_8859_1.name()); InputStream instream = new ByteArrayInputStream(bytes); - InputStreamEntity httpentity = new InputStreamEntity(instream, 7); + InputStreamEntity httpentity = new InputStreamEntity(instream, bytes.length); ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); - byte[] bytes2 = out.toByteArray(); - Assert.assertNotNull(bytes2); - Assert.assertEquals(7, bytes2.length); - final String s = new String(bytes2, Consts.ISO_8859_1.name()); - Assert.assertEquals("Message", s); + final byte[] writtenBytes = out.toByteArray(); + Assert.assertNotNull(writtenBytes); + Assert.assertEquals(bytes.length, writtenBytes.length); - instream = new ByteArrayInputStream(bytes); - httpentity = new InputStreamEntity(instream, 20); - out = new ByteArrayOutputStream(); + final String s = new String(writtenBytes, Consts.ISO_8859_1.name()); + Assert.assertEquals(message, s); + } + + @Test + public void testWriteToPartialContent() throws Exception { + final String message = "Message content"; + final byte[] bytes = message.getBytes(Consts.ISO_8859_1.name()); + InputStream instream = new ByteArrayInputStream(bytes); + final int contentLength = 7; + InputStreamEntity httpentity = new InputStreamEntity(instream, contentLength); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); - bytes2 = out.toByteArray(); - Assert.assertNotNull(bytes2); - Assert.assertEquals(bytes.length, bytes2.length); + final byte[] writtenBytes = out.toByteArray(); + Assert.assertNotNull(writtenBytes); + Assert.assertEquals(contentLength, writtenBytes.length); - instream = new ByteArrayInputStream(bytes); - httpentity = new InputStreamEntity(instream, -1); - out = new ByteArrayOutputStream(); + final String s = new String(writtenBytes, Consts.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(Consts.ISO_8859_1.name()); + InputStream instream = new ByteArrayInputStream(bytes); + InputStreamEntity httpentity = new InputStreamEntity(instream); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); httpentity.writeTo(out); - bytes2 = out.toByteArray(); - Assert.assertNotNull(bytes2); - Assert.assertEquals(bytes.length, bytes2.length); + final byte[] writtenBytes = out.toByteArray(); + Assert.assertNotNull(writtenBytes); + Assert.assertEquals(bytes.length, writtenBytes.length); - try { - httpentity.writeTo(null); - Assert.fail("IllegalArgumentException should have been thrown"); - } catch (final IllegalArgumentException ex) { - // expected - } + final String s = new String(writtenBytes, Consts.ISO_8859_1.name()); + Assert.assertEquals(message, s); } + @Test(expected = IllegalArgumentException.class) + public void testWriteToNull() throws Exception { + InputStream instream = new ByteArrayInputStream(new byte[0]); + InputStreamEntity httpentity = new InputStreamEntity(instream, 0); + httpentity.writeTo(null); + } }