pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1827818 - /pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
Date Tue, 27 Mar 2018 09:20:53 GMT
Author: msahyoun
Date: Tue Mar 27 09:20:52 2018
New Revision: 1827818

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

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java?rev=1827818&r1=1827817&r2=1827818&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/cos/COSDocument.java Tue Mar
27 09:20:52 2018
@@ -427,24 +427,75 @@ 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();
+                    COSStream cosStream = (COSStream) cosObject;
+                    try
+                    {
+                        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;
+            }
         }
     }
 



Mime
View raw message