Search in sources :

Example 1 with RandomAccessInputStream

use of com.tom_roush.pdfbox.io.RandomAccessInputStream in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.io.RandomAccessInputStream)

Example 2 with RandomAccessInputStream

use of com.tom_roush.pdfbox.io.RandomAccessInputStream in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.COSFilterInputStream) RandomAccessInputStream(com.tom_roush.pdfbox.io.RandomAccessInputStream)

Example 3 with RandomAccessInputStream

use of com.tom_roush.pdfbox.io.RandomAccessInputStream in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.io.RandomAccessInputStream) InputStream(java.io.InputStream) RandomAccessInputStream(com.tom_roush.pdfbox.io.RandomAccessInputStream)

Example 4 with RandomAccessInputStream

use of com.tom_roush.pdfbox.io.RandomAccessInputStream in project PdfBox-Android by TomRoush.

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(com.tom_roush.pdfbox.io.RandomAccessInputStream)

Example 5 with RandomAccessInputStream

use of com.tom_roush.pdfbox.io.RandomAccessInputStream in project PdfBox-Android by TomRoush.

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<DecodeResult>();
    InputStream input = in;
    if (filters.isEmpty()) {
        input = in;
    } else {
        Set<Filter> filterSet = new HashSet<Filter>(filters);
        if (filterSet.size() != filters.size()) {
            throw new IOException("Duplicate");
        }
        // 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 : FilterInputStream(java.io.FilterInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) RandomAccessInputStream(com.tom_roush.pdfbox.io.RandomAccessInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) RandomAccess(com.tom_roush.pdfbox.io.RandomAccess) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DecodeResult(com.tom_roush.pdfbox.filter.DecodeResult) Filter(com.tom_roush.pdfbox.filter.Filter) ByteArrayInputStream(java.io.ByteArrayInputStream) RandomAccessOutputStream(com.tom_roush.pdfbox.io.RandomAccessOutputStream) RandomAccessInputStream(com.tom_roush.pdfbox.io.RandomAccessInputStream) HashSet(java.util.HashSet)

Aggregations

RandomAccessInputStream (com.tom_roush.pdfbox.io.RandomAccessInputStream)6 InputStream (java.io.InputStream)3 RandomAccess (com.tom_roush.pdfbox.io.RandomAccess)2 RandomAccessOutputStream (com.tom_roush.pdfbox.io.RandomAccessOutputStream)2 IOException (java.io.IOException)2 DecodeResult (com.tom_roush.pdfbox.filter.DecodeResult)1 Filter (com.tom_roush.pdfbox.filter.Filter)1 COSFilterInputStream (com.tom_roush.pdfbox.pdmodel.interactive.digitalsignature.COSFilterInputStream)1 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 HashSet (java.util.HashSet)1