commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mbe...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/java/org/apache/commons/httpclient ContentLengthInputStream.java
Date Mon, 09 Aug 2004 01:22:05 GMT
mbecke      2004/08/08 18:22:05

  Modified:    httpclient/src/test/org/apache/commons/httpclient Tag:
                        HTTPCLIENT_2_0_BRANCH TestStreams.java
               httpclient/src/java/org/apache/commons/httpclient Tag:
                        HTTPCLIENT_2_0_BRANCH ContentLengthInputStream.java
  Log:
  Adds ContentLengthInputStream.skip().
  
  PR: 30433
  Submitted by: Michael Becke
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.11.2.2  +20 -3     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java
  
  Index: TestStreams.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestStreams.java,v
  retrieving revision 1.11.2.1
  retrieving revision 1.11.2.2
  diff -u -r1.11.2.1 -r1.11.2.2
  --- TestStreams.java	22 Feb 2004 18:21:16 -0000	1.11.2.1
  +++ TestStreams.java	9 Aug 2004 01:22:05 -0000	1.11.2.2
  @@ -131,6 +131,23 @@
           assertEquals(result, "1234567890");
       }
   
  +    public void testContentLengthInputStreamSkip() throws IOException {
  +        InputStream in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]),
10);
  +        assertEquals(10, in.skip(10));
  +        assertTrue(in.read() == -1);
  +
  +        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 10);
  +        in.read();
  +        assertEquals(9, in.skip(10));
  +        assertTrue(in.read() == -1);
  +
  +        in = new ContentLengthInputStream(new ByteArrayInputStream(new byte[20]), 2);
  +        in.read();
  +        in.read();
  +        assertTrue(in.skip(10) <= 0);
  +        assertTrue(in.read() == -1);
  +    }
  +
       public void testChunkedConsitance() throws IOException {
           String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
           ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  
  
  
  No                   revision
  No                   revision
  1.6.2.2   +25 -4     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.1
  retrieving revision 1.6.2.2
  diff -u -r1.6.2.1 -r1.6.2.2
  --- ContentLengthInputStream.java	22 Feb 2004 18:21:13 -0000	1.6.2.1
  +++ ContentLengthInputStream.java	9 Aug 2004 01:22:05 -0000	1.6.2.2
  @@ -38,7 +38,7 @@
   /**
    * Cuts the wrapped InputStream off after a specified number of bytes.
    *
  - * @author Ortwin Glück
  + * @author Ortwin Gl?ck
    * @author Eric Johnson
    * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
    * @since 2.0
  @@ -149,4 +149,25 @@
           return read(b, 0, b.length);
       }
   
  +    /**
  +     * Skips and discards a number of bytes from the input stream.
  +     * @param n The number of bytes to skip.
  +     * @return The actual number of bytes skipped. <= 0 if no bytes
  +     * are skipped.
  +     * @throws IOException If an error occurs while skipping bytes.
  +     * @see InputStream#skip(long)
  +     */
  +    public long skip(long n) throws IOException {
  +        // make sure we don't skip more bytes than are 
  +        // still available
  +        long length = Math.min(n, contentLength - pos);
  +        // skip and keep track of the bytes actually skipped
  +        length = super.skip(length);
  +        // only add the skipped bytes to the current position
  +        // if bytes were actually skipped
  +        if (length > 0) {
  +            pos += length;
  +        }
  +        return length;
  +    }
   }
  
  
  

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


Mime
View raw message