commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From scolebou...@apache.org
Subject svn commit: r154514 - in jakarta/commons/proper/io/trunk: project.xml src/java/org/apache/commons/io/input/CountingInputStream.java src/test/org/apache/commons/io/input/CountingInputStreamTest.java
Date Sun, 20 Feb 2005 15:06:40 GMT
Author: scolebourne
Date: Sun Feb 20 07:06:38 2005
New Revision: 154514

URL: http://svn.apache.org/viewcvs?view=rev&rev=154514
Log:
Fix bug where count inaccurate after End of File
bug 33336, from Marcelo Liberato

Modified:
    jakarta/commons/proper/io/trunk/project.xml
    jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/input/CountingInputStream.java
    jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/input/CountingInputStreamTest.java

Modified: jakarta/commons/proper/io/trunk/project.xml
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/project.xml?view=diff&r1=154513&r2=154514
==============================================================================
--- jakarta/commons/proper/io/trunk/project.xml (original)
+++ jakarta/commons/proper/io/trunk/project.xml Sun Feb 20 07:06:38 2005
@@ -159,6 +159,9 @@
       <name>Jason Anderson</name>
     </contributor>
     <contributor>
+      <name>Marcelo Liberato</name>
+    </contributor>
+    <contributor>
       <name>Alban Peignier</name>
       <email>alban.peignier at free.fr</email>
     </contributor>

Modified: jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/input/CountingInputStream.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/input/CountingInputStream.java?view=diff&r1=154513&r2=154514
==============================================================================
--- jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/input/CountingInputStream.java
(original)
+++ jakarta/commons/proper/io/trunk/src/java/org/apache/commons/io/input/CountingInputStream.java
Sun Feb 20 07:06:38 2005
@@ -23,6 +23,7 @@
  * have passed through so far.
  *
  * @author Henri Yandell
+ * @author Marcelo Liberato
  * @version $Id$
  */
 public class CountingInputStream extends ProxyInputStream {
@@ -45,7 +46,7 @@
      */
     public int read(byte[] b) throws IOException {
         int found = super.read(b);
-        this.count += found;
+        this.count += (found >= 0) ? found : 0;
         return found;
     }
 
@@ -56,18 +57,19 @@
      */
     public int read(byte[] b, int off, int len) throws IOException {
         int found = super.read(b, off, len);
-        this.count += found;
+        this.count += (found >= 0) ? found : 0;
         return found;
     }
 
     /**
-     * Increases the count by 1. 
+     * Increases the count by 1 if a byte is successfully read. 
      *
      * @see java.io.InputStream#read()
      */
     public int read() throws IOException {
-        this.count++;
-        return super.read();
+        int found = super.read();
+        this.count += (found >= 0) ? 1 : 0;
+        return found;
     }
 
     /**

Modified: jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/input/CountingInputStreamTest.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/input/CountingInputStreamTest.java?view=diff&r1=154513&r2=154514
==============================================================================
--- jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/input/CountingInputStreamTest.java
(original)
+++ jakarta/commons/proper/io/trunk/src/test/org/apache/commons/io/input/CountingInputStreamTest.java
Sun Feb 20 07:06:38 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003,2004 The Apache Software Foundation.
+ * Copyright 2003-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -23,7 +23,10 @@
 /**
  * Tests the CountingInputStream.
  *
- * @author <a href="mailto:bayard@apache.org">Henri Yandell</a>
+ * @author Henri Yandell
+ * @author Marcelo Liberato
+ * @author Stephen Colebourne
+ * @version $Id$
  */
 public class CountingInputStreamTest extends TestCase {
 
@@ -78,5 +81,73 @@
         found = cis.read(result, 6, 5);
         assertEquals( found, cis.getCount() );
     }
-}
+    
+    public void testZeroLength1() throws Exception {
+        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
+        CountingInputStream cis = new CountingInputStream(bais);
+
+        int found = cis.read();
+        assertEquals(-1, found);
+        assertEquals(0, cis.getCount());
+    }
+
+    public void testZeroLength2() throws Exception {
+        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
+        CountingInputStream cis = new CountingInputStream(bais);
+
+        byte[] result = new byte[10];
+
+        int found = cis.read(result);
+        assertEquals(-1, found);
+        assertEquals(0, cis.getCount());
+    }
+
+    public void testZeroLength3() throws Exception {
+        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
+        CountingInputStream cis = new CountingInputStream(bais);
+
+        byte[] result = new byte[10];
 
+        int found = cis.read(result, 0, 5);
+        assertEquals(-1, found);
+        assertEquals(0, cis.getCount());
+    }
+
+    public void testEOF1() throws Exception {
+        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
+        CountingInputStream cis = new CountingInputStream(bais);
+
+        int found = cis.read();
+        assertEquals(0, found);
+        assertEquals(1, cis.getCount());
+        found = cis.read();
+        assertEquals(0, found);
+        assertEquals(2, cis.getCount());
+        found = cis.read();
+        assertEquals(-1, found);
+        assertEquals(2, cis.getCount());
+    }
+
+    public void testEOF2() throws Exception {
+        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
+        CountingInputStream cis = new CountingInputStream(bais);
+
+        byte[] result = new byte[10];
+
+        int found = cis.read(result);
+        assertEquals(2, found);
+        assertEquals(2, cis.getCount());
+    }
+
+    public void testEOF3() throws Exception {
+        ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
+        CountingInputStream cis = new CountingInputStream(bais);
+
+        byte[] result = new byte[10];
+
+        int found = cis.read(result, 0, 5);
+        assertEquals(2, found);
+        assertEquals(2, cis.getCount());
+    }
+
+}



---------------------------------------------------------------------
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