Skip to content

Commit cd2e523

Browse files
committed
BoundedInputStream.getCount() should not count EOF
Add BoundedInputStream.getRemaining()
1 parent 0bcfe53 commit cd2e523

3 files changed

Lines changed: 58 additions & 9 deletions

File tree

src/changes/changes.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,16 @@ The <action> type attribute can be add,update,fix,remove.
7474
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in HiddenFileFilter.accept(Path, BasicFileAttributes) on null input.</action>
7575
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in IOCase.checkIndexOf(String, int, String) on null input.</action>
7676
<action dev="ggregory" type="fix" due-to="Gary Gregory">Avoid NullPointerException in IOCase.checkRegionMatches(String, int, String) on null input.</action>
77+
<action dev="ggregory" type="fix" due-to="Gary Gregory">BoundedInputStream.getCount() should not count EOF.</action>
7778
<!-- Add -->
78-
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, R&gt;).</action>
79-
<action dev="ggregory" type="fix" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action>
80-
<action dev="ggregory" type="fix" due-to="Gary Gregory">Make public Erase.rethrow(Throwable).</action>
81-
<action dev="ggregory" type="fix" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenInputStream.BrokenInputStream(Throwable).</action>
82-
<action dev="ggregory" type="fix" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenReader.BrokenReader(Throwable).</action>
83-
<action dev="ggregory" type="fix" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenOutputStream.BrokenOutputStream(Throwable).</action>
84-
<action dev="ggregory" type="fix" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenWriter.BrokenWriter(Throwable).</action>
79+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileName(Path, Function&lt;Path, R&gt;).</action>
80+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add and use PathUtils.getFileNameString().</action>
81+
<action dev="ggregory" type="add" due-to="Gary Gregory">Make public Erase.rethrow(Throwable).</action>
82+
<action dev="ggregory" type="add" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenInputStream.BrokenInputStream(Throwable).</action>
83+
<action dev="ggregory" type="add" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenReader.BrokenReader(Throwable).</action>
84+
<action dev="ggregory" type="add" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenOutputStream.BrokenOutputStream(Throwable).</action>
85+
<action dev="ggregory" type="add" issue="IO-826" due-to="markslater, Gary Gregory">Add BrokenWriter.BrokenWriter(Throwable).</action>
86+
<action dev="ggregory" type="add" due-to="Gary Gregory">Add BoundedInputStream.getRemaining().</action>
8587
<!-- UPDATE -->
8688
<action dev="ggregory" type="fix" due-to="Gary Gregory">Bump commons.bytebuddy.version from 1.14.10 to 1.14.11 #534.</action>
8789
</release>

src/main/java/org/apache/commons/io/input/BoundedInputStream.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ public long getCount() {
105105
return count;
106106
}
107107

108+
/**
109+
* Gets how many bytes remain to read.
110+
*
111+
* @return bytes how many bytes remain to read.
112+
* @since 2.16.0
113+
*/
114+
public long getRemaining() {
115+
return getMaxLength() - getCount();
116+
}
117+
108118
/**
109119
* Gets the max count of bytes to read.
110120
*
@@ -160,6 +170,7 @@ public boolean markSupported() {
160170
* @throws IOException Subclasses may throw.
161171
* @since 2.12.0
162172
*/
173+
@SuppressWarnings("unused")
163174
protected void onMaxLength(final long maxLength, final long count) throws IOException {
164175
// for subclasses
165176
}
@@ -179,7 +190,9 @@ public int read() throws IOException {
179190
return EOF;
180191
}
181192
final int result = in.read();
182-
count++;
193+
if (result != EOF) {
194+
count++;
195+
}
183196
return result;
184197
}
185198

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,26 +52,49 @@ protected void onMaxLength(final long max, final long readCount) {
5252
boolRef.set(true);
5353
}
5454
};
55+
assertEquals(helloWorld.length, bounded.getMaxLength());
56+
assertEquals(0, bounded.getCount());
57+
assertEquals(bounded.getMaxLength(), bounded.getRemaining());
5558
assertFalse(boolRef.get());
59+
int readCount = 0;
5660
for (int i = 0; i < helloWorld.length; i++) {
5761
assertEquals(helloWorld[i], bounded.read(), "limit = length byte[" + i + "]");
62+
readCount++;
63+
assertEquals(helloWorld.length, bounded.getMaxLength());
64+
assertEquals(readCount, bounded.getCount());
65+
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
5866
}
5967
assertEquals(-1, bounded.read(), "limit = length end");
68+
assertEquals(helloWorld.length, bounded.getMaxLength());
69+
assertEquals(readCount, bounded.getCount());
70+
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
6071
assertTrue(boolRef.get());
6172

6273
// limit > length
6374
boolRef.set(false);
64-
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), helloWorld.length + 1) {
75+
final int length2 = helloWorld.length + 1;
76+
bounded = new BoundedInputStream(new ByteArrayInputStream(helloWorld), length2) {
6577
@Override
6678
protected void onMaxLength(final long max, final long readCount) {
6779
boolRef.set(true);
6880
}
6981
};
82+
assertEquals(length2, bounded.getMaxLength());
83+
assertEquals(0, bounded.getCount());
84+
assertEquals(bounded.getMaxLength(), bounded.getRemaining());
7085
assertFalse(boolRef.get());
86+
readCount = 0;
7187
for (int i = 0; i < helloWorld.length; i++) {
7288
assertEquals(helloWorld[i], bounded.read(), "limit > length byte[" + i + "]");
89+
readCount++;
90+
assertEquals(length2, bounded.getMaxLength());
91+
assertEquals(readCount, bounded.getCount());
92+
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
7393
}
7494
assertEquals(-1, bounded.read(), "limit > length end");
95+
assertEquals(length2, bounded.getMaxLength());
96+
assertEquals(readCount, bounded.getCount());
97+
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
7598
assertFalse(boolRef.get());
7699

77100
// limit < length
@@ -82,11 +105,22 @@ protected void onMaxLength(final long max, final long readCount) {
82105
boolRef.set(true);
83106
}
84107
};
108+
assertEquals(hello.length, bounded.getMaxLength());
109+
assertEquals(0, bounded.getCount());
110+
assertEquals(bounded.getMaxLength(), bounded.getRemaining());
85111
assertFalse(boolRef.get());
112+
readCount = 0;
86113
for (int i = 0; i < hello.length; i++) {
87114
assertEquals(hello[i], bounded.read(), "limit < length byte[" + i + "]");
115+
readCount++;
116+
assertEquals(hello.length, bounded.getMaxLength());
117+
assertEquals(readCount, bounded.getCount());
118+
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
88119
}
89120
assertEquals(-1, bounded.read(), "limit < length end");
121+
assertEquals(hello.length, bounded.getMaxLength());
122+
assertEquals(readCount, bounded.getCount());
123+
assertEquals(bounded.getMaxLength() - readCount, bounded.getRemaining());
90124
assertTrue(boolRef.get());
91125
}
92126

0 commit comments

Comments
 (0)