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();
+ }
}
}
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;
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();
+ }
}
/**
* 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 {
*/
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 {
*/
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 {
*/
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 {
*/
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 {
*/
public SharedStringsTable(PackagePart part) throws IOException {
super(part);
- readFrom(part.getInputStream());
+ try (InputStream stream = part.getInputStream()) {
+ readFrom(stream);
+ }
}
/**
*/
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 {
*/
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) {
import java.awt.Dimension;
import java.io.IOException;
+import java.io.InputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
* @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");
import java.awt.Dimension;
import java.io.IOException;
+import java.io.InputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
* @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");
==================================================================== */
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;
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;
@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());
}
@Beta
protected XSSFPivotCacheDefinition(PackagePart part) throws IOException {
super(part);
- readFrom(part.getInputStream());
+ try (InputStream stream = part.getInputStream()) {
+ readFrom(stream);
+ }
}
@Beta
@Beta
protected XSSFPivotCacheRecords(PackagePart part) throws IOException {
super(part);
- readFrom(part.getInputStream());
+ try (InputStream stream = part.getInputStream()) {
+ readFrom(stream);
+ }
}
@Beta
==================================================================== */
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;
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 {
@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);
*/
public XSSFTable(PackagePart part) throws IOException {
super(part);
- readFrom(part.getInputStream());
+ try (InputStream stream = part.getInputStream()) {
+ readFrom(stream);
+ }
}
/**
* 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));
}
/**
* @since POI 4.0.0
*/
public XSSFWorkbook(PackagePart part) throws IOException {
- this(part.getInputStream());
+ this(part.getInputStream(), true);
}
/**
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;
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;
}
}
+ @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)) {