You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2020/09/22 06:06:57 UTC

svn commit: r1881920 - in /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox: Loader.java cos/COSDocument.java pdfparser/PDFParser.java

Author: lehmi
Date: Tue Sep 22 06:06:57 2020
New Revision: 1881920

URL: http://svn.apache.org/viewvc?rev=1881920&view=rev
Log:
PDFBOX-4836: move the creation of ScratchFiles to COSDocument, remove the usage of a ScratchFile as a cache for a stream as input

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/Loader.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdfparser/PDFParser.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/Loader.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/Loader.java?rev=1881920&r1=1881919&r2=1881920&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/Loader.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/Loader.java Tue Sep 22 06:06:57 2020
@@ -28,7 +28,6 @@ import org.apache.pdfbox.io.MemoryUsageS
 import org.apache.pdfbox.io.RandomAccessReadBufferedFile;
 import org.apache.pdfbox.io.RandomAccessReadBuffer;
 import org.apache.pdfbox.io.RandomAccessRead;
-import org.apache.pdfbox.io.ScratchFile;
 import org.apache.pdfbox.pdfparser.FDFParser;
 import org.apache.pdfbox.pdfparser.PDFParser;
 import org.apache.pdfbox.pdmodel.PDDocument;
@@ -196,10 +195,19 @@ public class Loader
     public static PDDocument loadPDF(byte[] input, String password, InputStream keyStore, String alias,
             MemoryUsageSetting memUsageSetting) throws IOException
     {
-        ScratchFile scratchFile = new ScratchFile(memUsageSetting);
-        RandomAccessRead source = new RandomAccessReadBuffer(input);
-        PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
-        return parser.parse();
+        RandomAccessRead source = null;
+        try
+        {
+            // RandomAccessRead is not closed here, may be needed for signing
+            source = new RandomAccessReadBuffer(input);
+            PDFParser parser = new PDFParser(source, password, keyStore, alias, memUsageSetting);
+            return parser.parse();
+        }
+        catch (IOException ioe)
+        {
+            IOUtils.closeQuietly(source);
+            throw ioe;
+        }
     }
     /**
      * Parses a PDF. Unrestricted main memory will be used for buffering PDF streams.
@@ -295,10 +303,11 @@ public class Loader
     public static PDDocument loadPDF(File file, String password, InputStream keyStore, String alias,
             MemoryUsageSetting memUsageSetting) throws IOException
     {
-        @SuppressWarnings({ "squid:S2095" }) // raFile not closed here, may be needed for signing
-        RandomAccessReadBufferedFile raFile = new RandomAccessReadBufferedFile(file);
+        RandomAccessRead raFile = null;
         try
         {
+            // RandomAccessRead is not closed here, may be needed for signing
+            raFile = new RandomAccessReadBufferedFile(file);
             return Loader.loadPDF(raFile, password, keyStore, alias, memUsageSetting);
         }
         catch (IOException ioe)
@@ -315,7 +324,7 @@ public class Loader
      * @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 PDF streams
+     * @param memUsageSetting defines how memory is used PDF streams
      * 
      * @return loaded document
      * 
@@ -325,17 +334,8 @@ public class Loader
             InputStream keyStore, String alias, MemoryUsageSetting memUsageSetting)
             throws IOException
     {
-        ScratchFile scratchFile = new ScratchFile(memUsageSetting);
-        try
-        {
-            PDFParser parser = new PDFParser(raFile, password, keyStore, alias, scratchFile);
-            return parser.parse();
-        }
-        catch (IOException ioe)
-        {
-            IOUtils.closeQuietly(scratchFile);
-            throw ioe;
-        }
+        PDFParser parser = new PDFParser(raFile, password, keyStore, alias, memUsageSetting);
+        return parser.parse();
     }
     /**
      * Parses a PDF. The given input stream is copied to the memory to enable random access to the pdf. Unrestricted
@@ -352,12 +352,13 @@ public class Loader
     {
         return Loader.loadPDF(input, "", null, null, MemoryUsageSetting.setupMainMemoryOnly());
     }
+    
     /**
      * 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. Don't forget to close it after loading.
-     * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams
+     * @param memUsageSetting defines how memory is used for buffering PDF streams
      * 
      * @return loaded document
      * 
@@ -403,13 +404,14 @@ public class Loader
     {
         return Loader.loadPDF(input, password, keyStore, alias, MemoryUsageSetting.setupMainMemoryOnly());
     }
+    
     /**
      * 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. Don't forget to close it after loading.
      * @param password password to be used for decryption
-     * @param memUsageSetting defines how memory is used for buffering input stream and PDF streams
+     * @param memUsageSetting defines how memory is used for buffering PDF streams
      * 
      * @return loaded document
      * 
@@ -421,15 +423,15 @@ public class Loader
     {
         return Loader.loadPDF(input, password, null, null, memUsageSetting);
     }
+    
     /**
-     * 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.
+     * Parses a PDF. The given input stream is copied to memory 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
+     * @param memUsageSetting defines how memory is used for buffering PDF streams
      * 
      * @return loaded document
      * 
@@ -439,26 +441,17 @@ public class Loader
     public static PDDocument loadPDF(InputStream input, String password, InputStream keyStore,
             String alias, MemoryUsageSetting memUsageSetting) throws IOException
     {
-        ScratchFile scratchFile = null;
+        RandomAccessRead source = null;
         try
         {
-            RandomAccessRead source;
-            if (memUsageSetting.useMainMemory() && !memUsageSetting.isMainMemoryRestricted())
-            {
-                // use RandomAccessReadBuffer instead of a ScratchFile for the input if MainMemoryOnly is configured
-                source = new RandomAccessReadBuffer(input);
-            }
-            else
-            {
-                scratchFile = new ScratchFile(memUsageSetting);
-                source = scratchFile.createBuffer(input);
-            }
-            PDFParser parser = new PDFParser(source, password, keyStore, alias, scratchFile);
+            // RandomAccessRead is not closed here, may be needed for signing
+            source = new RandomAccessReadBuffer(input);
+            PDFParser parser = new PDFParser(source, password, keyStore, alias, memUsageSetting);
             return parser.parse();
         }
         catch (IOException ioe)
         {
-            IOUtils.closeQuietly(scratchFile);
+            IOUtils.closeQuietly(source);
             throw ioe;
         }
     }

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1881920&r1=1881919&r2=1881920&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Tue Sep 22 06:06:57 2020
@@ -109,20 +109,39 @@ public class COSDocument extends COSBase
      */
     public COSDocument(ICOSParser parser)
     {
-        this(ScratchFile.getMainMemoryOnlyInstance(), parser);
+        this(MemoryUsageSetting.setupMainMemoryOnly(), parser);
     }
 
     /**
-     * Constructor that will use the provide memory settings for storage of the PDF streams.
+     * Constructor that will use the provided memory settings for storage of the PDF streams.
      *
      * @param memUsageSetting defines how memory is used for buffering PDF streams
      * 
      */
     public COSDocument(MemoryUsageSetting memUsageSetting)
     {
+        this(memUsageSetting, null);
+    }
+
+    /**
+     * Constructor that will use the provided memory settings for storage of the PDF streams.
+     *
+     * @param memUsageSetting defines how memory is used for buffering PDF streams
+     * @param parser Parser to be used to parse the document on demand
+     * 
+     */
+    public COSDocument(MemoryUsageSetting memUsageSetting, ICOSParser parser)
+    {
         try
         {
-            scratchFile = new ScratchFile(memUsageSetting);
+            if (memUsageSetting != null)
+            {
+                scratchFile = new ScratchFile(memUsageSetting);
+            }
+            else
+            {
+                scratchFile = ScratchFile.getMainMemoryOnlyInstance();
+            }
         }
         catch (IOException ioe)
         {
@@ -131,19 +150,6 @@ public class COSDocument extends COSBase
 
             scratchFile = ScratchFile.getMainMemoryOnlyInstance();
         }
-    }
-
-    /**
-     * Constructor that will use the provide memory handler for storage of the PDF streams.
-     *
-     * @param scratchFile memory handler for buffering of PDF streams
-     * @param parser Parser to be used to parse the document on demand
-     * 
-     */
-    public COSDocument(ScratchFile scratchFile, ICOSParser parser)
-    {
-        this.scratchFile = scratchFile != null ? scratchFile
-                : ScratchFile.getMainMemoryOnlyInstance();
         this.parser = parser;
     }
 

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=1881920&r1=1881919&r2=1881920&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 Tue Sep 22 06:06:57 2020
@@ -27,8 +27,8 @@ import org.apache.pdfbox.cos.COSDictiona
 import org.apache.pdfbox.cos.COSDocument;
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.io.IOUtils;
+import org.apache.pdfbox.io.MemoryUsageSetting;
 import org.apache.pdfbox.io.RandomAccessRead;
