{
if( log.isDebugEnabled() )
{
log.debug("parseCOSStream() " + pdfSource );
}
COSStream stream = new COSStream( dic, file );
OutputStream out = null;
try
{
char c;
String streamString = readString();
int streamOffset;
//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();
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;
out = stream.createFilteredStream( streamLength );
if( length != -1 )
{
byte[] buffer = new byte[1024];
int amountRead = 0;
int totalAmountRead = 0;