]> source.dussan.org Git - poi.git/commitdiff
Fix some cases where file resources were not closed correctly, mostly when Exceptions...
authorDominik Stadler <centic@apache.org>
Tue, 22 Dec 2015 22:36:43 +0000 (22:36 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 22 Dec 2015 22:36:43 +0000 (22:36 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1721470 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java
src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java
src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java

index 7fde64dee88442147c5eec5b3299e8d1859916d7..8b7b011e51a12aec36fbbe2f2648aa3937a36a89 100644 (file)
@@ -157,7 +157,6 @@ public class SlideShowFactory {
      *  @throws IOException if an error occurs while reading the data\r
      *  @throws EncryptedDocumentException If the wrong password is given for a protected file\r
      */\r
-    @SuppressWarnings("resource")\r
     public static SlideShow<?,?> create(InputStream inp, String password) throws IOException, EncryptedDocumentException {\r
         // If clearly doesn't do mark/reset, wrap up\r
         if (! inp.markSupported()) {\r
@@ -231,17 +230,25 @@ public class SlideShowFactory {
      *  @throws IOException if an error occurs while reading the data\r
      *  @throws EncryptedDocumentException If the wrong password is given for a protected file\r
      */\r
-    @SuppressWarnings("resource")\r
     public static SlideShow<?,?> create(File file, String password, boolean readOnly) throws IOException, EncryptedDocumentException {\r
         if (!file.exists()) {\r
             throw new FileNotFoundException(file.toString());\r
         }\r
 \r
+        NPOIFSFileSystem fs = null;\r
         try {\r
-            NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly);\r
+            fs = new NPOIFSFileSystem(file, readOnly);\r
             return create(fs, password);\r
         } catch(OfficeXmlFileException e) {\r
+            if(fs != null) {\r
+                fs.close();\r
+            }\r
             return createXSLFSlideShow(file, readOnly);\r
+        } catch(RuntimeException e) {\r
+            if(fs != null) {\r
+                fs.close();\r
+            }\r
+            throw e;\r
         }\r
     }\r
     \r
index 1817b176a235ad50301dc0ca2d9d4f226c32eecc..fe9af6cf5e84f9f7dd0c76d5a7cfe234a150f016 100644 (file)
@@ -278,7 +278,14 @@ public class WorkbookFactory {
 
         try {
             NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly);
-            return create(fs, password);
+            try {
+                return create(fs, password);
+            } catch (RuntimeException e) {
+                // ensure that the file-handle is closed again
+                fs.close();
+                
+                throw e;
+            }
         } catch(OfficeXmlFileException e) {
             // opening as .xls failed => try opening as .xlsx
             OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE);
@@ -291,7 +298,7 @@ public class WorkbookFactory {
 
                 // rethrow exception
                 throw ioe;
-            } catch (IllegalArgumentException ioe) {
+            } catch (RuntimeException ioe) {
                 // ensure that file handles are closed (use revert() to not re-write the file)
                 pkg.revert();
                 //pkg.close();
index 0babb76ae2adb22a3963e20dde3595e8542db655..1ce8029f47ba00431671e469b1ac6ad5297e0da9 100644 (file)
@@ -105,6 +105,7 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor {
     */
    public PowerPointExtractor(NPOIFSFileSystem fs) throws IOException {
       this(fs.getRoot());
+      setFilesystem(fs);
    }
 
    /**