]> source.dussan.org Git - poi.git/commitdiff
close input streams for parts
authorPJ Fanning <fanningpj@apache.org>
Tue, 28 Dec 2021 01:55:57 +0000 (01:55 +0000)
committerPJ Fanning <fanningpj@apache.org>
Tue, 28 Dec 2021 01:55:57 +0000 (01:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896461 13f79535-47bb-0310-9956-ffa450edef68

19 files changed:
poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java
poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/CalculationChain.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/ExternalLinksTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/MapInfo.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/SingleXmlCells.java
poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFPicture.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPicture.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCache.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheRecords.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
poi-ooxml/src/test/java/org/apache/poi/xssf/model/TestStylesTable.java

index 6b119d6b734b484634f70e4fab207f779f9dbb59..bc5642a9e66b9bd8fcdc7f9dc7e618b1815b6571 100644 (file)
@@ -43,10 +43,25 @@ import org.apache.poi.util.IOUtils;
 public final class PackageHelper {
 
     public static OPCPackage open(InputStream is) throws IOException {
+        return open(is, false);
+    }
+
+    /**
+     * @param stream
+     * @param closeStream whether to close the stream (default is false)
+     * @since POI 5.2.0
+     * @return OPCPackage
+     * @throws IOException
+     */
+    public static OPCPackage open(InputStream stream, boolean closeStream) throws IOException {
         try {
-            return OPCPackage.open(is);
+            return OPCPackage.open(stream);
         } catch (InvalidFormatException e){
             throw new POIXMLException(e);
+        } finally {
+            if (closeStream) {
+                stream.close();
+            }
         }
     }
 
index 344fab1554dc0672aa7408c2c4b2ce85e4348712..20bb719484ee32cd561e9b346464f63fda13574b 100644 (file)
@@ -22,6 +22,7 @@ package org.apache.poi.xddf.usermodel.chart;
 import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
 
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -147,7 +148,9 @@ public abstract class XDDFChart extends POIXMLDocumentPart implements TextContai
     protected XDDFChart(PackagePart part) throws IOException, XmlException {
         super(part);
 
-        chartSpace = ChartSpaceDocument.Factory.parse(part.getInputStream(), DEFAULT_XML_OPTIONS).getChartSpace();
+        try (InputStream stream = part.getInputStream()) {
+            chartSpace = ChartSpaceDocument.Factory.parse(stream, DEFAULT_XML_OPTIONS).getChartSpace();
+        }
     }
 
     /**
index 791a77c82854aa39c3720ef2d5717650c5eb3657..f658482b0979b31910ff2b8e96659a050444f96c 100644 (file)
@@ -77,7 +77,9 @@ public class XSSFBSharedStringsTable implements SharedStrings {
      * Like POIXMLDocumentPart constructor
      */
     XSSFBSharedStringsTable(PackagePart part) throws IOException, SAXException {
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
 
     private void readFrom(InputStream inputStream) throws IOException {
index ad962321f1ebc48bba32b3ad2a6e879449b7e4eb..1a3bafcac66a2c1b7e68ff8636b65e33dfa43ae3 100644 (file)
@@ -46,7 +46,9 @@ public class CalculationChain extends POIXMLDocumentPart {
      */
     public CalculationChain(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
 
     public void readFrom(InputStream is) throws IOException {
index f2685550a1b49edb282792ecc439d6328a41b3a3..2cd4d494a65e7cb9c15f3a1054b8eb10c2daa666 100644 (file)
@@ -76,7 +76,9 @@ public class CommentsTable extends POIXMLDocumentPart implements Comments {
      */
     public CommentsTable(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
     
     public void readFrom(InputStream is) throws IOException {
index 9d334b5dee0ffe78240958762e48c947b4616bee..21568d48970be768b0decf8e556539d87aa60b00 100644 (file)
@@ -56,7 +56,9 @@ public class ExternalLinksTable extends POIXMLDocumentPart {
      */
     public ExternalLinksTable(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
 
     public void readFrom(InputStream is) throws IOException {
index b70b4dbc684d97add3d1c835010b9094fbc57cae..09be0edf27d9ac48583b793729e138a2a5e53697 100644 (file)
@@ -63,7 +63,9 @@ public class MapInfo extends POIXMLDocumentPart {
      */
     public MapInfo(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
 
     public void readFrom(InputStream is) throws IOException {
index f1d59a14bb578f8c5719a498229882f65d808970..b83f5f297edda9110ad0ffdf2b7fd152247f41a3 100644 (file)
@@ -108,7 +108,9 @@ public class SharedStringsTable extends POIXMLDocumentPart implements SharedStri
      */
     public SharedStringsTable(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
 
     /**
index 8dad4297070b030049b724ee8861a19b30afe87f..f66d919677240f79e0e1247041ba3e348d440221 100644 (file)
@@ -56,7 +56,9 @@ public class SingleXmlCells extends POIXMLDocumentPart {
      */
     public SingleXmlCells(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
 
     public void readFrom(InputStream is) throws IOException {
index db9565347b5d8c5e56e81efae31bfccaaff3ebf5..9b2050e080dbfdda28db0696d6f37a693ac159f1 100644 (file)
@@ -155,7 +155,17 @@ public class StylesTable extends POIXMLDocumentPart implements Styles {
      */
     public StylesTable(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
+    }
+
+    /**
+     * @since POI 5.2.0
+     */
+    public StylesTable(InputStream stream) throws IOException {
+        super();
+        readFrom(stream);
     }
 
     public void setWorkbook(XSSFWorkbook wb) {
index 3f50a196fcc3ebb2524687a433278181fd5b430c..329125fc2878491ee8f3c24112cc0df3b7c80f09 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.xssf.streaming;
 
 import java.awt.Dimension;
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -218,8 +219,8 @@ public final class SXSSFPicture implements Picture {
      * @return image dimension in pixels
      */
     protected static Dimension getImageDimension(PackagePart part, int type){
-        try {
-            return ImageUtils.getImageDimension(part.getInputStream(), type);
+        try (InputStream stream = part.getInputStream()) {
+            return ImageUtils.getImageDimension(stream, type);
         } catch (IOException e){
             //return a "singulariry" if ImageIO failed to read the image
             LOG.atWarn().withThrowable(e).log("Failed to read image");
index 077d21e2e47f5b58cb942d9583d49008f66b9743..3980a7cb0012f5878af874c4461f657d8ae2ad35 100644 (file)
@@ -19,6 +19,7 @@ package org.apache.poi.xssf.usermodel;
 
 import java.awt.Dimension;
 import java.io.IOException;
+import java.io.InputStream;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
@@ -239,8 +240,8 @@ public final class XSSFPicture extends XSSFShape implements Picture {
      * @return image dimension in pixels
      */
     protected static Dimension getImageDimension(PackagePart part, int type){
-        try {
-            return ImageUtils.getImageDimension(part.getInputStream(), type);
+        try (InputStream stream = part.getInputStream()) {
+            return ImageUtils.getImageDimension(stream, type);
         } catch (IOException e){
             //return a "singulariry" if ImageIO failed to read the image
             LOG.atWarn().withThrowable(e).log("Failed to read image");
index fd0a24f40e357814dff26c48cfa452a6a0c78046..81984550c0c63c8c0ff29e05a59831c713629aad 100644 (file)
 ==================================================================== */
 package org.apache.poi.xssf.usermodel;
 
-import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
-
-import java.io.IOException;
-import java.io.InputStream;
-
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.util.Beta;
@@ -28,6 +23,11 @@ import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCache;
 
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
+
 public class XSSFPivotCache extends POIXMLDocumentPart {
 
     private CTPivotCache ctPivotCache;
@@ -55,16 +55,18 @@ public class XSSFPivotCache extends POIXMLDocumentPart {
     @Beta
     protected XSSFPivotCache(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
     
     @Beta
     protected void readFrom(InputStream is) throws IOException {
-    try {
-        XmlOptions options  = new XmlOptions(DEFAULT_XML_OPTIONS);
-        //Removing root element
-        options.setLoadReplaceDocumentElement(null);
-        ctPivotCache = CTPivotCache.Factory.parse(is, options);
+        try {
+            XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
+            //Removing root element
+            options.setLoadReplaceDocumentElement(null);
+            ctPivotCache = CTPivotCache.Factory.parse(is, options);
         } catch (XmlException e) {
             throw new IOException(e.getLocalizedMessage());
         }
index 0dabd263a1b68d705c8b3c1395162ebcabcdd5ba..4e3c8607e77e762b43489a2c5c75e6876ba01e8e 100644 (file)
@@ -65,7 +65,9 @@ public class XSSFPivotCacheDefinition extends POIXMLDocumentPart{
     @Beta
     protected XSSFPivotCacheDefinition(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
     
     @Beta
index d55b22479d955f124609bac278f8d27b94a87b9c..ddbe83e2f1483efa5f26575816d6a6818a4d2295 100644 (file)
@@ -52,7 +52,9 @@ public class XSSFPivotCacheRecords extends POIXMLDocumentPart {
     @Beta
     protected XSSFPivotCacheRecords(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
     
     @Beta
index 60ca7d0cd21261b124315790637ec75c03c3d877..d478320bebc0866b62dd12b6c35b579b0e0c2c84 100644 (file)
 ==================================================================== */
 package org.apache.poi.xssf.usermodel;
 
-import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
-
-import javax.xml.namespace.QName;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.ss.SpreadsheetVersion;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.CellType;
 import org.apache.poi.ss.usermodel.DataConsolidateFunction;
 import org.apache.poi.ss.usermodel.DataFormat;
 import org.apache.poi.ss.usermodel.Sheet;
@@ -41,26 +29,17 @@ import org.apache.poi.util.Beta;
 import org.apache.poi.util.Internal;
 import org.apache.xmlbeans.XmlException;
 import org.apache.xmlbeans.XmlOptions;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCacheSource;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColFields;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataField;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataFields;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTField;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItems;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTLocation;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageField;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageFields;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheDefinition;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotField;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFields;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableDefinition;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableStyle;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRowFields;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheetSource;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STDataConsolidateFunction;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType;
-import org.openxmlformats.schemas.spreadsheetml.x2006.main.STSourceType;
+import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
+
+import javax.xml.namespace.QName;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.apache.poi.ooxml.POIXMLTypeLoader.DEFAULT_XML_OPTIONS;
 
 public class XSSFPivotTable extends POIXMLDocumentPart {
 
@@ -95,13 +74,15 @@ public class XSSFPivotTable extends POIXMLDocumentPart {
     @Beta
     protected XSSFPivotTable(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
-    
+
     @Beta
     public void readFrom(InputStream is) throws IOException {
-    try {
-            XmlOptions options  = new XmlOptions(DEFAULT_XML_OPTIONS);
+        try {
+            XmlOptions options = new XmlOptions(DEFAULT_XML_OPTIONS);
             //Removing root element
             options.setLoadReplaceDocumentElement(null);
             pivotTableDefinition = CTPivotTableDefinition.Factory.parse(is, options);
index e140a1ec967bfd752292a574c316d7a5e6fc2a96..ff243e0e84c0d2f39a89de32ec9ef5fe8785a219 100644 (file)
@@ -81,7 +81,9 @@ public class XSSFTable extends POIXMLDocumentPart implements Table {
      */
     public XSSFTable(PackagePart part) throws IOException {
         super(part);
-        readFrom(part.getInputStream());
+        try (InputStream stream = part.getInputStream()) {
+            readFrom(stream);
+        }
     }
 
     /**
index efe4893c5a1983c90f7768c7a5c4a688efade7c9..c6edf23395bdf8782659152907a21eabb36864f4 100644 (file)
@@ -297,7 +297,11 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
      * input format
      */
     public XSSFWorkbook(InputStream is) throws IOException {
-        this(PackageHelper.open(is));
+        this(is, false);
+    }
+
+    private XSSFWorkbook(InputStream is, boolean closeStream) throws IOException {
+        this(PackageHelper.open(is, closeStream));
     }
 
     /**
@@ -353,7 +357,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
      * @since POI 4.0.0
      */
     public XSSFWorkbook(PackagePart part) throws IOException {
-        this(part.getInputStream());
+        this(part.getInputStream(), true);
     }
 
     /**
index a18313b577634a7910a463708e10658ae39b1ce6..14295640845ba90617f6e3650605c3b5eecdc271 100644 (file)
@@ -17,6 +17,9 @@
 
 package org.apache.poi.xssf.model;
 
+import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.xssf.usermodel.XSSFRelation;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
@@ -28,6 +31,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.Map;
 
 import org.apache.poi.ss.usermodel.BuiltinFormats;
@@ -91,6 +96,18 @@ public final class TestStylesTable {
         }
     }
 
+    @Test
+    void testLoadStream() throws IOException {
+        try (OPCPackage pkg = XSSFTestDataSamples.openSamplePackage(testFile)) {
+            ArrayList<PackagePart> parts = pkg.getPartsByContentType(XSSFRelation.STYLES.getContentType());
+            assertEquals(1, parts.size());
+            try (InputStream stream = parts.get(0).getInputStream()) {
+                StylesTable st = new StylesTable(stream);
+                doTestExisting(st);
+            }
+        }
+    }
+
     @Test
     void testLoadSaveLoad() throws IOException {
         try (XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook(testFile)) {