Skip to content

Commit 0c7f310

Browse files
committed
Fix occasional test failure - should always return 0 for read length == 0
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1300238 13f79535-47bb-0310-9956-ffa450edef68
1 parent 3200c5f commit 0c7f310

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ public int read(byte[] b, int off, int len) throws IOException {
117117
if (len < 0 || (off + len) > b.length) {
118118
throw new IndexOutOfBoundsException("Array Size=" + b.length +
119119
", offset=" + off + ", length=" + len);
120-
}
120+
}
121+
if (len == 0) {
122+
return 0; // must return 0 for zero length read
123+
}
121124
if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) {
122125
return -1;
123126
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ public void testReadZero() throws Exception {
109109
byte[] bytes = new byte[30];
110110
assertEquals(0, r.read(bytes, 0, 0));
111111
}
112+
113+
@Test
114+
public void testReadZeroEmptyString() throws Exception {
115+
InputStream r = new CharSequenceInputStream("", "UTF-8");
116+
byte[] bytes = new byte[30];
117+
assertEquals(0, r.read(bytes, 0, 0));
118+
}
112119

113120
@Test
114121
public void testCharsetMismatchInfiniteLoop() throws IOException {

0 commit comments

Comments
 (0)