diff options
author | Dominik Stadler <centic@apache.org> | 2021-01-07 06:52:57 +0000 |
---|---|---|
committer | Dominik Stadler <centic@apache.org> | 2021-01-07 06:52:57 +0000 |
commit | 4bc450ef5f7544d9918d0f560e7b5f4356ae1b81 (patch) | |
tree | 9c10a255995e8d58e764c6eca91508cb8a7b7ad8 /src/java | |
parent | 1882dc74c992997994860899e32900a79deab002 (diff) | |
download | poi-4bc450ef5f7544d9918d0f560e7b5f4356ae1b81.tar.gz poi-4bc450ef5f7544d9918d0f560e7b5f4356ae1b81.zip |
Properly close file handles in extractors and when handling password-protected files
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1885229 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java')
3 files changed, 34 insertions, 1 deletions
diff --git a/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java index 68c69cf1d3..a3239def70 100644 --- a/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java @@ -291,4 +291,20 @@ public class EventBasedExcelExtractor implements POIOLE2TextExtractor, org.apach public DirectoryEntry getRoot() { return _dir; } + + @Override + public void close() throws IOException { + // first perform the default close + POIOLE2TextExtractor.super.close(); + + // also ensure that an underlying DirectoryNode + // is closed properly to avoid leaking file-handles + DirectoryEntry root = getRoot(); + if (root instanceof DirectoryNode) { + Closeable fs = ((DirectoryNode) root).getFileSystem(); + if (isCloseFilesystem() && fs != null) { + fs.close(); + } + } + } } diff --git a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java index 56b1424b3d..c93482f901 100644 --- a/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java +++ b/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java @@ -101,15 +101,22 @@ public class OldExcelExtractor implements POITextExtractor { // ensure that the stream is properly closed here if an Exception // is thrown while opening biffStream.close(); + + toClose.close(); + throw e; } } public OldExcelExtractor(POIFSFileSystem fs) throws IOException { + toClose = fs; + open(fs); } public OldExcelExtractor(DirectoryNode directory) throws IOException { + toClose = directory.getFileSystem(); + open(directory); } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java index 0c4c98a7c2..b43a45de6a 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java @@ -100,7 +100,17 @@ public class HSSFWorkbookFactory implements WorkbookProvider { passwordSet = true; } try { - return new HSSFWorkbook(new POIFSFileSystem(file, readOnly), true); + POIFSFileSystem fs = new POIFSFileSystem(file, readOnly); + try { + return new HSSFWorkbook(fs, true); + } catch (RuntimeException e) { + // we need to close the filesystem + // if we encounter an exception to + // not leak file handles + fs.close(); + + throw e; + } } finally { if (passwordSet) { Biff8EncryptionKey.setCurrentUserPassword(null); |