summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPJ Fanning <fanningpj@apache.org>2021-12-28 01:55:57 +0000
committerPJ Fanning <fanningpj@apache.org>2021-12-28 01:55:57 +0000
commit42a2794ae7c4e5a7da7fe2d4c05bc08ac4120adb (patch)
treef189a5a866b694098a1ba7736430e37807b44e6a
parent96d8e3048c398de19308f077f1dd0e0541a232ca (diff)
downloadpoi-42a2794ae7c4e5a7da7fe2d4c05bc08ac4120adb.tar.gz
poi-42a2794ae7c4e5a7da7fe2d4c05bc08ac4120adb.zip
close input streams for parts
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896461 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java17
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java5
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/model/CalculationChain.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/model/ExternalLinksTable.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/model/MapInfo.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/model/SingleXmlCells.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java12
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFPicture.java5
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPicture.java5
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCache.java24
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheRecords.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java53
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java4
-rw-r--r--poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java8
-rw-r--r--poi-ooxml/src/test/java/org/apache/poi/xssf/model/TestStylesTable.java17
19 files changed, 120 insertions, 66 deletions
diff --git a/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java b/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java
index 6b119d6b73..bc5642a9e6 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/ooxml/util/PackageHelper.java
@@ -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();
+ }
}
}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
index 344fab1554..20bb719484 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xddf/usermodel/chart/XDDFChart.java
@@ -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();
+ }
}
/**
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java
index 791a77c828..f658482b09 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBSharedStringsTable.java
@@ -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 {
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CalculationChain.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CalculationChain.java
index ad962321f1..1a3bafcac6 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CalculationChain.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CalculationChain.java
@@ -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 {
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java
index f2685550a1..2cd4d494a6 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/CommentsTable.java
@@ -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 {
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ExternalLinksTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ExternalLinksTable.java
index 9d334b5dee..21568d4897 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ExternalLinksTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/ExternalLinksTable.java
@@ -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 {
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/MapInfo.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/MapInfo.java
index b70b4dbc68..09be0edf27 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/MapInfo.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/MapInfo.java
@@ -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 {
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
index f1d59a14bb..b83f5f297e 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SharedStringsTable.java
@@ -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);
+ }
}
/**
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SingleXmlCells.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SingleXmlCells.java
index 8dad429707..f66d919677 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SingleXmlCells.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/SingleXmlCells.java
@@ -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 {
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java
index db9565347b..9b2050e080 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/model/StylesTable.java
@@ -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) {
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFPicture.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFPicture.java
index 3f50a196fc..329125fc28 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFPicture.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/streaming/SXSSFPicture.java
@@ -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");
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPicture.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPicture.java
index 077d21e2e4..3980a7cb00 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPicture.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPicture.java
@@ -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");
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCache.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCache.java
index fd0a24f40e..81984550c0 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCache.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCache.java
@@ -16,11 +16,6 @@
==================================================================== */
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());
}
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
index 0dabd263a1..4e3c8607e7 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheDefinition.java
@@ -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
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheRecords.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheRecords.java
index d55b22479d..ddbe83e2f1 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheRecords.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotCacheRecords.java
@@ -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
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
index 60ca7d0cd2..d478320beb 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFPivotTable.java
@@ -16,21 +16,9 @@
==================================================================== */
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);
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
index e140a1ec96..ff243e0e84 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFTable.java
@@ -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);
+ }
}
/**
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
index efe4893c5a..c6edf23395 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
@@ -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);
}
/**
diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/model/TestStylesTable.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/model/TestStylesTable.java
index a18313b577..1429564084 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/model/TestStylesTable.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/model/TestStylesTable.java
@@ -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;
@@ -92,6 +97,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)) {
assertNotNull(workbook.getStylesSource());