*
* @throws IOException If there is an error reading the stream.
*/
protected COSStream parseCOSStream( COSDictionary dic, RandomAccess file ) throws IOException
{
COSStream stream = new COSStream( dic, file );
OutputStream out = null;
try
{
String streamString = readString();
//long streamLength;
if (!streamString.equals("stream"))
{
throw new IOException("expected='stream' actual='" + streamString + "'");
}
//PDF Ref 3.2.7 A stream must be followed by either
//a CRLF or LF but nothing else.
int whitespace = pdfSource.read();
//see brother_scan_cover.pdf, it adds whitespaces
//after the stream but before the start of the
//data, so just read those first
while (whitespace == 0x20)
{
whitespace = pdfSource.read();
}
if( whitespace == 0x0D )
{
whitespace = pdfSource.read();
if( whitespace != 0x0A )
{
pdfSource.unread( whitespace );
//The spec says this is invalid but it happens in the real
//world so we must support it.
//throw new IOException("expected='0x0A' actual='0x" +
// Integer.toHexString(whitespace) + "' " + pdfSource);
}
}
else if (whitespace == 0x0A)
{
//that is fine
}
else
{
//we are in an error.
//but again we will do a lenient parsing and just assume that everything
//is fine
pdfSource.unread( whitespace );
//throw new IOException("expected='0x0D or 0x0A' actual='0x" +
//Integer.toHexString(whitespace) + "' " + pdfSource);
}
COSBase streamLength = dic.getDictionaryObject(COSName.LENGTH);
/*long length = -1;
if( streamLength instanceof COSNumber )
{
length = ((COSNumber)streamLength).intValue();
}
else if( streamLength instanceof COSObject &&
((COSObject)streamLength).getObject() instanceof COSNumber )
{
length = ((COSNumber)((COSObject)streamLength).getObject()).intValue();
}*/
//length = -1;
//streamLength = null;
//Need to keep track of the
out = stream.createFilteredStream( streamLength );
String endStream = null;
//the length is wrong in some pdf documents which means
//that PDFBox must basically ignore it in order to be able to read
//the most number of PDF documents. This of course is a penalty hit,
//maybe I could implement a faster parser.