mbecke      2004/08/08 18:25:55

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestStreams.java
               httpclient/src/java/org/apache/commons/httpclient
                        ContentLengthInputStream.java
  Log:
  Adds ContentLengthInputStream.skip().
  
  PR: 30433
  Submitted by: Michael Becke
  
  Revision  Changes    Path
  1.17      +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.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- TestStreams.java  13 Jun 2004 20:22:19 -0000      1.16
  +++ TestStreams.java  9 Aug 2004 01:25:54 -0000       1.17
  @@ -138,6 +138,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();
  
  
  
  1.11      +24 -3     
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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ContentLengthInputStream.java     13 May 2004 04:03:25 -0000      1.10
  +++ ContentLengthInputStream.java     9 Aug 2004 01:25:54 -0000       1.11
  @@ -162,4 +162,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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to