Browse Source

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
tags/REL_3_14_FINAL
Dominik Stadler 8 years ago
parent
commit
f2013022e8

+ 10
- 3
src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java View File

@@ -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;
}
}

+ 9
- 2
src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java View 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();

+ 1
- 0
src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java View File

@@ -105,6 +105,7 @@ public final class PowerPointExtractor extends POIOLE2TextExtractor {
*/
public PowerPointExtractor(NPOIFSFileSystem fs) throws IOException {
this(fs.getRoot());
setFilesystem(fs);
}

/**

Loading…
Cancel
Save