From: Yegor Kozlov Date: Wed, 13 Jan 2010 20:24:15 +0000 (+0000) Subject: Avoid creating temporary files when opening OPC packages from input stream X-Git-Tag: REL_3_7_BETA1~138 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=691795a115016176691c61a6b6fab1d07bf7f628;p=poi.git Avoid creating temporary files when opening OPC packages from input stream git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@898927 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 986be3b352..033a74e370 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + Avoid creating temporary files when opening OPC packages from input stream Improved how HSMF handles multiple recipients Add PublisherTextExtractor support to ExtractorFactory Add XSLF support for text extraction from tables diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java index ee4bb53c7f..50d89918ac 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -173,23 +173,6 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{ */ public abstract List getAllEmbedds() throws OpenXML4JException; - /** - * YK: current implementation of OpenXML4J is funny. - * Packages opened by Package.open(InputStream is) are read-only, - * there is no way to change or even save such an instance in a OutputStream. - * The workaround is to create a copy via a temp file - */ - protected static OPCPackage ensureWriteAccess(OPCPackage pkg) throws IOException { - if(pkg.getPackageAccess() == PackageAccess.READ){ - try { - return PackageHelper.clone(pkg); - } catch (OpenXML4JException e){ - throw new POIXMLException(e); - } - } - return pkg; - } - protected final void load(POIXMLFactory factory) throws IOException { Map context = new HashMap(); try { diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index 2c0ae83d31..e67055838d 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -217,7 +217,7 @@ public abstract class OPCPackage implements RelationshipSource { */ public static OPCPackage open(InputStream in) throws InvalidFormatException, IOException { - OPCPackage pack = new ZipPackage(in, PackageAccess.READ); + OPCPackage pack = new ZipPackage(in, PackageAccess.READ_WRITE); if (pack.partList == null) { pack.getParts(); } diff --git a/src/ooxml/java/org/apache/poi/util/PackageHelper.java b/src/ooxml/java/org/apache/poi/util/PackageHelper.java index 5d8d9ade75..f4ab052055 100644 --- a/src/ooxml/java/org/apache/poi/util/PackageHelper.java +++ b/src/ooxml/java/org/apache/poi/util/PackageHelper.java @@ -34,23 +34,9 @@ import java.net.URI; */ public final class PackageHelper { - /** - * Clone the specified package. - * - * @param pkg the package to clone - * @return the cloned package - */ - public static OPCPackage clone(OPCPackage pkg) throws OpenXML4JException, IOException { - return clone(pkg, createTempFile()); - } - public static OPCPackage open(InputStream is) throws IOException { - File file = TempFile.createTempFile("poi-ooxml-", ".tmp"); - FileOutputStream out = new FileOutputStream(file); - IOUtils.copy(is, out); - out.close(); try { - return OPCPackage.open(file.getAbsolutePath()); + return OPCPackage.open(is); } catch (InvalidFormatException e){ throw new POIXMLException(e); } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index fc7fec6285..e05e57ec11 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -166,7 +166,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, IterablePackage object. */ public XSSFWorkbook(OPCPackage pkg) throws IOException { - super(ensureWriteAccess(pkg)); + super(pkg); //build a tree of POIXMLDocumentParts, this workbook being the root load(XSSFFactory.getInstance()); @@ -174,7 +174,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable