Search in sources :

Example 6 with RandomAccessInputStream

use of org.apache.pdfbox.io.RandomAccessInputStream 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();
    }
}
Also used : RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) RandomAccessOutputStream(org.apache.pdfbox.io.RandomAccessOutputStream) FilterOutputStream(java.io.FilterOutputStream) RandomAccess(org.apache.pdfbox.io.RandomAccess) RandomAccessOutputStream(org.apache.pdfbox.io.RandomAccessOutputStream) RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream)

Aggregations

RandomAccessInputStream (org.apache.pdfbox.io.RandomAccessInputStream)6 InputStream (java.io.InputStream)3 IOException (java.io.IOException)2 RandomAccess (org.apache.pdfbox.io.RandomAccess)2 RandomAccessOutputStream (org.apache.pdfbox.io.RandomAccessOutputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FilterInputStream (java.io.FilterInputStream)1 FilterOutputStream (java.io.FilterOutputStream)1 OutputStream (java.io.OutputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 ArrayList (java.util.ArrayList)1 DecodeResult (org.apache.pdfbox.filter.DecodeResult)1 COSFilterInputStream (org.apache.pdfbox.pdmodel.interactive.digitalsignature.COSFilterInputStream)1