pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tchoje...@apache.org
Subject svn commit: r1640774 - in /pdfbox/branches/1.8: examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
Date Thu, 20 Nov 2014 17:44:22 GMT
Author: tchojecki
Date: Thu Nov 20 17:44:22 2014
New Revision: 1640774

URL: http://svn.apache.org/r1640774
Log:
PDFBOX-2512: OutOfMemory while signing large documents
- 1. clean the objectpool while closing the document
- 2. pass a BufferedInputStream to the SignatureInteface / remove some code
- pass a scratchfile to the load method in the example 'CreateSignature'

Modified:
    pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java

Modified: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java?rev=1640774&r1=1640773&r2=1640774&view=diff
==============================================================================
--- pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
(original)
+++ pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/signature/CreateSignature.java
Thu Nov 20 17:44:22 2014
@@ -40,6 +40,7 @@ import java.util.List;
 
 import org.apache.pdfbox.exceptions.COSVisitorException;
 import org.apache.pdfbox.exceptions.SignatureException;
+import org.apache.pdfbox.io.RandomAccessFile;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSignature;
 import org.apache.pdfbox.pdmodel.interactive.digitalsignature.SignatureInterface;
@@ -52,7 +53,7 @@ import org.bouncycastle.cms.CMSSignedGen
 import org.bouncycastle.jce.provider.BouncyCastleProvider;
 
 /**
- * <p>This is an example for singing a pdf with bouncy castle.</p>
+ * <p>This is an example for signing a pdf with bouncy castle.</p>
  * <p>A keystore can be created with the java keytool 
  * (e.g. keytool -genkeypair -storepass 123456 -storetype pkcs12 -alias test -validity 365
-v -keyalg RSA -keystore keystore.p12 ) 
  * </p>
@@ -154,34 +155,50 @@ public class CreateSignature implements 
     fis.close();
     fis = new FileInputStream(outputDocument);
 
-    // load document
-    PDDocument doc = PDDocument.load(document);
+    File scratchFile = File.createTempFile("pdfbox_scratch", ".bin");
+    RandomAccessFile randomAccessFile = new RandomAccessFile(scratchFile, "rw");
 
-    // create signature dictionary
-    PDSignature signature = new PDSignature();
-    signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
-    // subfilter for basic and PAdES Part 2 signatures
-    signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
-    signature.setName("signer name");
-    signature.setLocation("signer location");
-    signature.setReason("reason for signature");
-
-    // the signing date, needed for valid signature
-    signature.setSignDate(Calendar.getInstance());
-
-    // register signature dictionary and sign interface
-    if (options==null)
+    try
     {
-      doc.addSignature(signature, this);
+      // load document
+      PDDocument doc = PDDocument.load(document, randomAccessFile);
+  
+      // create signature dictionary
+      PDSignature signature = new PDSignature();
+      signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); // default filter
+      // subfilter for basic and PAdES Part 2 signatures
+      signature.setSubFilter(PDSignature.SUBFILTER_ADBE_PKCS7_DETACHED);
+      signature.setName("signer name");
+      signature.setLocation("signer location");
+      signature.setReason("reason for signature");
+  
+      // the signing date, needed for valid signature
+      signature.setSignDate(Calendar.getInstance());
+  
+      // register signature dictionary and sign interface
+      if (options==null)
+      {
+        doc.addSignature(signature, this);
+      } 
+      else 
+      {
+        doc.addSignature(signature, this, options);
+      }
+      
+      // write incremental (only for signing purpose)
+      doc.saveIncremental(fis, fos);
     } 
-    else 
+    finally
     {
-      doc.addSignature(signature, this, options);
+      if (randomAccessFile!= null) 
+      {
+        randomAccessFile.close();
+      }
+      if (scratchFile != null && scratchFile.exists() && !scratchFile.delete())
+      {
+        scratchFile.deleteOnExit();
+      }
     }
-    
-    // write incremental (only for signing purpose)
-    doc.saveIncremental(fis, fos);
-
     return outputDocument;
   }
 

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1640774&r1=1640773&r2=1640774&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Thu Nov
20 17:44:22 2014
@@ -598,29 +598,25 @@ public class COSDocument extends COSBase
                 trailer = null;
             }
             // Clear object pool
-            List<COSObject> list = getObjects();
-            if (list != null && !list.isEmpty()) 
+            for (COSObject object : objectPool.values()) 
             {
-                for (COSObject object : list) 
+                COSBase cosObject = object.getObject();
+                // clear the resources of the pooled objects
+                if (cosObject instanceof COSStream)
                 {
-                    COSBase cosObject = object.getObject();
-                    // clear the resources of the pooled objects
-                    if (cosObject instanceof COSStream)
-                    {
-                        ((COSStream)cosObject).close();
-                    }
-                    else if (cosObject instanceof COSDictionary)
-                    {
-                        ((COSDictionary)cosObject).clear();
-                    }
-                    else if (cosObject instanceof COSArray)
-                    {
-                        ((COSArray)cosObject).clear();
-                    }
-                    // TODO are there other kind of COSObjects to be cleared?
+                    ((COSStream)cosObject).close();
                 }
-                list.clear();
+                else if (cosObject instanceof COSDictionary)
+                {
+                    ((COSDictionary)cosObject).clear();
+                }
+                else if (cosObject instanceof COSArray)
+                {
+                    ((COSArray)cosObject).clear();
+                }
+                // TODO are there other kind of COSObjects to be cleared?
             }
+            objectPool.clear();
             closed = true;
         }
     }

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java?rev=1640774&r1=1640773&r2=1640774&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdfwriter/COSWriter.java Thu
Nov 20 17:44:22 2014
@@ -16,8 +16,7 @@
  */
 package org.apache.pdfbox.pdfwriter;
 
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
+import java.io.BufferedInputStream;
 import java.io.Closeable;
 import java.io.IOException;
 import java.io.InputStream;
