use of org.apache.pdfbox.io.RandomAccess 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);
}
use of org.apache.pdfbox.io.RandomAccess in project pdfbox by apache.
the class COSOutputStream method close.
@Override
public void close() throws IOException {
try {
if (buffer != null) {
try {
// apply filters in reverse order
for (int i = filters.size() - 1; i >= 0; i--) {
try (InputStream unfilteredIn = new RandomAccessInputStream(buffer)) {
if (i == 0) {
/*
* The last filter to run can encode directly to the enclosed output
* stream.
*/
filters.get(i).encode(unfilteredIn, out, parameters, i);
} else {
RandomAccess filteredBuffer = scratchFile.createBuffer();
try (OutputStream filteredOut = new RandomAccessOutputStream(filteredBuffer)) {
filters.get(i).encode(unfilteredIn, filteredOut, parameters, i);
} finally {
buffer.close();
buffer = filteredBuffer;
}
}
}
}
} finally {
buffer.close();
buffer = null;
}
}
} finally {
super.close();
}
}
Aggregations