COSDictionary dict = (COSDictionary) options.getDictionaryObject("DecodeParms");
int predictor = -1;
int colors = -1;
int bitsPerPixel = -1;
int columns = -1;
InflaterInputStream decompressor = null;
ByteArrayInputStream bais = null;
ByteArrayOutputStream baos = null;
if (dict!=null)
{
predictor = dict.getInt("Predictor");
colors = dict.getInt("Colors");
bitsPerPixel = options.getInt("BitsPerComponent");
columns = dict.getInt("Columns");
}
try
{
// Decompress data to temporary ByteArrayOutputStream
decompressor = new InflaterInputStream(compressedData);
byte[] buffer = new byte[BUFFER_SIZE];
int amountRead;
// Decode data using given predictor
if (predictor==-1 || predictor == 1 && predictor == 10)
{
// decoding not needed
while ((amountRead = decompressor.read(buffer, 0, BUFFER_SIZE)) != -1)
{
result.write(buffer, 0, amountRead);
}
}
else
{
if (colors==-1 || bitsPerPixel==-1 || columns==-1)
{
throw new IOException("Could not read all parameters to decode image");
}
baos = new ByteArrayOutputStream();
while ((amountRead = decompressor.read(buffer, 0, BUFFER_SIZE)) != -1)
{
baos.write(buffer, 0, amountRead);
}
baos.flush();
// Copy data to ByteArrayInputStream for reading
bais = new ByteArrayInputStream(baos.toByteArray());
baos.close();
baos = null;
byte[] decodedData = decodePredictor(predictor, colors, bitsPerPixel, columns, bais);
bais.close();
bais = new ByteArrayInputStream(decodedData);
// write decoded data to result
while ((amountRead = bais.read(buffer)) != -1)
{
result.write(buffer, 0, amountRead);
}
bais.close();
bais = null;
}
result.flush();
}
finally
{
if (decompressor != null)
{
decompressor.close();
}
if (bais != null)
{
bais.close();
}