poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kiwiwi...@apache.org
Subject svn commit: r1747092 - /poi/trunk/src/java/org/apache/poi/POIDocument.java
Date Mon, 06 Jun 2016 23:34:17 GMT
Author: kiwiwings
Date: Mon Jun  6 23:34:17 2016
New Revision: 1747092

URL: http://svn.apache.org/viewvc?rev=1747092&view=rev
Log:
close resources

Modified:
    poi/trunk/src/java/org/apache/poi/POIDocument.java

Modified: poi/trunk/src/java/org/apache/poi/POIDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/POIDocument.java?rev=1747092&r1=1747091&r2=1747092&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/POIDocument.java (original)
+++ poi/trunk/src/java/org/apache/poi/POIDocument.java Mon Jun  6 23:34:17 2016
@@ -70,18 +70,24 @@ public abstract class POIDocument {
 
     /**
      * Constructs from an old-style OPOIFS
+     * 
+     * @param fs the filesystem the document is read from
      */
     protected POIDocument(OPOIFSFileSystem fs) {
        this(fs.getRoot());
     }
     /**
      * Constructs from an old-style OPOIFS
+     * 
+     * @param fs the filesystem the document is read from
      */
     protected POIDocument(NPOIFSFileSystem fs) {
        this(fs.getRoot());
     }
     /**
      * Constructs from the default POIFS
+     * 
+     * @param fs the filesystem the document is read from
      */
     protected POIDocument(POIFSFileSystem fs) {
         this(fs.getRoot());
@@ -180,49 +186,46 @@ public abstract class POIDocument {
         DirectoryNode dirNode = directory;
         
         NPOIFSFileSystem encPoifs = null;
-        if (encryptionInfo != null) {
-            try {
+        String step = "getting";
+        try {
+            if (encryptionInfo != null) {
+                step = "getting encrypted";
                 InputStream is = encryptionInfo.getDecryptor().getDataStream(directory);
-                encPoifs = new NPOIFSFileSystem(is);
-                is.close();
-                dirNode = encPoifs.getRoot();
-            } catch (Exception e) {
-                logger.log(POILogger.ERROR, "Error getting encrypted property set with name
" + setName, e);
+                try {
+                    encPoifs = new NPOIFSFileSystem(is);
+                    dirNode = encPoifs.getRoot();
+                } finally {
+                    is.close();
+                }
+            }
+            
+            //directory can be null when creating new documents
+            if (dirNode == null || !dirNode.hasEntry(setName)) {
                 return null;
             }
-        }
-        
-        //directory can be null when creating new documents
-        if (dirNode == null || !dirNode.hasEntry(setName)) 
-            return null;
-
-        DocumentInputStream dis;
-        try {
+    
             // Find the entry, and get an input stream for it
-            dis = dirNode.createDocumentInputStream( dirNode.getEntry(setName) );
-        } catch(IOException ie) {
-            // Oh well, doesn't exist
-            logger.log(POILogger.WARN, "Error getting property set with name " + setName
+ "\n" + ie);
+            step = "getting";
+            DocumentInputStream dis = dirNode.createDocumentInputStream( dirNode.getEntry(setName)
);
+            try {
+                // Create the Property Set
+                step = "creating";
+                return PropertySetFactory.create(dis);
+            } finally {
+                dis.close();
+            }
+        } catch (Exception e) {
+            logger.log(POILogger.WARN, "Error "+step+" property set with name " + setName,
e);
             return null;
-        }
-
-        try {
-            // Create the Property Set
-            PropertySet set = PropertySetFactory.create(dis);
-            // Tidy up if needed
+        } finally {
             if (encPoifs != null) {
-                encPoifs.close();
+                try {
+                    encPoifs.close();
+                } catch(IOException e) {
+                    logger.log(POILogger.WARN, "Error closing encrypted property poifs",
e);
+                }
             }
-            // Return the properties
-            return set;
-        } catch(IOException ie) {
-            // Must be corrupt or something like that
-            logger.log(POILogger.WARN, "Error creating property set with name " + setName
+ "\n" + ie);
-        } catch(org.apache.poi.hpsf.HPSFException he) {
-            // Oh well, doesn't exist
-            logger.log(POILogger.WARN, "Error creating property set with name " + setName
+ "\n" + he);
         }
-        return null;
     }
     
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message