Skip to content

Commit a9aa579

Browse files
author
Stephen Colebourne
committed
Fix bug where count inaccurate after End of File
bug 33336, from Marcelo Liberato git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@154514 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1ccd298 commit a9aa579

3 files changed

Lines changed: 84 additions & 8 deletions

File tree

project.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,9 @@
158158
<contributor>
159159
<name>Jason Anderson</name>
160160
</contributor>
161+
<contributor>
162+
<name>Marcelo Liberato</name>
163+
</contributor>
161164
<contributor>
162165
<name>Alban Peignier</name>
163166
<email>alban.peignier at free.fr</email>

src/java/org/apache/commons/io/input/CountingInputStream.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* have passed through so far.
2424
*
2525
* @author Henri Yandell
26+
* @author Marcelo Liberato
2627
* @version $Id$
2728
*/
2829
public class CountingInputStream extends ProxyInputStream {
@@ -45,7 +46,7 @@ public CountingInputStream(InputStream in) {
4546
*/
4647
public int read(byte[] b) throws IOException {
4748
int found = super.read(b);
48-
this.count += found;
49+
this.count += (found >= 0) ? found : 0;
4950
return found;
5051
}
5152

@@ -56,18 +57,19 @@ public int read(byte[] b) throws IOException {
5657
*/
5758
public int read(byte[] b, int off, int len) throws IOException {
5859
int found = super.read(b, off, len);
59-
this.count += found;
60+
this.count += (found >= 0) ? found : 0;
6061
return found;
6162
}
6263

6364
/**
64-
* Increases the count by 1.
65+
* Increases the count by 1 if a byte is successfully read.
6566
*
6667
* @see java.io.InputStream#read()
6768
*/
6869
public int read() throws IOException {
69-
this.count++;
70-
return super.read();
70+
int found = super.read();
71+
this.count += (found >= 0) ? 1 : 0;
72+
return found;
7173
}
7274

7375
/**

src/test/org/apache/commons/io/input/CountingInputStreamTest.java

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2003,2004 The Apache Software Foundation.
2+
* Copyright 2003-2005 The Apache Software Foundation.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,7 +23,10 @@
2323
/**
2424
* Tests the CountingInputStream.
2525
*
26-
* @author <a href="mailto:bayard@apache.org">Henri Yandell</a>
26+
* @author Henri Yandell
27+
* @author Marcelo Liberato
28+
* @author Stephen Colebourne
29+
* @version $Id$
2730
*/
2831
public class CountingInputStreamTest extends TestCase {
2932

@@ -78,5 +81,73 @@ public void testResetting() throws Exception {
7881
found = cis.read(result, 6, 5);
7982
assertEquals( found, cis.getCount() );
8083
}
81-
}
84+
85+
public void testZeroLength1() throws Exception {
86+
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
87+
CountingInputStream cis = new CountingInputStream(bais);
88+
89+
int found = cis.read();
90+
assertEquals(-1, found);
91+
assertEquals(0, cis.getCount());
92+
}
93+
94+
public void testZeroLength2() throws Exception {
95+
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
96+
CountingInputStream cis = new CountingInputStream(bais);
97+
98+
byte[] result = new byte[10];
99+
100+
int found = cis.read(result);
101+
assertEquals(-1, found);
102+
assertEquals(0, cis.getCount());
103+
}
104+
105+
public void testZeroLength3() throws Exception {
106+
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]);
107+
CountingInputStream cis = new CountingInputStream(bais);
108+
109+
byte[] result = new byte[10];
82110

111+
int found = cis.read(result, 0, 5);
112+
assertEquals(-1, found);
113+
assertEquals(0, cis.getCount());
114+
}
115+
116+
public void testEOF1() throws Exception {
117+
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
118+
CountingInputStream cis = new CountingInputStream(bais);
119+
120+
int found = cis.read();
121+
assertEquals(0, found);
122+
assertEquals(1, cis.getCount());
123+
found = cis.read();
124+
assertEquals(0, found);
125+
assertEquals(2, cis.getCount());
126+
found = cis.read();
127+
assertEquals(-1, found);
128+
assertEquals(2, cis.getCount());
129+
}
130+
131+
public void testEOF2() throws Exception {
132+
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
133+
CountingInputStream cis = new CountingInputStream(bais);
134+
135+
byte[] result = new byte[10];
136+
137+
int found = cis.read(result);
138+
assertEquals(2, found);
139+
assertEquals(2, cis.getCount());
140+
}
141+
142+
public void testEOF3() throws Exception {
143+
ByteArrayInputStream bais = new ByteArrayInputStream(new byte[2]);
144+
CountingInputStream cis = new CountingInputStream(bais);
145+
146+
byte[] result = new byte[10];
147+
148+
int found = cis.read(result, 0, 5);
149+
assertEquals(2, found);
150+
assertEquals(2, cis.getCount());
151+
}
152+
153+
}

0 commit comments

Comments
 (0)