Skip to content

Commit f72beff

Browse files
committed
PDFBOX-6166: replace default implementation of available and readFully
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1932019 13f79535-47bb-0310-9956-ffa450edef68
1 parent 0c4b77b commit f72beff

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.pdfbox.io;
1818

19+
import java.io.EOFException;
1920
import java.io.IOException;
2021
import java.io.InputStream;
2122

@@ -164,6 +165,23 @@ else if (!fetch())
164165
return numberOfBytesRead;
165166
}
166167

168+
@Override
169+
public void readFully(byte[] b, int offset, int length) throws IOException
170+
{
171+
// override the default implementation as the return value from length isn't reliable
172+
checkClosed();
173+
int bytesReadTotal = 0;
174+
while (bytesReadTotal < length)
175+
{
176+
int bytesReadNow = read(b, offset + bytesReadTotal, length - bytesReadTotal);
177+
if (bytesReadNow <= 0)
178+
{
179+
throw new EOFException("EOF, should have been detected earlier");
180+
}
181+
bytesReadTotal += bytesReadNow;
182+
}
183+
}
184+
167185
private void switchBuffers(int firstBuffer, int secondBuffer)
168186
{
169187
byte[] tmpBuffer = buffers[firstBuffer];
@@ -222,6 +240,16 @@ private boolean fetch() throws IOException
222240
return true;
223241
}
224242

243+
/**
244+
* Returns an estimate of the number of bytes that can be read (or skipped over) from the underlying input stream
245+
* without blocking, which may be 0, or 0 when end of stream is detected.
246+
*/
247+
@Override
248+
public int available() throws IOException
249+
{
250+
checkClosed();
251+
return is.available();
252+
}
225253

226254
/**
227255
* {@inheritDoc}

0 commit comments

Comments
 (0)