Author: tilman
Date: Thu Sep 20 06:15:15 2018
New Revision: 1841425

URL: http://svn.apache.org/viewvc?rev=1841425&view=rev
Log:
PDFBOX-4312: fix bug that resulted in storage of signature bytes at wrong 
position or not at all or file corruption + improve javadoc

Modified:
    
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Modified: 
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1841425&r1=1841424&r2=1841425&view=diff
==============================================================================
--- 
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
 (original)
+++ 
pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
 Thu Sep 20 06:15:15 2018
@@ -21,8 +21,6 @@ import java.awt.print.Pageable;
 import java.awt.print.Printable;
 import java.awt.print.PrinterException;
 import java.awt.print.PrinterJob;
-import java.io.BufferedInputStream;
-import java.io.BufferedOutputStream;
 import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
@@ -50,6 +48,7 @@ import org.apache.pdfbox.cos.COSStream;
 import org.apache.pdfbox.exceptions.COSVisitorException;
 import org.apache.pdfbox.exceptions.CryptographyException;
 import org.apache.pdfbox.exceptions.SignatureException;
+import org.apache.pdfbox.io.IOUtils;
 import org.apache.pdfbox.io.RandomAccess;
 import org.apache.pdfbox.pdfparser.BaseParser;
 import org.apache.pdfbox.pdfparser.NonSequentialPDFParser;
@@ -1378,26 +1377,39 @@ public class PDDocument implements Pagea
         }
     }
 
-    /** 
-     * Save the pdf as incremental.
-     * 
-     * @param fileName the filename to be used
+    /**
+     * Save the pdf as incremental for signing. Use this only for small files
+     * because this method temporarly stores the entire file into memory.
+     *
+     * @param fileName the filename to be used. This should be a copy of the
+     * original file.
      * @throws IOException if something went wrong
-     * @throws COSVisitorException  if something went wrong
+     * @throws COSVisitorException if something went wrong
      */
-    public void saveIncremental( String fileName ) throws IOException, 
COSVisitorException
+    public void saveIncremental(String fileName) throws IOException, 
COSVisitorException
     {
-        saveIncremental(new BufferedInputStream(new 
FileInputStream(fileName)), 
-                new BufferedOutputStream(new FileOutputStream(fileName, 
true)));
+        FileInputStream fis = new FileInputStream(fileName);
+        byte[] ba = IOUtils.toByteArray(fis);
+        fis.close();
+        FileOutputStream fos = new FileOutputStream(fileName);
+        fos.write(ba);
+        fis = new FileInputStream(fileName);
+        saveIncremental(fis, fos);
     }
-    
-    /** 
-     * Save the pdf as incremental.
-     * 
-     * @param input 
-     * @param output
+
+    /**
+     * Save the pdf as incremental for signing. See the signature examples
+     * sources on how to use this.
+     *
+     * @param input. This must be a FileInputStream or it won't work. It should
+     * point to the same file than the output parameter.
+     * @param output. This must be a FileOutputStream or it won't work. It must
+     * be positioned at the end of the file, i.e. it should just have written
+     * the original file. The appending constructor of FileOutputStream has 
been
+     * found not to be working, so you need to write the whole file yourself.
+     *
      * @throws IOException if something went wrong
-     * @throws COSVisitorException  if something went wrong
+     * @throws COSVisitorException if something went wrong
      */
     public void saveIncremental(InputStream input, OutputStream output) throws 
IOException, COSVisitorException
     {


Reply via email to