Skip to content

Commit e07fdbd

Browse files
committed
PDFBOX-6083: catch length 0 case + plus test
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1929152 13f79535-47bb-0310-9956-ffa450edef68
1 parent 48aef99 commit e07fdbd

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

io/src/main/java/org/apache/pdfbox/io/RandomAccessRead.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ default void readFully(byte[] b, int offset, int length) throws IOException
189189
throw new EOFException("Premature end of buffer reached");
190190
}
191191
int bytesReadTotal = 0;
192-
do
192+
while (bytesReadTotal < length)
193193
{
194194
int bytesReadNow = read(b, offset + bytesReadTotal, length - bytesReadTotal);
195195
if (bytesReadNow <= 0)
@@ -198,6 +198,5 @@ default void readFully(byte[] b, int offset, int length) throws IOException
198198
}
199199
bytesReadTotal += bytesReadNow;
200200
}
201-
while (bytesReadTotal < length);
202201
}
203202
}

io/src/test/java/org/apache/pdfbox/io/RandomAccessReadBufferedFileTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,17 @@ void testReadFullyAcrossBuffers() throws IOException
296296
Assertions.assertArrayEquals(expectedBytes, b);
297297
}
298298
}
299+
300+
@Test
301+
void testReadFullyNothing() throws IOException, URISyntaxException
302+
{
303+
try (RandomAccessRead randomAccessSource = new RandomAccessReadBufferedFile(
304+
new File(getClass().getResource("RandomAccessReadFile1.txt").toURI())))
305+
{
306+
assertEquals(0, randomAccessSource.getPosition());
307+
byte[] b = new byte[0];
308+
randomAccessSource.readFully(b);
309+
assertEquals(0, randomAccessSource.getPosition());
310+
}
311+
}
299312
}

0 commit comments

Comments
 (0)