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;
}
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());
}
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);
}
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);
}
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));
}
Aggregations