]> source.dussan.org Git - poi.git/commitdiff
Avoid creating temporary files when opening OPC packages from input stream
authorYegor Kozlov <yegor@apache.org>
Wed, 13 Jan 2010 20:24:15 +0000 (20:24 +0000)
committerYegor Kozlov <yegor@apache.org>
Wed, 13 Jan 2010 20:24:15 +0000 (20:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@898927 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/POIXMLDocument.java
src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
src/ooxml/java/org/apache/poi/util/PackageHelper.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java

index 986be3b3523d20e12fc9e4e69d2a6fce5a128d1f..033a74e37093dc89f620aafee6b1733b114b62a4 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-SNAPSHOT" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="add">Avoid creating temporary files when opening OPC packages from input stream</action>
            <action dev="POI-DEVELOPERS" type="add">Improved how HSMF handles multiple recipients</action>
            <action dev="POI-DEVELOPERS" type="add">Add PublisherTextExtractor support to ExtractorFactory</action>
            <action dev="POI-DEVELOPERS" type="add">Add XSLF support for text extraction from tables</action>
index ee4bb53c7f37a49a64d88062860bb8e360a6a68b..50d89918ac134c903bb18a48d367f055b1923dc1 100644 (file)
@@ -173,23 +173,6 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart{
      */
     public abstract List<PackagePart> 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<PackagePart, POIXMLDocumentPart> context = new HashMap<PackagePart, POIXMLDocumentPart>();
         try {
index 2c0ae83d319d513c0cff55c88eb23abd5afc0838..e67055838d348826aa29387406f7cedd989563b5 100644 (file)
@@ -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();
                }
index 5d8d9ade757daad228dda4fcf51abfbebc549fb5..f4ab0520555bf428dd64dcf0263e4af6d0fd3e45 100644 (file)
@@ -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);
         }
index fc7fec62851f35ffb8b743de144d5572cc6097b7..e05e57ec116a2b6876748e1ec2e30cb9c8f0c62b 100644 (file)
@@ -166,7 +166,7 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable<X
      * @param pkg the OpenXML4J <code>Package</code> 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<X
 
     public XSSFWorkbook(InputStream is) throws IOException {
         super(PackageHelper.open(is));
-
+        
         //build a tree of POIXMLDocumentParts, this workbook being the root
         load(XSSFFactory.getInstance());
     }
index 640833c12cddddd1ce8a6d949a884629f079d3bf..20004bd368988d8de02123efb63d52e06fa60d3f 100644 (file)
@@ -88,7 +88,7 @@ public class XWPFDocument extends POIXMLDocument {
     private XWPFHeaderFooterPolicy headerFooterPolicy;
 
     public XWPFDocument(OPCPackage pkg) throws IOException {
-        super(ensureWriteAccess(pkg));
+        super(pkg);
 
         //build a tree of POIXMLDocumentParts, this document being the root
         load(XWPFFactory.getInstance());