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 09:20:36 UTC

svn commit: r1827817 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java

Author: msahyoun
Date: Tue Mar 27 09:20:36 2018
New Revision: 1827817

URL: http://svn.apache.org/viewvc?rev=1827817&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/cos/COSDocument.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1827817&r1=1827816&r2=1827817&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Tue Mar 27 09:20:36 2018
@@ -424,24 +424,74 @@ public class COSDocument extends COSBase
     {
         if (!closed)
         {
+            // Make sure that:
+            // - first Exception is kept
+            // - all COSStreams are closed
+            // - ScratchFile is closed
+            // - there's a way to see which errors occured
+
+            IOException firstException = null;
+
             // close all open I/O streams
             for (COSObject object : getObjects())
             {
                 COSBase cosObject = object.getObject();
                 if (cosObject instanceof COSStream)
                 {
-                    ((COSStream) cosObject).close();
+                    try (COSStream cosStream = (COSStream) cosObject)
+                    {
+                        cosStream.close();
+                    }
+                    catch (IOException ioe)
+                    {
+                        LOG.warn("Error closing COSStream", ioe);
+                        if (firstException == null)
+                        {
+                            firstException = ioe;
+                        }
+
+                    }
                 }
             }
             for (COSStream stream : streams)
             {
-                stream.close();
+                try
+                {
+                    stream.close();
+                }
+                catch (IOException ioe)
+                {
+                    LOG.warn("Error closing COSStream", ioe);
+                    if (firstException == null)
+                    {
+                        firstException = ioe;
+                    }
+
+                }
             }
             if (scratchFile != null)
             {
-                scratchFile.close();
+                try
+                {
+                    scratchFile.close();
+                }
+                catch (IOException ioe)
+                {
+                    LOG.warn("Error closing ScratchFile", ioe);
+                    if (firstException == null)
+                    {
+                        firstException = ioe;
+                    }
+
+                }
             }
             closed = true;
+
+            // rethrow first exception to keep method contract
+            if (firstException != null)
+            {
+                throw firstException;
+            }
         }
     }