Skip to content

Commit 2f92931

Browse files
author
Maruan Sahyoun
committed
PDFBOX-6166: fixes after code review; adjust unit test; parts by Claude Sonnet
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1932993 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2e110f3 commit 2f92931

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

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

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ public void seek(long position) throws IOException
9595
@Override
9696
public void skip(int length) throws IOException
9797
{
98-
for (int i=0; i< length;i++)
98+
byte[] skipBuffer = new byte[Math.min(length, BUFFER_SIZE)];
99+
int remaining = length;
100+
while (remaining > 0)
99101
{
100-
read();
102+
int bytesRead = read(skipBuffer, 0, Math.min(remaining, skipBuffer.length));
103+
if (bytesRead == -1)
104+
{
105+
break;
106+
}
107+
remaining -= bytesRead;
101108
}
102109
}
103110

@@ -138,6 +145,20 @@ public int read() throws IOException
138145
public int read(byte[] b, int offset, int length) throws IOException
139146
{
140147
checkClosed();
148+
// Parameter validation as defined in InputStream.read(byte[], int, int)
149+
if (b == null)
150+
{
151+
throw new NullPointerException("buffer is null");
152+
}
153+
if (offset < 0 || length < 0 || offset + length > b.length)
154+
{
155+
throw new IndexOutOfBoundsException("buffer length=" + b.length + " offset=" + offset
156+
+ " length=" + length);
157+
}
158+
if (length == 0)
159+
{
160+
return 0;
161+
}
141162
if (isEOF())
142163
{
143164
return -1;
@@ -162,7 +183,7 @@ else if (!fetch())
162183
break;
163184
}
164185
}
165-
return numberOfBytesRead;
186+
return numberOfBytesRead > 0 ? numberOfBytesRead : -1;
166187
}
167188

168189
@Override
@@ -233,7 +254,7 @@ private boolean fetch() throws IOException
233254
catch (IOException exception)
234255
{
235256
// some data could be read -> don't throw an exception
236-
LOG.warn("FlateFilter: premature end of stream due to a DataFormatException");
257+
LOG.warn("premature end of stream, some data could be read ", exception);
237258
isEOF = true;
238259
throw exception;
239260
}
@@ -248,7 +269,8 @@ private boolean fetch() throws IOException
248269
public int available() throws IOException
249270
{
250271
checkClosed();
251-
return is.available();
272+
int buffered = Math.max(0, bufferBytes[CURRENT] - currentBufferPointer);
273+
return buffered + is.available();
252274
}
253275

254276
/**
@@ -258,7 +280,7 @@ public int available() throws IOException
258280
public long length() throws IOException
259281
{
260282
checkClosed();
261-
return size;
283+
return size + is.available();
262284
}
263285

264286
@Override
@@ -269,8 +291,9 @@ public void rewind(int bytes) throws IOException
269291
{
270292
currentBufferPointer -= bytes;
271293
position -= bytes;
294+
isEOF = false;
272295
}
273-
else if (bufferBytes[LAST] > 0)
296+
else if (bufferBytes[LAST] > 0 && (bytes - currentBufferPointer) <= bufferBytes[LAST])
274297
{
275298
// there is a former buffer
276299
int remainingBytesToRewind = bytes - currentBufferPointer;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void testRewindAcrossBuffers2() throws IOException
255255
ByteArrayInputStream bais = new ByteArrayInputStream(ba);
256256
try (RandomAccessRead rar = new NonSeekableRandomAccessReadInputStream(bais))
257257
{
258-
assertEquals(0, rar.length()); // not really what I'd expect...
258+
assertEquals(4096 * 2, rar.length());
259259
int len = rar.read(new byte[4096 + 1]);
260260
assertEquals(4096 * 2, rar.length());
261261
assertEquals(4096 + 1, len);

0 commit comments

Comments
 (0)