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 2018/08/20 17:19:50 UTC

svn commit: r1838442 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java

Author: lehmi
Date: Mon Aug 20 17:19:49 2018
New Revision: 1838442

URL: http://svn.apache.org/viewvc?rev=1838442&view=rev
Log:
PDFBOX-4295: don't create intermediate streams when merging files

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1838442&r1=1838441&r2=1838442&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java Mon Aug 20 17:19:49 2018
@@ -81,8 +81,7 @@ public class PDFMergerUtility
      */
     private static final Log LOG = LogFactory.getLog(PDFMergerUtility.class);
 
-    private final List<InputStream> sources;
-    private final List<FileInputStream> fileInputStreams;
+    private final List<Object> sources;
     private String destinationFileName;
     private OutputStream destinationStream;
     private boolean ignoreAcroFormErrors = false;
@@ -117,8 +116,7 @@ public class PDFMergerUtility
      */
     public PDFMergerUtility()
     {
-        sources = new ArrayList<InputStream>();
-        fileInputStreams = new ArrayList<FileInputStream>();
+        sources = new ArrayList<Object>();
     }
 
     /**
@@ -228,7 +226,6 @@ public class PDFMergerUtility
     {
         FileInputStream stream = new FileInputStream(source);
         sources.add(stream);
-        fileInputStreams.add(stream);
     }
 
     /**
@@ -285,7 +282,8 @@ public class PDFMergerUtility
         }
     }
 
-    private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting, List<InputStream> sourceDocuments) throws IOException
+    private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting,
+            List<Object> sourceDocuments) throws IOException
     {
         PDDocument destination = null;
         try
@@ -293,12 +291,19 @@ public class PDFMergerUtility
             destination = new PDDocument(memUsageSetting);
             PDFCloneUtility cloner = new PDFCloneUtility(destination);
 
-            for (InputStream sourceInputStream : sources)
+            for (Object sourceObject : sources)
             {
                 PDDocument sourceDoc = null;
                 try
                 {
-                    sourceDoc = PDDocument.load(sourceInputStream, memUsageSetting);
+                    if (sourceObject instanceof File)
+                    {
+                        sourceDoc = PDDocument.load((File) sourceObject, memUsageSetting);
+                    }
+                    else
+                    {
+                        sourceDoc = PDDocument.load((InputStream) sourceObject, memUsageSetting);
+                    }
 
                     for (PDPage page : sourceDoc.getPages())
                     {
@@ -318,13 +323,11 @@ public class PDFMergerUtility
                         }
                         destination.addPage(newPage);
                     }
-                    sourceDoc.close();
                 }
                 finally
                 {
                     IOUtils.closeQuietly(sourceDoc);
                 }
-                sourceInputStream.close();
             }
             
             if (destinationStream == null)
@@ -354,8 +357,6 @@ public class PDFMergerUtility
     private void legacyMergeDocuments(MemoryUsageSetting memUsageSetting) throws IOException
     {
         PDDocument destination = null;
-        InputStream sourceFile;
-        PDDocument source;
         if (sources != null && sources.size() > 0)
         {
             // Make sure that:
@@ -374,9 +375,18 @@ public class PDFMergerUtility
                         MemoryUsageSetting.setupMainMemoryOnly();
                 destination = new PDDocument(partitionedMemSetting);
 
-                for (InputStream sourceInputStream : sources)
+                for (Object sourceObject : sources)
                 {
-                    PDDocument sourceDoc = PDDocument.load(sourceInputStream, partitionedMemSetting);
+                    PDDocument sourceDoc = null;
+                    if (sourceObject instanceof File)
+                    {
+                        sourceDoc = PDDocument.load((File) sourceObject, partitionedMemSetting);
+                    }
+                    else
+                    {
+                        sourceDoc = PDDocument.load((InputStream) sourceObject,
+                                partitionedMemSetting);
+                    }
                     tobeclosed.add(sourceDoc);
                     appendDocument(destination, sourceDoc);
                 }
@@ -411,11 +421,6 @@ public class PDFMergerUtility
                 {
                     IOUtils.closeAndLogException(doc, LOG, "PDDocument", null);
                 }
-
-                for (FileInputStream stream : fileInputStreams)
-                {
-                    IOUtils.closeAndLogException(stream, LOG, "FileInputStream", null);
-                }
             }
         }
     }