diff options
5 files changed, 54 insertions, 14 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); diff --git a/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java index 2d90b46f20..8cad08609d 100644 --- a/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java +++ b/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java @@ -240,8 +240,6 @@ public final class POIXMLExtractorFactory implements ExtractorProvider { } return null; - } catch (IOException e) { - throw e; } catch (Error | RuntimeException | XmlException | OpenXML4JException e) { // NOSONAR throw new IOException(e); } @@ -271,6 +269,13 @@ public final class POIXMLExtractorFactory implements ExtractorProvider { } try (InputStream is = dec.getDataStream(poifsDir)) { return create(is, password); + } finally { + // we should close the underlying file-system as all information + // is read now and we should make sure that resources are freed + POIFSFileSystem fs = poifsDir.getFileSystem(); + if (fs != null) { + fs.close(); + } } } catch (IOException | RuntimeException e) { throw e; diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java index c7584ef978..849f656d1c 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java @@ -20,6 +20,7 @@ package org.apache.poi.ss.usermodel; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.IOException; import java.util.Calendar; import java.util.Date; import java.util.Locale; @@ -345,19 +346,20 @@ public abstract class BaseTestSheetAutosizeColumn { @Test - public void testExcelExporter() { - final Workbook wb = _testDataProvider.createWorkbook(); - final Sheet sheet = wb.createSheet("test"); - trackColumnsForAutoSizingIfSXSSF(sheet); - final Row row = sheet.createRow(0); - final Cell cell = row.createCell(0); + public void testExcelExporter() throws IOException { + try (final Workbook wb = _testDataProvider.createWorkbook()) { + final Sheet sheet = wb.createSheet("test"); + trackColumnsForAutoSizingIfSXSSF(sheet); + final Row row = sheet.createRow(0); + final Cell cell = row.createCell(0); - CellStyle csDateTime = wb.createCellStyle(); - csDateTime.setAlignment(HorizontalAlignment.LEFT); + CellStyle csDateTime = wb.createCellStyle(); + csDateTime.setAlignment(HorizontalAlignment.LEFT); - cell.setCellValue(new Date(Long.parseLong("1439800763994"))); - cell.setCellStyle(csDateTime); + cell.setCellValue(new Date(Long.parseLong("1439800763994"))); + cell.setCellStyle(csDateTime); - sheet.autoSizeColumn(0); + sheet.autoSizeColumn(0); + } } }
\ No newline at end of file |