olegk 2004/10/04 15:05:44
Modified: httpclient/src/java/org/apache/commons/httpclient
ContentLengthInputStream.java
Log:
ContentLengthInputStream no longer supports mark() & reset() methods. Old broken
implementation removed.
Contributed by Eric Johnson <eric at tibco.com>
Reviewed by Oleg Kalnichevski
Revision Changes Path
1.12 +33 -12
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java
Index: ContentLengthInputStream.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/ContentLengthInputStream.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ContentLengthInputStream.java 9 Aug 2004 01:25:54 -0000 1.11
+++ ContentLengthInputStream.java 4 Oct 2004 22:05:44 -0000 1.12
@@ -29,19 +29,35 @@
package org.apache.commons.httpclient;
-import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Cuts the wrapped InputStream off after a specified number of bytes.
*
- * @author Ortwin Gl???ck
+ * <p>Implementation note: Choices abound. One approach would pass
+ * through the [EMAIL PROTECTED] InputStream#mark} and [EMAIL PROTECTED]
InputStream#reset} calls to
+ * the underlying stream. That's tricky, though, because you then have to
+ * start duplicating the work of keeping track of how much a reset rewinds.
+ * Further, you have to watch out for the "readLimit", and since the semantics
+ * for the readLimit leave room for differing implementations, you might get
+ * into a lot of trouble.</p>
+ *
+ * <p>Alternatively, you could make this class extend [EMAIL PROTECTED]
java.io.BufferedInputStream}
+ * and then use the protected members of that class to avoid duplicated effort.
+ * That solution has the side effect of adding yet another possible layer of
+ * buffering.</p>
+ *
+ * <p>Then, there is the simple choice, which this takes - simply don't
+ * support [EMAIL PROTECTED] InputStream#mark} and [EMAIL PROTECTED]
InputStream#reset}. That choice
+ * has the added benefit of keeping this class very simple.</p>
+ *
+ * @author Ortwin Glueck
* @author Eric Johnson
* @author <a href="mailto:[EMAIL PROTECTED]">Mike Bowler</a>
* @since 2.0
*/
-public class ContentLengthInputStream extends FilterInputStream {
+public class ContentLengthInputStream extends InputStream {
/**
* The maximum number of bytes that can be read from the stream. Subsequent
@@ -56,6 +72,11 @@
private boolean closed = false;
/**
+ * Wrapped input stream that all calls are delegated to.
+ */
+ private InputStream wrappedStream = null;
+
+ /**
* @deprecated use [EMAIL PROTECTED] #ContentLengthInputStream(InputStream,
long)}
*
* Creates a new length limited stream
@@ -65,8 +86,7 @@
* the stream. Subsequent read operations will return -1.
*/
public ContentLengthInputStream(InputStream in, int contentLength) {
- super(in);
- this.contentLength = contentLength;
+ this(in, (long)contentLength);
}
/**
@@ -79,7 +99,8 @@
* @since 3.0
*/
public ContentLengthInputStream(InputStream in, long contentLength) {
- super(in);
+ super();
+ this.wrappedStream = in;
this.contentLength = contentLength;
}
@@ -118,7 +139,7 @@
return -1;
}
pos++;
- return super.read();
+ return this.wrappedStream.read();
}
/**
@@ -145,7 +166,7 @@
if (pos + len > contentLength) {
len = (int) (contentLength - pos);
}
- int count = super.read(b, off, len);
+ int count = this.wrappedStream.read(b, off, len);
pos += count;
return count;
}
@@ -175,7 +196,7 @@
// still available
long length = Math.min(n, contentLength - pos);
// skip and keep track of the bytes actually skipped
- length = super.skip(length);
+ length = this.wrappedStream.skip(length);
// only add the skipped bytes to the current position
// if bytes were actually skipped
if (length > 0) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]