You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2004/10/05 00:04:34 UTC

cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient ContentLengthInputStream.java

olegk       2004/10/04 15:04:34

  Modified:    httpclient/src/java/org/apache/commons/httpclient Tag:
                        HTTPCLIENT_2_0_BRANCH 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
  No                   revision
  No                   revision
  1.6.2.3   +30 -9     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.6.2.2
  retrieving revision 1.6.2.3
  diff -u -r1.6.2.2 -r1.6.2.3
  --- ContentLengthInputStream.java	9 Aug 2004 01:22:05 -0000	1.6.2.2
  +++ ContentLengthInputStream.java	4 Oct 2004 22:04:34 -0000	1.6.2.3
  @@ -31,20 +31,36 @@
   
   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.
    *
  + * <p>Implementation note: Choices abound. One approach would pass
  + * through the {@link InputStream#mark} and {@link 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 {@link 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 {@link InputStream#mark} and {@link InputStream#reset}.  That choice
  + * has the added benefit of keeping this class very simple.</p>
  + *
    * @author Ortwin Gl?ck
    * @author Eric Johnson
    * @author <a href="mailto:mbowler@GargoyleSoftware.com">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
  @@ -59,6 +75,11 @@
       private boolean closed = false;
   
       /**
  +     * Wrapped input stream that all calls are delegated to.
  +     */
  +    private InputStream wrappedStream;
  +
  +    /**
        * Creates a new length limited stream
        *
        * @param in The stream to wrap
  @@ -66,7 +87,7 @@
        * the stream. Subsequent read operations will return -1.
        */
       public ContentLengthInputStream(InputStream in, int contentLength) {
  -        super(in);
  +        wrappedStream = in;
           this.contentLength = contentLength;
       }
   
  @@ -105,7 +126,7 @@
               return -1;
           }
           pos++;
  -        return super.read();
  +        return wrappedStream.read();
       }
   
       /**
  @@ -132,7 +153,7 @@
           if (pos + len > contentLength) {
               len = contentLength - pos;
           }
  -        int count = super.read(b, off, len);
  +        int count = wrappedStream.read(b, off, len);
           pos += count;
           return count;
       }
  @@ -162,7 +183,7 @@
           // still available
           long length = Math.min(n, contentLength - pos);
           // skip and keep track of the bytes actually skipped
  -        length = super.skip(length);
  +        length = wrappedStream.skip(length);
           // only add the skipped bytes to the current position
           // if bytes were actually skipped
           if (length > 0) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org