-import org.apache.pdfbox.io.ScratchFile;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.encryption.InvalidPasswordException;
 
@@ -45,20 +45,7 @@ public class PDFParser extends COSParser
      */
     public PDFParser(RandomAccessRead source) throws IOException
     {
-        this(source, "", null);
-    }
-
-    /**
-     * 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);
+        this(source, "");
     }
 
     /**
@@ -71,22 +58,7 @@ public class PDFParser extends COSParser
      */
     public PDFParser(RandomAccessRead source, String decryptionPassword) throws IOException
     {
-        this(source, decryptionPassword, null);
-    }
-
-    /**
-     * 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);
+        this(source, decryptionPassword, null, null);
     }
 
     /**
@@ -111,21 +83,20 @@ public class PDFParser extends COSParser
      * 
      * @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 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()}
+     * @param memUsageSetting defines how memory is used for buffering PDF streams
      *
      * @throws IOException If something went wrong.
      */
     public PDFParser(RandomAccessRead source, String decryptionPassword, InputStream keyStore,
-                     String alias, ScratchFile scratchFile) throws IOException
+            String alias, MemoryUsageSetting memUsageSetting) throws IOException
     {
         super(source, decryptionPassword, keyStore, alias);
-        init(scratchFile);
+        init(memUsageSetting);
     }
-    
-    private void init(ScratchFile scratchFile)
+
+    private void init(MemoryUsageSetting memUsageSetting)
     {
         String eofLookupRangeStr = System.getProperty(SYSPROP_EOFLOOKUPRANGE);
         if (eofLookupRangeStr != null)
@@ -140,7 +111,7 @@ public class PDFParser extends COSParser
                         + " does not contain an integer value, but: '" + eofLookupRangeStr + "'");
             }
         }
-        document = new COSDocument(scratchFile, this);
+        document = new COSDocument(memUsageSetting, this);
     }
     
     /**