Search in sources :

Example 1 with RandomAccessInputStream

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

the class COSWriter method writeExternalSignature.

/**
 * Write externally created signature of PDF data obtained via {@link #getDataToSign()} method.
 *
 * @param cmsSignature CMS signature byte array
 * @throws IllegalStateException if PDF is not prepared for external signing
 * @throws IOException if source data stream is closed
 */
public void writeExternalSignature(byte[] cmsSignature) throws IOException {
    if (incrementPart == null || incrementalInput == null) {
        throw new IllegalStateException("PDF not prepared for setting signature");
    }
    byte[] signatureBytes = Hex.getBytes(cmsSignature);
    // subtract 2 bytes because of the enclosing "<>"
    if (signatureBytes.length > signatureLength - 2) {
        throw new IOException("Can't write signature, not enough space");
    }
    // overwrite the signature Contents in the buffer
    int incPartSigOffset = (int) (signatureOffset - incrementalInput.length());
    System.arraycopy(signatureBytes, 0, incrementPart, incPartSigOffset + 1, signatureBytes.length);
    // write the data to the incremental output stream
    IOUtils.copy(new RandomAccessInputStream(incrementalInput), incrementalOutput);
    incrementalOutput.write(incrementPart);
    // prevent further use
    incrementPart = null;
}
Also used : IOException(java.io.IOException) RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream)

Example 2 with RandomAccessInputStream

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

the class COSWriter method doWriteIncrement.

/**
 * Write an incremental update for a non signature case. This can be used for e.g. augmenting
 * signatures.
 *
 * @throws IOException
 */
private void doWriteIncrement() throws IOException {
    // write existing PDF
    IOUtils.copy(new RandomAccessInputStream(incrementalInput), incrementalOutput);
    // write the actual incremental update
    incrementalOutput.write(((ByteArrayOutputStream) output).toByteArray());
}
Also used : RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream)

Example 3 with RandomAccessInputStream

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

the class COSStream method createInputStream.

public COSInputStream createInputStream(DecodeOptions options) throws IOException {
    checkClosed();
    if (isWriting) {
        throw new IllegalStateException("Cannot read while there is an open stream writer");
    }
    ensureRandomAccessExists(true);
    InputStream input = new RandomAccessInputStream(randomAccess);
    return COSInputStream.create(getFilterList(), this, input, scratchFile, options);
}
Also used : RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream) InputStream(java.io.InputStream) RandomAccessInputStream(org.apache.pdfbox.io.RandomAccessInputStream)

Example 4 with RandomAccessInputStream

use of org.apache.pdfbox.io.RandomAccessInputStream 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 5 with RandomAccessInputStream

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

the class COSWriter method getDataToSign.

/**
 * Return the stream of PDF data to be signed. Clients should use this method only to create
 * signatures externally. {@link #write(PDDocument)} method should have been called prior. The
 * created signature should be set using {@link #writeExternalSignature(byte[])}.
 * <p>
 * When {@link SignatureInterface} instance is used, COSWriter obtains and writes the signature
 * itself.
 * </p>
 *
 * @return data stream to be signed
 * @throws IllegalStateException if PDF is not prepared for external signing
 * @throws IOException if input data is closed
 */
public InputStream getDataToSign() throws IOException {
    if (incrementPart == null || incrementalInput == null) {
        throw new IllegalStateException("PDF not prepared for signing");
    }
    // range of incremental bytes to be signed (includes /ByteRange but not /Contents)
    int incPartSigOffset = (int) (signatureOffset - incrementalInput.length());
    int afterSigOffset = incPartSigOffset + (int) signatureLength;
    int[] range = { 0, incPartSigOffset, afterSigOffset, incrementPart.length - afterSigOffset };
    return new SequenceInputStream(new RandomAccessInputStream(incrementalInput), new COSFilterInputStream(incrementPart, range));
}
Also used : SequenceInputStream(java.io.SequenceInputStream) COSFilterInputStream(org.apache.pdfbox.pdmodel.interactive.digitalsignature.COSFilterInputStream) 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