You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2018/03/27 16:50:36 UTC

svn commit: r1827848 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java

Author: msahyoun
Date: Tue Mar 27 16:50:36 2018
New Revision: 1827848

URL: http://svn.apache.org/viewvc?rev=1827848&view=rev
Log:
PDFBOX-4158: try closing all open IO ressources even if there is an error when closing one of these

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java?rev=1827848&r1=1827847&r2=1827848&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDDocument.java Tue Mar 27 16:50:36 2018
@@ -1389,19 +1389,59 @@ public class PDDocument implements Close
     {
         if (!document.isClosed())
         {
+             // Make sure that:
+            // - first Exception is kept
+            // - all IO resources are closed
+            // - there's a way to see which errors occured
+
+            IOException firstException = null;
+
             // close resources and COSWriter
             if (signingSupport != null)
             {
-                signingSupport.close();
+                try
+                {
+                    signingSupport.close();
+                }
+                catch (IOException ioe)
+                {
+                    LOG.warn("Error closing SigningSupport", ioe);
+                    if (firstException == null)
+                    {
+                        firstException = ioe;
+                    }
+                }
             }
 
             // close all intermediate I/O streams
-            document.close();
+            try
+            {
+                document.close();
+            }
+            catch (IOException ioe)
+            {   
+                LOG.warn("Error closing COSDocument", ioe);
+                if (firstException == null)
+                {
+                    firstException = ioe;
+                }
+            }
             
             // close the source PDF stream, if we read from one
             if (pdfSource != null)
             {
-                pdfSource.close();
+                try
+                {
+                    pdfSource.close();
+                }
+                catch (IOException ioe)
+                {
+                    LOG.warn("Error closing RandomAccessRead pdfSource", ioe);
+                    if (firstException == null)
+                    {
+                        firstException = ioe;
+                    }
+                }
             }
         }
     }