Author: tilman
Date: Wed Feb 12 08:25:46 2025
New Revision: 1923750

URL: http://svn.apache.org/viewvc?rev=1923750&view=rev
Log:
PDFBOX-5950: close source immediately, as suggested by asdpboy

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

Modified: 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
URL: 
http://svn.apache.org/viewvc/pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java?rev=1923750&r1=1923749&r2=1923750&view=diff
==============================================================================
--- 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
 (original)
+++ 
pdfbox/branches/3.0/pdfbox/src/main/java/org/apache/pdfbox/multipdf/PDFMergerUtility.java
 Wed Feb 12 08:25:46 2025
@@ -451,15 +451,13 @@ public class PDFMergerUtility
             // - all PDDocuments are closed
             // - all FileInputStreams are closed
             // - there's a way to see which errors occurred
-
-            List<PDDocument> tobeclosed = new ArrayList<>(sources.size());
             StreamCacheCreateFunction strmCacheFunc = 
streamCacheCreateFunction != null ? streamCacheCreateFunction
                     : IOUtils.createMemoryOnlyStreamCache();
             try (PDDocument destination = new PDDocument(strmCacheFunc))
             {
                 for (Object sourceObject : sources)
                 {
-                    PDDocument sourceDoc = null;
+                    PDDocument sourceDoc;
                     if (sourceObject instanceof File)
                     {
                         sourceDoc = Loader.loadPDF((File) sourceObject);
@@ -468,8 +466,14 @@ public class PDFMergerUtility
                     {
                         sourceDoc = Loader.loadPDF((RandomAccessRead) 
sourceObject);
                     }
-                    tobeclosed.add(sourceDoc);
-                    appendDocument(destination, sourceDoc);
+                    try
+                    {
+                        appendDocument(destination, sourceDoc);
+                    }
+                    finally
+                    {
+                        IOUtils.closeAndLogException(sourceDoc, LOG, 
"PDDocument", null);
+                    }
                 }
                 
                 // optionally set meta data
@@ -491,13 +495,6 @@ public class PDFMergerUtility
                     destination.save(destinationStream, compressParameters);
                 }
             }
-            finally
-            {
-                for (PDDocument doc : tobeclosed)
-                {
-                    IOUtils.closeAndLogException(doc, LOG, "PDDocument", null);
-                }
-            }
         }
     }
 


Reply via email to