mirror of
https://github.com/apache/poi.git
synced 2026-02-27 20:40:08 +08:00
close input streams for parts
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1896461 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
96d8e3048c
commit
42a2794ae7
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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");
|
||||
|
||||
@ -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());
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -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)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user