Skip to content

Commit c4251ac

Browse files
committed
IO-306 ReaderInputStream#read(byte[] b, int off, int len) should always return 0 for length == 0
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1300249 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0c7f310 commit c4251ac

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/changes/changes.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ The <action> type attribute can be add,update,fix,remove.
4040

4141
<body>
4242
<release version="2.2" date="TBA">
43+
<action dev="sebb" type="fix" issue="IO-306">
44+
ReaderInputStream#read(byte[] b, int off, int len) should always return 0 for length == 0
45+
</action>
4346
<action dev="sebb" type="add" issue="IO-173" due-to="Marcos Vinícius da Silva">
4447
FileUtils.listFiles() doesn't return directories
4548
</action>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,9 @@ private void fillBuffer() throws IOException {
221221
@Override
222222
public int read(byte[] b, int off, int len) throws IOException {
223223
int read = 0;
224+
if (len == 0) {
225+
return 0; // Always return 0 if len == 0
226+
}
224227
while (len > 0) {
225228
if (encoderOut.hasRemaining()) {
226229
int c = Math.min(encoderOut.remaining(), len);

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,24 @@ public void testUTF16WithSingleByteRead() throws IOException {
105105

106106
@Test
107107
public void testReadZero() throws Exception {
108-
ReaderInputStream r = new ReaderInputStream(new StringReader("test"));
108+
final String inStr = "test";
109+
ReaderInputStream r = new ReaderInputStream(new StringReader(inStr));
109110
byte[] bytes = new byte[30];
110111
assertEquals(0, r.read(bytes, 0, 0));
112+
assertEquals(inStr.length(), r.read(bytes, 0, inStr.length()+1));
113+
// Should always return 0 for length == 0
114+
assertEquals(0, r.read(bytes, 0, 0));
115+
}
116+
117+
@Test
118+
public void testReadZeroEmptyString() throws Exception {
119+
ReaderInputStream r = new ReaderInputStream(new StringReader(""));
120+
byte[] bytes = new byte[30];
121+
// Should always return 0 for length == 0
122+
assertEquals(0, r.read(bytes, 0, 0));
123+
assertEquals(-1, r.read(bytes, 0, 1));
124+
assertEquals(0, r.read(bytes, 0, 0));
125+
assertEquals(-1, r.read(bytes, 0, 1));
111126
}
112127

113128
/**

0 commit comments

Comments
 (0)