while (skipped != offset)
{
long curSkip = ra.skip(offset - skipped);
if (curSkip < 0)
{
throw new ValidationException("Unable to skip bytes in the PDFFile to check stream length");
}
skipped += curSkip;
}
// ---- go to the stream key word
if (readUntilStream(ra))
{
int c = ra.read();
if (c == '\r')
{
ra.read();
} // else c is '\n' no more character to read
// ---- Here is the true beginning of the Stream Content.
// ---- Read the given length of bytes and check the 10 next bytes
// ---- to see if there are endstream.
byte[] buffer = new byte[1024];
int nbBytesToRead = length;
do
{
int cr = 0;
if (nbBytesToRead > 1024)
{
cr = ra.read(buffer, 0, 1024);
}
else
{
cr = ra.read(buffer, 0, nbBytesToRead);
}
if (cr == -1)
{
addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_LENGTH_INVALID,
"Stream length is invalide"));
return;
}
else
{
nbBytesToRead = nbBytesToRead - cr;
}
} while (nbBytesToRead > 0);
int len = "endstream".length() + 2;
byte[] buffer2 = new byte[len];
for (int i = 0; i < len; ++i)
{
buffer2[i] = (byte) ra.read();
}
// ---- check the content of 10 last characters
String endStream = new String(buffer2);
if (buffer2[0] == '\r' && buffer2[1] == '\n')
{
if (!endStream.contains("endstream"))
{
addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_LENGTH_INVALID,
"Stream length is invalide"));
}
}
else if (buffer2[0] == '\r' && buffer2[1] == 'e')
{
if (!endStream.contains("endstream"))
{
addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_LENGTH_INVALID,
"Stream length is invalide"));
}
}
else if (buffer2[0] == '\n' && buffer2[1] == 'e')
{
if (!endStream.contains("endstream"))
{
addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_LENGTH_INVALID,
"Stream length is invalide"));
}
}
else
{
addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_LENGTH_INVALID,
"Stream length is invalide"));
}
}
else
{
addValidationError(context, new ValidationError(ERROR_SYNTAX_STREAM_LENGTH_INVALID,
"Stream length is invalide"));
}
}
}
catch (IOException e)
{
throw new ValidationException("Unable to read a stream to validate it due to : " + e.getMessage(), e);
}
finally
{
if (ra != null)
{