use of org.apache.pdfbox.filter.DecodeResult in project pdfbox by apache.
the class COSInputStream method create.
/**
* Creates a new COSInputStream from an encoded input stream.
*
* @param filters Filters to be applied.
* @param parameters Filter parameters.
* @param in Encoded input stream.
* @param options decode options for the encoded stream
* @return Decoded stream.
* @throws IOException If the stream could not be read.
*/
static COSInputStream create(List<Filter> filters, COSDictionary parameters, InputStream in, DecodeOptions options) throws IOException {
if (filters.isEmpty()) {
return new COSInputStream(in, Collections.<DecodeResult>emptyList());
}
List<DecodeResult> results = new ArrayList<>(filters.size());
InputStream input = in;
if (filters.size() > 1) {
Set<Filter> filterSet = new HashSet<>(filters);
if (filterSet.size() != filters.size()) {
throw new IOException("Duplicate");
}
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
// apply filters
for (int i = 0; i < filters.size(); i++) {
output.reset();
results.add(filters.get(i).decode(input, output, parameters, i, options));
input = new ByteArrayInputStream(output.toByteArray());
}
return new COSInputStream(input, results);
}
use of org.apache.pdfbox.filter.DecodeResult in project pdfbox by apache.
the class COSInputStream method create.
static COSInputStream create(List<Filter> filters, COSDictionary parameters, InputStream in, ScratchFile scratchFile, DecodeOptions options) throws IOException {
List<DecodeResult> results = new ArrayList<>();
InputStream input = in;
if (filters.isEmpty()) {
input = in;
} else {
// apply filters
for (int i = 0; i < filters.size(); i++) {
if (scratchFile != null) {
// scratch file
final RandomAccess buffer = scratchFile.createBuffer();
DecodeResult result = filters.get(i).decode(input, new RandomAccessOutputStream(buffer), parameters, i, options);
results.add(result);
input = new RandomAccessInputStream(buffer) {
@Override
public void close() throws IOException {
buffer.close();
}
};
} else {
// in-memory
ByteArrayOutputStream output = new ByteArrayOutputStream();
DecodeResult result = filters.get(i).decode(input, output, parameters, i, options);
results.add(result);
input = new ByteArrayInputStream(output.toByteArray());
}
}
}
return new COSInputStream(input, results);
}
Aggregations