poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1885229 - in /poi/trunk/src: java/org/apache/poi/hssf/extractor/ java/org/apache/poi/hssf/usermodel/ ooxml/java/org/apache/poi/ooxml/extractor/ testcases/org/apache/poi/ss/usermodel/
Date Thu, 07 Jan 2021 06:52:57 GMT
Author: centic
Date: Thu Jan  7 06:52:57 2021
New Revision: 1885229

URL: http://svn.apache.org/viewvc?rev=1885229&view=rev
Log:
Properly close file handles in extractors and when handling password-protected files

Modified:
    poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java
    poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
    poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java
    poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java

Modified: poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java?rev=1885229&r1=1885228&r2=1885229&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/extractor/EventBasedExcelExtractor.java Thu Jan
 7 06:52:57 2021
@@ -291,4 +291,20 @@ public class EventBasedExcelExtractor im
     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();
+            }
+        }
+    }
 }

Modified: poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java?rev=1885229&r1=1885228&r2=1885229&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/extractor/OldExcelExtractor.java Thu Jan  7 06:52:57
2021
@@ -101,15 +101,22 @@ public class OldExcelExtractor implement
             // 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);
     }
 

Modified: poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java?rev=1885229&r1=1885228&r2=1885229&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java (original)
+++ poi/trunk/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbookFactory.java Thu Jan  7 06:52:57
2021
@@ -100,7 +100,17 @@ public class HSSFWorkbookFactory impleme
             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);

Modified: poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java?rev=1885229&r1=1885228&r2=1885229&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/ooxml/extractor/POIXMLExtractorFactory.java Thu
Jan  7 06:52:57 2021
@@ -240,8 +240,6 @@ public final class POIXMLExtractorFactor
             }
 
             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 POIXMLExtractorFactor
                 }
                 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;

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java?rev=1885229&r1=1885228&r2=1885229&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheetAutosizeColumn.java Thu
Jan  7 06:52:57 2021
@@ -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 BaseTestSheetAutos
 
 
     @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



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message