From a021bc780b76f2cbf0eec33c97afca860519d63a Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Tue, 22 Dec 2015 22:36:43 +0000 Subject: [PATCH] Fix some cases where file resources were not closed correctly, mostly when Exceptions occur during opening files git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1721470 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/sl/usermodel/SlideShowFactory.java | 13 ++++++++++--- .../apache/poi/ss/usermodel/WorkbookFactory.java | 11 +++++++++-- .../poi/hslf/extractor/PowerPointExtractor.java | 1 + 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java index 7fde64dee..8b7b011e5 100644 --- a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java +++ b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java @@ -157,7 +157,6 @@ public class SlideShowFactory { * @throws IOException if an error occurs while reading the data * @throws EncryptedDocumentException If the wrong password is given for a protected file */ - @SuppressWarnings("resource") public static SlideShow create(InputStream inp, String password) throws IOException, EncryptedDocumentException { // If clearly doesn't do mark/reset, wrap up if (! inp.markSupported()) { @@ -231,17 +230,25 @@ public class SlideShowFactory { * @throws IOException if an error occurs while reading the data * @throws EncryptedDocumentException If the wrong password is given for a protected file */ - @SuppressWarnings("resource") public static SlideShow create(File file, String password, boolean readOnly) throws IOException, EncryptedDocumentException { if (!file.exists()) { throw new FileNotFoundException(file.toString()); } + NPOIFSFileSystem fs = null; try { - NPOIFSFileSystem fs = new NPOIFSFileSystem(file, readOnly); + fs = new NPOIFSFileSystem(file, readOnly); return create(fs, password); } catch(OfficeXmlFileException e) { + if(fs != null) { + fs.close(); + } return createXSLFSlideShow(file, readOnly); + } catch(RuntimeException e) { + if(fs != null) { + fs.close(); + } + throw e; } } diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index 1817b176a..fe9af6cf5 100644 --- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -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(); diff --git a/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java b/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java index 0babb76ae..1ce8029f4 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java @@ -105,6 +105,7 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor { */ public PowerPointExtractor(NPOIFSFileSystem fs) throws IOException { this(fs.getRoot()); + setFilesystem(fs); } /**