You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by tb...@apache.org on 2015/09/28 13:31:47 UTC
svn commit: r1705657 - in /pdfbox/trunk:
pdfbox/src/main/java/org/apache/pdfbox/io/
pdfbox/src/main/java/org/apache/pdfbox/multipdf/
pdfbox/src/main/java/org/apache/pdfbox/pdfparser/
pdfbox/src/main/java/org/apache/pdfbox/pdmodel/ pdfbox/src/test/java/...
Author: tboehme
Date: Mon Sep 28 11:31:47 2015
New Revision: 1705657
URL: http://svn.apache.org/viewvc?rev=1705657&view=rev
Log:
PDFBOX-2883: remove PDDocument constructor/methods using boolean 'useScratchFiles' parameter and ensure to have an equivalent constructor/method using MemoryUsageSetting object; default is using main memory only (as before)
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/MemoryUsageSetting.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFile.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFMerger.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/MemoryUsageSetting.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/MemoryUsageSetting.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/MemoryUsageSetting.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/MemoryUsageSetting.java Mon Sep 28 11:31:47 2015
@@ -162,6 +162,30 @@ public final class MemoryUsageSetting
{
return new MemoryUsageSetting(true, true, maxMainMemoryBytes, maxStorageBytes);
}
+
+ /**
+ * Returns a copy of this instance with the maximum memory/storage restriction
+ * divided by the provided number of parallel uses.
+ *
+ * @param parallelUseCount specifies the number of parallel usages for the setting to
+ * be returned
+ *
+ * @return a copy from this instance with the maximum memory/storage restrictions
+ * adjusted to the multiple usage
+ */
+ public MemoryUsageSetting getPartitionedCopy(int parallelUseCount)
+ {
+ long newMaxMainMemoryBytes = maxMainMemoryBytes <= 0 ? maxMainMemoryBytes :
+ maxMainMemoryBytes / parallelUseCount;
+ long newMaxStorageBytes = maxStorageBytes <= 0 ? maxStorageBytes :
+ maxStorageBytes / parallelUseCount;
+
+ MemoryUsageSetting copy = new MemoryUsageSetting( useMainMemory, useTempFile,
+ newMaxMainMemoryBytes, newMaxStorageBytes );
+ copy.tempDir = tempDir;
+
+ return copy;
+ }
/**
* Sets directory to be used for temporary files.
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFile.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFile.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFile.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/io/ScratchFile.java Mon Sep 28 11:31:47 2015
@@ -131,6 +131,19 @@ public class ScratchFile implements Clos
}
/**
+ * Getter for an instance using only unrestricted main memory for buffering
+ * (same as <code>new ScratchFile(MemoryUsageSetting.setupMainMemoryOnly())</code>).
+ *
+ * @return instance configured to only use main memory with no size restriction
+ *
+ * @throws IOException
+ */
+ public static ScratchFile getMainMemoryOnlyInstance() throws IOException
+ {
+ return new ScratchFile(MemoryUsageSetting.setupMainMemoryOnly());
+ }
+
+ /**
* Returns a new free page, either from free page pool
* or by enlarging scratch file (may be created).
*
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Mon Sep 28 11:31:47 2015
@@ -36,6 +36,7 @@ import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSNumber;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.cos.COSString;
+import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;
@@ -171,10 +172,12 @@ public class PDFMergerUtility
* Merge the list of source documents, saving the result in the destination
* file.
*
- * @param useScratchFiles enables the usage of a scratch file if set to true
+ * @param memUsageSetting defines how memory is used for buffering PDF streams;
+ * in case of <code>null</code> unrestricted main memory is used
+ *
* @throws IOException If there is an error saving the document.
*/
- public void mergeDocuments(boolean useScratchFiles) throws IOException
+ public void mergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException
{
PDDocument destination = null;
InputStream sourceFile;
@@ -185,13 +188,16 @@ public class PDFMergerUtility
try
{
+ MemoryUsageSetting partitionedMemSetting = memUsageSetting != null ?
+ memUsageSetting.getPartitionedCopy(sources.size()+1) :
+ MemoryUsageSetting.setupMainMemoryOnly();
Iterator<InputStream> sit = sources.iterator();
- destination = new PDDocument(useScratchFiles);
+ destination = new PDDocument(partitionedMemSetting);
while (sit.hasNext())
{
sourceFile = sit.next();
- source = PDDocument.load(sourceFile, useScratchFiles);
+ source = PDDocument.load(sourceFile, partitionedMemSetting);
tobeclosed.add(source);
appendDocument(destination, source);
}
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java Mon Sep 28 11:31:47 2015
@@ -50,13 +50,14 @@ public class PDFParser extends COSParser
/**
* 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, "", false);
+ this(source, "", ScratchFile.getMainMemoryOnlyInstance());
}
/**
@@ -67,13 +68,14 @@ public class PDFParser extends COSParser
*
* @throws IOException If something went wrong.
*/
- public PDFParser(RandomAccessRead source, boolean useScratchFiles) throws IOException
+ public PDFParser(RandomAccessRead source, ScratchFile scratchFile) throws IOException
{
- this(source, "", useScratchFiles);
+ this(source, "", scratchFile);
}
/**
* 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.
@@ -81,7 +83,7 @@ public class PDFParser extends COSParser
*/
public PDFParser(RandomAccessRead source, String decryptionPassword) throws IOException
{
- this(source, decryptionPassword, false);
+ this(source, decryptionPassword, ScratchFile.getMainMemoryOnlyInstance());
}
/**
@@ -93,14 +95,15 @@ public class PDFParser extends COSParser
*
* @throws IOException If something went wrong.
*/
- public PDFParser(RandomAccessRead source, String decryptionPassword, boolean useScratchFiles)
+ public PDFParser(RandomAccessRead source, String decryptionPassword, ScratchFile scratchFile)
throws IOException
{
- this(source, decryptionPassword, null, null, useScratchFiles);
+ this(source, decryptionPassword, null, null, scratchFile);
}
/**
* 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.
@@ -112,29 +115,7 @@ public class PDFParser extends COSParser
public PDFParser(RandomAccessRead source, String decryptionPassword, InputStream keyStore,
String alias) throws IOException
{
- this(source, decryptionPassword, keyStore, alias, false);
- }
-
- /**
- * 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 useScratchFiles use a buffer for temporary storage.
- *
- * @throws IOException If something went wrong.
- */
- public PDFParser(RandomAccessRead source, String decryptionPassword, InputStream keyStore,
- String alias, boolean useScratchFiles) throws IOException
- {
- super(source);
- fileLen = source.length();
- password = decryptionPassword;
- keyStoreInputStream = keyStore;
- keyAlias = alias;
- init(useScratchFiles);
+ this(source, decryptionPassword, keyStore, alias, ScratchFile.getMainMemoryOnlyInstance());
}
/**
@@ -178,24 +159,6 @@ public class PDFParser extends COSParser
document = new COSDocument(scratchFile);
}
- private void init(boolean useScratchFiles) 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(useScratchFiles);
- }
-
/**
* This will get the PD document that was parsed. When you are done with
* this document you must call close() on it to release resources.
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Mon Sep 28 11:31:47 2015
@@ -112,63 +112,34 @@ public class PDDocument implements Close
*/
public PDDocument()
{
- this(false);
+ this(MemoryUsageSetting.setupMainMemoryOnly());
}
/**
* Creates an empty PDF document.
* You need to add at least one page for the document to be valid.
*
- * @param useScratchFiles enables the usage of a scratch file if set to true
- */
- public PDDocument(boolean useScratchFiles)
- {
- this(useScratchFiles, null);
- }
-
- /**
- * Creates an empty PDF document.
- * You need to add at least one page for the document to be valid.
- *
* @param memUsageSetting defines how memory is used for buffering PDF streams
*/
public PDDocument(MemoryUsageSetting memUsageSetting)
{
- this(true, memUsageSetting);
- }
-
- /**
- * Internal constructor which support setting scratch file usage
- * via boolean parameter or directly (new). This will be only needed
- * as long as the new ScratchFile handling is tested.
- *
- * <p>You need to add at least one page for the document to be valid.</p>
- *
- * @param useScratchFiles enables the usage of a scratch file if set to true
- * @param memUsageSetting defines how memory is used for buffering PDF streams
- */
- private PDDocument(boolean useScratchFiles, MemoryUsageSetting memUsageSetting)
- {
ScratchFile scratchFile = null;
- if (memUsageSetting != null)
+ try
{
+ scratchFile = new ScratchFile(memUsageSetting);
+ }
+ catch (IOException ioe)
+ {
+ LOG.warn("Error initializing scratch file: " + ioe.getMessage() +
+ ". Fall back to main memory usage only.");
try
{
- scratchFile = new ScratchFile(memUsageSetting);
- }
- catch (IOException ioe)
- {
- LOG.warn("Error initializing scratch file: " + ioe.getMessage() + ". Fall back to main memory usage only.");
- try
- {
- scratchFile = new ScratchFile(MemoryUsageSetting.setupMainMemoryOnly());
- } catch ( IOException ioe2 ) {}
+ scratchFile = new ScratchFile(MemoryUsageSetting.setupMainMemoryOnly());
}
+ catch (IOException ioe2) {}
}
-
- document = scratchFile != null ? new COSDocument(scratchFile) :
- new COSDocument(useScratchFiles);
+ document = new COSDocument(scratchFile);
pdfSource = null;
// First we need a trailer
@@ -324,7 +295,7 @@ public class PDDocument implements Close
// take care that page and acroforms do not share the same array (if so, we don't need to add it twice)
if (!(annotations instanceof COSArrayList &&
acroFormFields instanceof COSArrayList &&
- ((COSArrayList) annotations).toList().equals(((COSArrayList) acroFormFields).toList()) &&
+ ((COSArrayList<?>) annotations).toList().equals(((COSArrayList<?>) acroFormFields).toList()) &&
checkFields))
{
annotations.add(signatureField.getWidgets().get(0));
@@ -783,7 +754,7 @@ public class PDDocument implements Close
}
/**
- * Parses a PDF.
+ * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
*
* @param file file to be loaded
*
@@ -793,22 +764,7 @@ public class PDDocument implements Close
*/
public static PDDocument load(File file) throws IOException
{
- return load(file, "", false);
- }
-
- /**
- * Parses a PDF.
- *
- * @param file file to be loaded
- * @param useScratchFiles enables the usage of a scratch file if set to true
- *
- * @return loaded document
- *
- * @throws IOException in case of a file reading or parsing error
- */
- public static PDDocument load(File file, boolean useScratchFiles) throws IOException
- {
- return load(file, "", null, null, useScratchFiles);
+ return load(file, "", MemoryUsageSetting.setupMainMemoryOnly());
}
/**
@@ -827,7 +783,7 @@ public class PDDocument implements Close
}
/**
- * Parses a PDF.
+ * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
*
* @param file file to be loaded
* @param password password to be used for decryption
@@ -838,23 +794,7 @@ public class PDDocument implements Close
*/
public static PDDocument load(File file, String password) throws IOException
{
- return load(file, password, null, null, false);
- }
-
- /**
- * Parses a PDF.
- *
- * @param file file to be loaded
- * @param password password to be used for decryption
- * @param useScratchFiles enables the usage of a scratch file if set to true
- *
- * @return loaded document
- *
- * @throws IOException in case of a file reading or parsing error
- */
- public static PDDocument load(File file, String password, boolean useScratchFiles) throws IOException
- {
- return load(file, password, null, null, useScratchFiles);
+ return load(file, password, null, null, MemoryUsageSetting.setupMainMemoryOnly());
}
/**
@@ -874,7 +814,7 @@ public class PDDocument implements Close
}
/**
- * Parses a PDF.
+ * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
*
* @param file file to be loaded
* @param password password to be used for decryption
@@ -886,31 +826,9 @@ public class PDDocument implements Close
* @throws IOException in case of a file reading or parsing error
*/
public static PDDocument load(File file, String password, InputStream keyStore, String alias)
- throws IOException
+ throws IOException
{
- return load(file, password, keyStore, alias, false);
- }
-
- /**
- * Parses a PDF.
- *
- * @param file file to be loaded
- * @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 useScratchFiles enables the usage of a scratch file if set to true
- *
- * @return loaded document
- *
- * @throws IOException in case of a file reading or parsing error
- */
- public static PDDocument load(File file, String password, InputStream keyStore, String alias,
- boolean useScratchFiles) throws IOException
- {
- RandomAccessBufferedFileInputStream raFile = new RandomAccessBufferedFileInputStream(file);
- PDFParser parser = new PDFParser(raFile, password, keyStore, alias, useScratchFiles);
- parser.parse();
- return parser.getPDDocument();
+ return load(file, password, keyStore, alias, MemoryUsageSetting.setupMainMemoryOnly());
}
/**
@@ -937,6 +855,7 @@ public class PDDocument implements Close
/**
* Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf.
+ * Unrestricted main memory will be used for buffering PDF streams.
*
* @param input stream that contains the document.
*
@@ -946,29 +865,12 @@ public class PDDocument implements Close
*/
public static PDDocument load(InputStream input) throws IOException
{
- return load(input, "", null, null, false);
- }
-
- /**
- * Parses a PDF. Depending on the parameter useScratchFiles the given input
- * stream is either copied to the memory or to a temporary file to enable
- * random access to the pdf.
- *
- * @param input stream that contains the document.
- * @param useScratchFiles enables the usage of a scratch file if set to true
- *
- * @return loaded document
- *
- * @throws IOException in case of a file reading or parsing error
- */
- public static PDDocument load(InputStream input, boolean useScratchFiles) throws IOException
- {
- return load(input, "", null, null, useScratchFiles);
+ return load(input, "", null, null, MemoryUsageSetting.setupMainMemoryOnly());
}
/**
- * Parses a PDF. Depending on the parameter useScratchFiles the given input
- * stream is either copied to the memory or to a temporary file to enable
+ * Parses a PDF. Depending on the memory settings parameter the given input
+ * stream is either copied to main memory or to a temporary file to enable
* random access to the pdf.
*
* @param input stream that contains the document.
@@ -985,6 +887,7 @@ public class PDDocument implements Close
/**
* Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf.
+ * Unrestricted main memory will be used for buffering PDF streams.
*
* @param input stream that contains the document.
* @param password password to be used for decryption
@@ -996,11 +899,12 @@ public class PDDocument implements Close
public static PDDocument load(InputStream input, String password)
throws IOException
{
- return load(input, password, null, null, false);
+ return load(input, password, null, null, MemoryUsageSetting.setupMainMemoryOnly());
}
/**
* Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf.
+ * Unrestricted main memory will be used for buffering PDF streams.
*
* @param input stream that contains the document.
* @param password password to be used for decryption
@@ -1014,30 +918,12 @@ public class PDDocument implements Close
public static PDDocument load(InputStream input, String password, InputStream keyStore, String alias)
throws IOException
{
- return load(input, password, keyStore, alias, false);
+ return load(input, password, keyStore, alias, MemoryUsageSetting.setupMainMemoryOnly());
}
/**
- * Parses a PDF. Depending on the parameter useScratchFiles the given input
- * stream is either copied to the memory or to a temporary file to enable
- * random access to the pdf.
- *
- * @param input stream that contains the document.
- * @param password password to be used for decryption
- * @param useScratchFiles enables the usage of a scratch file if set to true
- *
- * @return loaded document
- *
- * @throws IOException in case of a file reading or parsing error
- */
- public static PDDocument load(InputStream input, String password, boolean useScratchFiles) throws IOException
- {
- return load(input, password, null, null, useScratchFiles);
- }
-
- /**
- * Parses a PDF. Depending on the parameter useScratchFiles the given input
- * stream is either copied to the memory or to a temporary file to enable
+ * Parses a PDF. Depending on the memory settings parameter the given input
+ * stream is either copied to main memory or to a temporary file to enable
* random access to the pdf.
*
* @param input stream that contains the document.
@@ -1048,95 +934,82 @@ public class PDDocument implements Close
*
* @throws IOException in case of a file reading or parsing error
*/
- public static PDDocument load(InputStream input, String password, MemoryUsageSetting memUsageSetting) throws IOException
+ public static PDDocument load(InputStream input, String password, MemoryUsageSetting memUsageSetting)
+ throws IOException
{
return load(input, password, null, null, memUsageSetting);
}
/**
- * Parses a PDF. Depending on the parameter useScratchFiles the given input
- * stream is either copied to the memory or to a temporary file to enable
+ * 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.
* @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 useScratchFiles enables the usage of a scratch file if set to true
+ * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams
*
* @return loaded document
*
* @throws IOException in case of a file reading or parsing error
*/
public static PDDocument load(InputStream input, String password, InputStream keyStore,
- String alias, boolean useScratchFiles) throws IOException
+ String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
- RandomAccessRead source;
- if (useScratchFiles)
- {
- source = new RandomAccessBufferedFileInputStream(input);
- }
- else
- {
- source = new RandomAccessBuffer(input);
- }
- PDFParser parser = new PDFParser(source, password, keyStore, alias, useScratchFiles);
+ ScratchFile scratchFile = new ScratchFile(memUsageSetting);
+ RandomAccessRead source = scratchFile.createBuffer(input);
+ PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
parser.parse();
return parser.getPDDocument();
}
/**
- * Parses a PDF. Depending on the parameter useScratchFiles the given input
- * stream is either copied to the memory or to a temporary file to enable
- * random access to the pdf.
+ * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
*
- * @param input stream 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
+ * @param input byte array that contains the document.
*
* @return loaded document
*
* @throws IOException in case of a file reading or parsing error
*/
- public static PDDocument load(InputStream input, String password, InputStream keyStore,
- String alias, MemoryUsageSetting memUsageSetting) throws IOException
+ public static PDDocument load(byte[] input) throws IOException
{
- ScratchFile scratchFile = new ScratchFile(memUsageSetting);
- RandomAccessRead source = scratchFile.createBuffer(input);
- PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
- parser.parse();
- return parser.getPDDocument();
+ return load(input, "");
}
/**
- * Parses a PDF.
+ * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
*
* @param input byte array that contains the document.
+ * @param password password to be used for decryption
*
* @return loaded document
*
* @throws IOException in case of a file reading or parsing error
*/
- public static PDDocument load(byte[] input) throws IOException
+ public static PDDocument load(byte[] input, String password) throws IOException
{
- return load(input, "");
+ return load(input, password, null, null);
}
/**
- * Parses a PDF.
+ * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
*
* @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
*
* @return loaded document
*
* @throws IOException in case of a file reading or parsing error
*/
- public static PDDocument load(byte[] input, String password) throws IOException
+ public static PDDocument load(byte[] input, String password, InputStream keyStore,
+ String alias) throws IOException
{
- return load(input, password, null, null);
+ return load(input, password, keyStore, alias, MemoryUsageSetting.setupMainMemoryOnly());
}
/**
@@ -1146,16 +1019,18 @@ public class PDDocument implements Close
* @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 IOException in case of a file reading or parsing error
*/
public static PDDocument load(byte[] input, String password, InputStream keyStore,
- String alias) throws IOException
+ String alias, MemoryUsageSetting memUsageSetting) throws IOException
{
+ ScratchFile scratchFile = new ScratchFile(memUsageSetting);
RandomAccessRead source = new RandomAccessBuffer(input);
- PDFParser parser = new PDFParser(source, password, keyStore, alias, false);
+ PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
parser.parse();
return parser.getPDDocument();
}
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/encryption/TestPublicKeyEncryption.java Mon Sep 28 11:31:47 2015
@@ -26,6 +26,7 @@ import java.security.cert.X509Certificat
import javax.crypto.Cipher;
+import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.encryption.AccessPermission;
import org.apache.pdfbox.pdmodel.encryption.PublicKeyProtectionPolicy;
@@ -262,7 +263,7 @@ public class TestPublicKeyEncryption ext
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
doc.save(buffer);
return PDDocument.load(new ByteArrayInputStream(buffer.toByteArray()), decryptionPassword,
- keyStore, null, false);
+ keyStore, null, MemoryUsageSetting.setupMainMemoryOnly());
}
/**
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/multipdf/PDFMergerUtilityTest.java Mon Sep 28 11:31:47 2015
@@ -18,7 +18,10 @@ package org.apache.pdfbox.multipdf;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
+
import junit.framework.TestCase;
+
+import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
@@ -63,13 +66,13 @@ public class PDFMergerUtilityTest extend
checkMergeIdentical("PDFBox.GlobalResourceMergeTest.Doc01.decoded.pdf",
"PDFBox.GlobalResourceMergeTest.Doc02.decoded.pdf",
"GlobalResourceMergeTestResult.pdf",
- false);
+ MemoryUsageSetting.setupMainMemoryOnly());
// once again, with scratch file
checkMergeIdentical("PDFBox.GlobalResourceMergeTest.Doc01.decoded.pdf",
"PDFBox.GlobalResourceMergeTest.Doc02.decoded.pdf",
"GlobalResourceMergeTestResult2.pdf",
- true);
+ MemoryUsageSetting.setupTempFileOnly());
}
/**
@@ -84,13 +87,13 @@ public class PDFMergerUtilityTest extend
checkMergeIdentical("jpegrgb.pdf",
"multitiff.pdf",
"JpegMultiMergeTestResult.pdf",
- false);
+ MemoryUsageSetting.setupMainMemoryOnly());
// once again, with scratch file
checkMergeIdentical("jpegrgb.pdf",
"multitiff.pdf",
"JpegMultiMergeTestResult.pdf",
- true);
+ MemoryUsageSetting.setupTempFileOnly());
}
// see PDFBOX-2893
@@ -99,19 +102,19 @@ public class PDFMergerUtilityTest extend
checkMergeIdentical("PDFBox.GlobalResourceMergeTest.Doc01.pdf",
"PDFBox.GlobalResourceMergeTest.Doc02.pdf",
"GlobalResourceMergeTestResult.pdf",
- false);
+ MemoryUsageSetting.setupMainMemoryOnly());
// once again, with scratch file
checkMergeIdentical("PDFBox.GlobalResourceMergeTest.Doc01.pdf",
"PDFBox.GlobalResourceMergeTest.Doc02.pdf",
"GlobalResourceMergeTestResult2.pdf",
- true);
+ MemoryUsageSetting.setupTempFileOnly());
}
// checks that the result file of a merge has the same rendering as the two
// source files
private void checkMergeIdentical(String filename1, String filename2, String mergeFilename,
- boolean useScratchFiles)
+ MemoryUsageSetting memUsageSetting)
throws IOException
{
PDDocument srcDoc1 = PDDocument.load(new File(SRCDIR, filename1), (String)null);
@@ -138,7 +141,7 @@ public class PDFMergerUtilityTest extend
pdfMergerUtility.addSource(new File(SRCDIR, filename1));
pdfMergerUtility.addSource(new File(SRCDIR, filename2));
pdfMergerUtility.setDestinationFileName(TARGETTESTDIR + mergeFilename);
- pdfMergerUtility.mergeDocuments(useScratchFiles);
+ pdfMergerUtility.mergeDocuments(memUsageSetting);
PDDocument mergedDoc
= PDDocument.load(new File(TARGETTESTDIR, mergeFilename), (String)null);
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdfparser/TestPDFParser.java Mon Sep 28 11:31:47 2015
@@ -30,8 +30,10 @@ import java.io.FilenameFilter;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
+import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
import org.apache.pdfbox.io.RandomAccessRead;
+import org.apache.pdfbox.io.ScratchFile;
import org.junit.Before;
import org.junit.Test;
@@ -83,30 +85,31 @@ public class TestPDFParser
@Test
public void testPDFParserFile() throws IOException
{
- executeParserTest(new RandomAccessBufferedFileInputStream(new File(PATH_OF_PDF)), false);
+ executeParserTest(new RandomAccessBufferedFileInputStream(new File(PATH_OF_PDF)), MemoryUsageSetting.setupMainMemoryOnly());
}
@Test
public void testPDFParserInputStream() throws IOException
{
- executeParserTest(new RandomAccessBufferedFileInputStream(new FileInputStream(PATH_OF_PDF)), false);
+ executeParserTest(new RandomAccessBufferedFileInputStream(new FileInputStream(PATH_OF_PDF)), MemoryUsageSetting.setupMainMemoryOnly());
}
@Test
public void testPDFParserFileScratchFile() throws IOException
{
- executeParserTest(new RandomAccessBufferedFileInputStream(new File(PATH_OF_PDF)), true);
+ executeParserTest(new RandomAccessBufferedFileInputStream(new File(PATH_OF_PDF)), MemoryUsageSetting.setupTempFileOnly());
}
@Test
public void testPDFParserInputStreamScratchFile() throws IOException
{
- executeParserTest(new RandomAccessBufferedFileInputStream(new FileInputStream(PATH_OF_PDF)), true);
+ executeParserTest(new RandomAccessBufferedFileInputStream(new FileInputStream(PATH_OF_PDF)), MemoryUsageSetting.setupTempFileOnly());
}
- private void executeParserTest(RandomAccessRead source, boolean useScratchFile) throws IOException
+ private void executeParserTest(RandomAccessRead source, MemoryUsageSetting memUsageSetting) throws IOException
{
- PDFParser pdfParser = new PDFParser(source, useScratchFile);
+ ScratchFile scratchFile = new ScratchFile(memUsageSetting);
+ PDFParser pdfParser = new PDFParser(source, scratchFile);
pdfParser.parse();
COSDocument doc = pdfParser.getDocument();
assertNotNull(doc);
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFMerger.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFMerger.java?rev=1705657&r1=1705656&r2=1705657&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFMerger.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFMerger.java Mon Sep 28 11:31:47 2015
@@ -18,6 +18,7 @@ package org.apache.pdfbox.tools;
import java.io.IOException;
+import org.apache.pdfbox.io.MemoryUsageSetting;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
/**
@@ -66,7 +67,7 @@ public final class PDFMerger
String destinationFileName = args[args.length-1];
merger.setDestinationFileName(destinationFileName);
- merger.mergeDocuments(false);
+ merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
}
/**