You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2013/01/18 11:53:59 UTC
svn commit: r1435081 - in /httpcomponents/httpcore/trunk: RELEASE_NOTES.txt
httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java
httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java
Author: olegk
Date: Fri Jan 18 10:53:59 2013
New Revision: 1435081
URL: http://svn.apache.org/viewvc?rev=1435081&view=rev
Log:
HTTPCORE-330: Clarify InputStreamEntity length constructor argument
Contributed by John McCarthy <jmsignup at gmail.com>
Modified:
httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java
httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java
Modified: httpcomponents/httpcore/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/RELEASE_NOTES.txt?rev=1435081&r1=1435080&r2=1435081&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpcore/trunk/RELEASE_NOTES.txt Fri Jan 18 10:53:59 2013
@@ -1,17 +1,13 @@
-Release 4.3-ALPHA2
+Changes since 4.3-ALPHA1
-------------------
-This is the second release from the 4.3.x release branch. The main theme of the 4.3 release
-series is streamlining of component configuration and deprecation of the old configuration
-API based on HttpParams in favor of constructor-based dependency injection and plain objects
-for configuration parameters.
+* [HTTPCORE-330] Clarify InputStreamEntity length constructor argument.
+ Contributed by John McCarthy <jmsignup at gmail.com>
-We are kindly asking all upstream projects to review API changes and help us improve
-the APIs by providing feedback and sharing ideas on dev@hc.apache.org.
+* [HTTPCORE-323] Undocumented UnsupportedCharsetException in ContentType#getOrDefault.
+ Contributed by Gary D. Gregory <ggregory at apache.org>
-This release also includes all fixes from the stable 4.2.x release branch.
-* [HTTPCORE-323] Undocumented UnsupportedCharsetException in org.apache.http.entity.ContentType.getOrDefault.
Release 4.3-ALPHA1
-------------------
Modified: httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java?rev=1435081&r1=1435080&r2=1435081&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/main/java/org/apache/http/entity/InputStreamEntity.java Fri Jan 18 10:53:59 2013
@@ -48,11 +48,47 @@ public class InputStreamEntity extends A
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 @@ public class InputStreamEntity extends A
return false;
}
+ /**
+ * @return the content length or {@code -1} if unknown
+ */
public long getContentLength() {
return this.length;
}
@@ -76,6 +115,13 @@ public class InputStreamEntity extends A
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;
Modified: httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java?rev=1435081&r1=1435080&r2=1435081&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java (original)
+++ httpcomponents/httpcore/trunk/httpcore/src/test/java/org/apache/http/entity/TestInputStreamEntity.java Fri Jan 18 10:53:59 2013
@@ -54,52 +54,74 @@ public class TestInputStreamEntity {
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);
-
- instream = new ByteArrayInputStream(bytes);
- httpentity = new InputStreamEntity(instream, 20);
- out = new ByteArrayOutputStream();
+ final byte[] writtenBytes = out.toByteArray();
+ Assert.assertNotNull(writtenBytes);
+ Assert.assertEquals(bytes.length, writtenBytes.length);
+
+ 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);
-
- instream = new ByteArrayInputStream(bytes);
- httpentity = new InputStreamEntity(instream, -1);
- out = new ByteArrayOutputStream();
+ final byte[] writtenBytes = out.toByteArray();
+ Assert.assertNotNull(writtenBytes);
+ Assert.assertEquals(contentLength, writtenBytes.length);
+
+ 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);
-
- try {
- httpentity.writeTo(null);
- Assert.fail("IllegalArgumentException should have been thrown");
- } catch (final IllegalArgumentException ex) {
- // expected
- }
+ final byte[] writtenBytes = out.toByteArray();
+ Assert.assertNotNull(writtenBytes);
+ Assert.assertEquals(bytes.length, writtenBytes.length);
+
+ 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);
+ }
}