Search in sources :

Example 1 with RandomAccessOutputStream

use of org.apache.pdfbox.io.RandomAccessOutputStream in project pdfbox by apache.

the class COSStream method createOutputStream.

/**
 * Returns a new OutputStream for writing stream data, using and the given filters.
 *
 * @param filters COSArray or COSName of filters to be used.
 * @return OutputStream for un-encoded stream data.
 * @throws IOException If the output stream could not be created.
 */
public OutputStream createOutputStream(COSBase filters) throws IOException {
    checkClosed();
    if (isWriting) {
        throw new IllegalStateException("Cannot have more than one open stream writer.");
    }
    // apply filters, if any
    if (filters != null) {
        setItem(COSName.FILTER, filters);
    }
    if (randomAccess != null)
        randomAccess.clear();
    else
        randomAccess = getScratchFile().createBuffer();
    OutputStream randomOut = new RandomAccessOutputStream(randomAccess);
    OutputStream cosOut = new COSOutputStream(getFilterList(), this, randomOut, getScratchFile());
    isWriting = true;
    return new FilterOutputStream(cosOut) {

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            this.out.write(b, off, len);
        }

        @Override
        public void close() throws IOException {
            super.close();
            setInt(COSName.LENGTH, (int) randomAccess.length());
            isWriting = false;
        }
    };
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilterOutputStream(java.io.FilterOutputStream) RandomAccessOutputStream(org.apache.pdfbox.io.RandomAccessOutputStream) RandomAccessOutputStream(org.apache.pdfbox.io.RandomAccessOutputStream) FilterOutputStream(java.io.FilterOutputStream)

Example 2 with RandomAccessOutputStream

use of org.apache.pdfbox.io.RandomAccessOutputStream in project pdfbox by apache.

the class COSStream method createRawOutputStream.

/**
 * Returns a new OutputStream for writing encoded PDF data. Experts only!
 *
 * @return OutputStream for raw PDF stream data.
 * @throws IOException If the output stream could not be created.
 */
public OutputStream createRawOutputStream() throws IOException {
    checkClosed();
    if (isWriting) {
        throw new IllegalStateException("Cannot have more than one open stream writer.");
    }
    if (randomAccess != null)
        randomAccess.clear();
    else
        randomAccess = getScratchFile().createBuffer();
    OutputStream out = new RandomAccessOutputStream(randomAccess);
    isWriting = true;
    return new FilterOutputStream(out) {

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            this.out.write(b, off, len);
        }

        @Override
        public void close() throws IOException {
            super.close();
            setInt(COSName.LENGTH, (int) randomAccess.length());
            isWriting = false;
        }
    };
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FilterOutputStream(java.io.FilterOutputStream) RandomAccessOutputStream(org.apache.pdfbox.io.RandomAccessOutputStream) RandomAccessOutputStream(org.apache.pdfbox.io.RandomAccessOutputStream) FilterOutputStream(java.io.FilterOutputStream)

Example 3 with RandomAccessOutputStream

use of org.apache.pdfbox.io.RandomAccessOutputStream 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)

Example 4 with RandomAccessOutputStream

use of org.apache.pdfbox.io.RandomAccessOutputStream 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

RandomAccessOutputStream (org.apache.pdfbox.io.RandomAccessOutputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 FilterOutputStream (java.io.FilterOutputStream)3 OutputStream (java.io.OutputStream)3 InputStream (java.io.InputStream)2 RandomAccess (org.apache.pdfbox.io.RandomAccess)2 RandomAccessInputStream (org.apache.pdfbox.io.RandomAccessInputStream)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FilterInputStream (java.io.FilterInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 DecodeResult (org.apache.pdfbox.filter.DecodeResult)1