org.apache.pdfbox.io.ScratchFile Java Examples

The following examples show how to use org.apache.pdfbox.io.ScratchFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfBleachSession.java    From DocBleach with MIT License 6 votes vote down vote up
@SuppressFBWarnings(
    value = "EXS_EXCEPTION_SOFTENING_RETURN_FALSE",
    justification = "This method is an helper to check the password")
private PDDocument testPassword(ScratchFile inFile, RandomAccessRead source, String password)
    throws IOException {
  PDFParser parser = new PDFParser(source, password, inFile);
  try {
    parser.parse();
    return parser.getPDDocument();
  } catch (InvalidPasswordException e) {
    LOGGER.error("The tested password is invalid");
    return null;
  } finally {
    source.rewind((int) source.getPosition());
  }
}
 
Example #2
Source File: PdfBleachSession.java    From DocBleach with MIT License 6 votes vote down vote up
private PDDocument getDocument(RandomAccessRead source) throws IOException, BleachException {
  PDDocument doc;
  for (String pwd : COMMON_PASSWORDS) {
    ScratchFile scratchFile = new ScratchFile(MEMORY_USAGE_SETTING);
    doc = testPassword(scratchFile, source, pwd);
    if (doc != null) {
      LOGGER.debug("Password was guessed: '{}'", pwd);
      doc.protect(new StandardProtectionPolicy(pwd, pwd, doc.getCurrentAccessPermission()));
      return doc;
    }
    scratchFile.close();
  }

  // @TODO: fetch password from config?

  throw new BleachException("PDF is protected with an unknown password");
}
 
Example #3
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Parses a PDF. Depending on the memory settings parameter the given input stream is either
 * copied to memory or to a temporary file to enable random access to the pdf.
 *
 * @param input stream that contains the document. Don't forget to close it after loading.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(InputStream input, String password, InputStream keyStore, 
                              String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    try
    {
        RandomAccessRead source = scratchFile.createBuffer(input);
        PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
        parser.parse();
        return parser.getPDDocument();
    }
    catch (IOException ioe)
    {
        IOUtils.closeQuietly(scratchFile);
        throw ioe;
    }
}
 
Example #4
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private static PDDocument load(RandomAccessBufferedFileInputStream raFile, String password,
                               InputStream keyStore, String alias,
                               MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    try
    {
        PDFParser parser = new PDFParser(raFile, password, keyStore, alias, scratchFile);
        parser.parse();
        return parser.getPDDocument();
    }
    catch (IOException ioe)
    {
        IOUtils.closeQuietly(scratchFile);
        throw ioe;
    }
}
 
Example #5
Source File: COSOutputStream.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Creates a new COSOutputStream writes to an encoded COS stream.
 * 
 * @param filters Filters to apply.
 * @param parameters Filter parameters.
 * @param output Encoded stream.
 * @param scratchFile Scratch file to use.
 * 
 * @throws IOException If there was an error creating a temporary buffer
 */
COSOutputStream(List<Filter> filters, COSDictionary parameters, OutputStream output,
                ScratchFile scratchFile) throws IOException
{
    super(output);
    this.filters = filters;
    this.parameters = parameters;
    this.scratchFile = scratchFile;

    if (filters.isEmpty())
    {
        this.buffer = null;
    }
    else
    {
        this.buffer = scratchFile.createBuffer();
    }
}
 
Example #6
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void init(ScratchFile scratchFile) throws IOException
{
    String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE);
    if (eofLookupRangeStr != null)
    {
        try
        {
            setEOFLookupRange(Integer.parseInt(eofLookupRangeStr));
        }
        catch (NumberFormatException nfe)
        {
            LOG.warn("System property " + SYSPROP_EOFLOOKUPRANGE
                    + " does not contain an integer value, but: '" + eofLookupRangeStr + "'");
        }
    }
    document = new COSDocument(scratchFile);
}
 
Example #7
Source File: COSDocument.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Constructor. Uses main memory to buffer PDF streams.
 */
public COSDocument()
{
    this(ScratchFile.getMainMemoryOnlyInstance());
}
 
Example #8
Source File: COSStream.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
/**
 * Creates a new stream with an empty dictionary. Data is stored in the given scratch file.
 *
 * @param scratchFile Scratch file for writing stream data.
 */
public COSStream(ScratchFile scratchFile)
{
    setInt(COSName.LENGTH, 0);
    this.scratchFile = scratchFile != null ? scratchFile : ScratchFile.getMainMemoryOnlyInstance();
}
 
