Search in sources :

Example 1 with DecodeResult

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);
}
Also used : DecodeResult(org.apache.pdfbox.filter.DecodeResult) Filter(org.apache.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HashSet(java.util.HashSet)

Example 2 with DecodeResult

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);
}
Also used : RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream) FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RandomAccess(org.apache.pdfbox.io.RandomAccess) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DecodeResult(org.apache.pdfbox.filter.DecodeResult) ByteArrayInputStream(java.io.ByteArrayInputStream) RandomAccessOutputStream(org.apache.pdfbox.io.RandomAccessOutputStream) RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FilterInputStream (java.io.FilterInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 DecodeResult (org.apache.pdfbox.filter.DecodeResult)2 HashSet (java.util.HashSet)1 Filter (org.apache.pdfbox.filter.Filter)1 RandomAccess (org.apache.pdfbox.io.RandomAccess)1 RandomAccessInputStream (org.apache.pdfbox.io.RandomAccessInputStream)1 RandomAccessOutputStream (org.apache.pdfbox.io.RandomAccessOutputStream)1