pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1838439 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
Date Mon, 20 Aug 2018 17:08:19 GMT
Author: lehmi
Date: Mon Aug 20 17:08:19 2018
New Revision: 1838439

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

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

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=1838439&r1=1838438&r2=1838439&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
Aug 20 17:08:19 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;
@@ -137,7 +136,6 @@ public class PDFMergerUtility
     public PDFMergerUtility()
     {
         sources = new ArrayList<>();
-        fileInputStreams = new ArrayList<>();
     }
     
     /**
@@ -267,7 +265,6 @@ public class PDFMergerUtility
     {
         FileInputStream stream = new FileInputStream(source);
         sources.add(stream);
-        fileInputStreams.add(stream);
     }
 
     /**
@@ -324,36 +321,44 @@ public class PDFMergerUtility
         }
     }
     
-    private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting, List<InputStream>
sourceDocuments) throws IOException
+    private void optimizedMergeDocuments(MemoryUsageSetting memUsageSetting,
+            List<Object> sourceDocuments) throws IOException
     {
         try (PDDocument destination = new PDDocument(memUsageSetting))
         {
             PDFCloneUtility cloner = new PDFCloneUtility(destination);
-            for (InputStream sourceInputStream : sources)
+            for (Object sourceObject : sources)
             {
-                try (PDDocument sourceDoc = PDDocument.load(sourceInputStream, memUsageSetting))
+                PDDocument sourceDoc = null;
+                if (sourceObject instanceof File)
                 {
-                    for (PDPage page : sourceDoc.getPages())
+                    sourceDoc = PDDocument.load((File) sourceObject, memUsageSetting);
+                }
+                else
+                {
+                    sourceDoc = PDDocument.load((InputStream) sourceObject, memUsageSetting);
+                }
+                for (PDPage page : sourceDoc.getPages())
+                {
+                    PDPage newPage = new PDPage(
+                            (COSDictionary) cloner.cloneForNewDocument(page.getCOSObject()));
+                    newPage.setCropBox(page.getCropBox());
+                    newPage.setMediaBox(page.getMediaBox());
+                    newPage.setRotation(page.getRotation());
+                    PDResources resources = page.getResources();
+                    if (resources != null)
+                    {
+                        // this is smart enough to just create references for resources that
are used on multiple pages
+                        newPage.setResources(new PDResources(
+                                (COSDictionary) cloner.cloneForNewDocument(resources)));
+                    }
+                    else
                     {
-                        PDPage newPage = new PDPage((COSDictionary) cloner.cloneForNewDocument(page.getCOSObject()));
-                        newPage.setCropBox(page.getCropBox());
-                        newPage.setMediaBox(page.getMediaBox());
-                        newPage.setRotation(page.getRotation());
-                        PDResources resources = page.getResources();
-                        if (resources != null)
-                        {
-                            // this is smart enough to just create references for resources
that are used on multiple pages
-                            newPage.setResources(new PDResources((COSDictionary) cloner.cloneForNewDocument(resources)));
-                        }
-                        else
-                        {
-                            newPage.setResources(new PDResources());
-                        }
-                        destination.addPage(newPage);
+                        newPage.setResources(new PDResources());
                     }
-                    sourceDoc.close();
+                    destination.addPage(newPage);
                 }
-                sourceInputStream.close();
+                sourceDoc.close();
             }
             
             if (destinationStream == null)
@@ -393,9 +398,18 @@ public class PDFMergerUtility
                     MemoryUsageSetting.setupMainMemoryOnly();
             try (PDDocument 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);
                 }
@@ -425,11 +439,6 @@ public class PDFMergerUtility
                 {
                     IOUtils.closeAndLogException(doc, LOG, "PDDocument", null);
                 }
-
-                for (FileInputStream stream : fileInputStreams)
-                {
-                    IOUtils.closeAndLogException(stream, LOG, "FileInputStream", null);
-                }
             }
         }
     }



Mime
View raw message