Hi,

I recently replaced the use of my own CountingInputStream with the commons-io and got burnt because io's CountingInputStream doesn't count skipped bytes. I have a patch with fix and patch if people are interested... or I could just commit it...

Thoughts?

Rob


Index: src/test/org/apache/commons/io/input/CountingInputStreamTest.java
===================================================================
--- src/test/org/apache/commons/io/input/CountingInputStreamTest.java   
(revision 160052)
+++ src/test/org/apache/commons/io/input/CountingInputStreamTest.java   
(working copy)
@@ -17,6 +17,8 @@
 package org.apache.commons.io.input;
 
 import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.Arrays;
 
 import junit.framework.TestCase;
 
@@ -149,5 +151,20 @@
         assertEquals(2, found);
         assertEquals(2, cis.getCount());
     }
+    
+    public void testSkipping() throws IOException {
+        String text = "Hello World!";
+        byte[] bytes = text.getBytes();
+        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+        CountingInputStream cis = new CountingInputStream(bais);
+        
+        assertEquals(6,cis.skip(6));
+        assertEquals(6,cis.getCount());
+        final byte[] result = new byte[6];
+        cis.read(result);
+        
+        assertEquals("World!",new String(result));
+        assertEquals(12,cis.getCount());
+    }
 
 }
Index: src/java/org/apache/commons/io/input/CountingInputStream.java
===================================================================
--- src/java/org/apache/commons/io/input/CountingInputStream.java       
(revision 160052)
+++ src/java/org/apache/commons/io/input/CountingInputStream.java       
(working copy)
@@ -71,6 +71,17 @@
         this.count += (found >= 0) ? 1 : 0;
         return found;
     }
+    
+    /**
+     * Increases the count by the number of skipped bytes.
+     * 
+     * @see java.io.InputStream#skip(long)
+     */
+    public long skip(final long length) throws IOException {
+        final long skip = super.skip(length);
+        this.count += skip;
+        return skip;
+    }
 
     /**
      * The number of bytes that have passed through this stream.

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to