Example #9
Source File: PDDocument.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Parses a PDF.
 * 
 * @param input byte array that contains the document.
 * @param password password to be used for decryption
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams 
 * 
 * @return loaded document
 * 
 * @throws InvalidPasswordException If the password is incorrect.
 * @throws IOException In case of a reading or parsing error.
 */
public static PDDocument load(byte[] input, String password, InputStream keyStore, 
        String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
    ScratchFile scratchFile = new ScratchFile(memUsageSetting);
    RandomAccessRead source = new RandomAccessBuffer(input);
    PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
    parser.parse();
    return parser.getPDDocument();
}
 
Example #10
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 3 votes vote down vote up
/**
 * Constructor.
 * 
 * @param source input representing the pdf.
 * @param decryptionPassword password to be used for decryption.
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 * @param scratchFile buffer handler for temporary storage; it will be closed on
 *        {@link COSDocument#close()}
 *
 * @throws IOException If something went wrong.
 */
public PDFParser(RandomAccessRead source, String decryptionPassword, InputStream keyStore,
                 String alias, ScratchFile scratchFile) throws IOException
{
    super(source, decryptionPassword, keyStore, alias);
    fileLen = source.length();
    init(scratchFile);
}
 
Example #11
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param source input representing the pdf.
 * @param decryptionPassword password to be used for decryption.
 * @throws IOException If something went wrong.
 */
public PDFParser(RandomAccessRead source, String decryptionPassword) throws IOException
{
    this(source, decryptionPassword, ScratchFile.getMainMemoryOnlyInstance());
}
 
Example #12
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * 
 * @param source input representing the pdf.
 * @param decryptionPassword password to be used for decryption.
 * @param scratchFile use a {@link ScratchFile} for temporary storage.
 *
 * @throws IOException If something went wrong.
 */
public PDFParser(RandomAccessRead source, String decryptionPassword, ScratchFile scratchFile)
        throws IOException
{
    this(source, decryptionPassword, null, null, scratchFile);
}
 
Example #13
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param source input representing the pdf.
 * @param decryptionPassword password to be used for decryption.
 * @param keyStore key store to be used for decryption when using public key security 
 * @param alias alias to be used for decryption when using public key security
 *
 * @throws IOException If something went wrong.
 */
public PDFParser(RandomAccessRead source, String decryptionPassword, InputStream keyStore,
        String alias) throws IOException
{
    this(source, decryptionPassword, keyStore, alias, ScratchFile.getMainMemoryOnlyInstance());
}
 
Example #14
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * 
 * @param source input representing the pdf.
 * @param scratchFile use a {@link ScratchFile} for temporary storage.
 * 
 * @throws IOException If something went wrong.
 */
public PDFParser(RandomAccessRead source, ScratchFile scratchFile) throws IOException
{
    this(source, "", scratchFile);
}
 
Example #15
Source File: PDFParser.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor.
 * Unrestricted main memory will be used for buffering PDF streams.
 * 
 * @param source source representing the pdf.
 * @throws IOException If something went wrong.
 */
public PDFParser(RandomAccessRead source) throws IOException
{
    this(source, "", ScratchFile.getMainMemoryOnlyInstance());
}
 
Example #16
Source File: COSStream.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Creates a new stream with an empty dictionary.
 * <p>
 * Try to avoid using this constructor because it creates a new scratch file in memory. Instead,
 * use {@link COSDocument#createCOSStream() document.getDocument().createCOSStream()} which will
 * use the existing scratch file (in memory or in temp file) of the document.
 * </p>
 */
public COSStream()
{
    this(ScratchFile.getMainMemoryOnlyInstance());
}
 
Example #17
Source File: COSDocument.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Constructor that will use the provide memory handler for storage of the
 * PDF streams.
 *
 * @param scratchFile memory handler for buffering of PDF streams
 * 
 */
public COSDocument(ScratchFile scratchFile)
{
    this.scratchFile = scratchFile;
}
 
Example #18
Source File: COSInputStream.java    From gcs with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Creates a new COSInputStream from an encoded input stream.
 *
 * @param filters Filters to be applied.
 * @param parameters Filter parameters.
 * @param in Encoded input stream.
 * @param scratchFile Scratch file to use, or null.
 * @return Decoded stream.
 * @throws IOException If the stream could not be read.
 */
static COSInputStream create(List<Filter> filters, COSDictionary parameters, InputStream in,
                             ScratchFile scratchFile) throws IOException
{
    return create(filters, parameters, in, scratchFile, DecodeOptions.DEFAULT);
}