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
This commit is contained in:
Dominik Stadler 2015-12-22 22:36:43 +00:00
parent c9dee45744
commit a021bc780b
3 changed files with 20 additions and 5 deletions

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

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();

View File

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