You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/04/05 23:35:47 UTC
svn commit: r1671427 -
/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
Author: tilman
Date: Sun Apr 5 21:35:47 2015
New Revision: 1671427
URL: http://svn.apache.org/r1671427
Log:
PDFBOX-2576: refactor segment in parseCOSStream, replace code that should never be accessed with exception throw
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java?rev=1671427&r1=1671426&r2=1671427&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/COSParser.java Sun Apr 5 21:35:47 2015
@@ -911,37 +911,14 @@ public class COSParser extends BaseParse
}
}
- boolean useReadUntilEnd = false;
// get output stream to copy data to
if (streamLengthObj != null && validateStreamLength(streamLengthObj.longValue()))
{
out = stream.createFilteredStream(streamLengthObj);
- long remainBytes = streamLengthObj.longValue();
- int bytesRead = 0;
- while (remainBytes > 0)
- {
- final int readBytes = pdfSource
- .read(streamCopyBuf,
- 0,
- (remainBytes > STREAMCOPYBUFLEN) ? STREAMCOPYBUFLEN : (int) remainBytes);
- if (readBytes <= 0)
- {
- useReadUntilEnd = true;
- out.close();
- pdfSource.unread(bytesRead);
- break;
- }
- out.write(streamCopyBuf, 0, readBytes);
- remainBytes -= readBytes;
- bytesRead += readBytes;
- }
+ readValidStream(out, streamLengthObj);
}
else
{
- useReadUntilEnd = true;
- }
- if (useReadUntilEnd)
- {
out = stream.createFilteredStream();
readUntilEndStream(new EndstreamOutputStream(out));
}
@@ -977,6 +954,24 @@ public class COSParser extends BaseParse
return stream;
}
+ private void readValidStream(OutputStream out, COSNumber streamLengthObj) throws IOException
+ {
+ long remainBytes = streamLengthObj.longValue();
+ while (remainBytes > 0)
+ {
+ final int chunk = (remainBytes > STREAMCOPYBUFLEN) ? STREAMCOPYBUFLEN : (int) remainBytes;
+ final int readBytes = pdfSource.read(streamCopyBuf, 0, chunk);
+ if (readBytes <= 0)
+ {
+ // shouldn't happen, the stream length has already been validated
+ throw new IOException("read error at offset " + pdfSource.getOffset()
+ + ": expected " + chunk + " bytes, but read() returns " + readBytes);
+ }
+ out.write(streamCopyBuf, 0, readBytes);
+ remainBytes -= readBytes;
+ }
+ }
+
private boolean validateStreamLength(long streamLength) throws IOException
{
boolean streamLengthIsValid = true;