@@ -759,41 +758,30 @@ public class COSWriter implements ICOSVi
             }
         
             getStandardOutput().setPos(0);
-            // Begin - extracting document
-            InputStream filterInputStream = new COSFilterInputStream(in, 
-                    new int[] {0,signaturePosition[0],signaturePosition[1],left});
-            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+            
+            InputStream filterInputStream = null;
             try 
             {
-                byte[] buffer = new byte[1024];
-                int c;
-                while((c = filterInputStream.read(buffer)) != -1)
+                filterInputStream = new COSFilterInputStream(new BufferedInputStream(in),
new int[] {0,signaturePosition[0],signaturePosition[1],left});
+                SignatureInterface signatureInterface = doc.getSignatureInterface();
+                byte[] sign = signatureInterface.sign(filterInputStream);
+                String signature = new COSString(sign).getHexString();
+                int startPos = signaturePosition[0] + 1; // move past "<"
+                int endPos = signaturePosition[1] - 1; // move in front of ">"
+                if (startPos + signature.length() > endPos)            
                 {
-                    bytes.write(buffer, 0, c);
+                    throw new IOException("Can't write signature, not enough space");
                 }
+                getStandardOutput().setPos(startPos);
+                getStandardOutput().write(signature.getBytes());
             } 
             finally 
             {
-                if(filterInputStream !=null)
-                {
-                    filterInputStream.close();
-                }
-            }
-
-            byte[] pdfContent = bytes.toByteArray();
-            // End - extracting document
-        
-            SignatureInterface signatureInterface = doc.getSignatureInterface();
-            byte[] sign = signatureInterface.sign(new ByteArrayInputStream(pdfContent));
-            String signature = new COSString(sign).getHexString();
-            int startPos = signaturePosition[0] + 1; // move past "<"
-            int endPos = signaturePosition[1] - 1; // move in front of ">"
-            if (startPos + signature.length() > endPos)            
-            {
-                throw new IOException("Can't write signature, not enough space");
+              if(filterInputStream !=null)
+              {
+                filterInputStream.close();
+              }
             }
-            getStandardOutput().setPos(startPos);
-            getStandardOutput().write(signature.getBytes());
         }
     }
     



Mime
View raw message