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
This commit is contained in:
Yegor Kozlov 2010-01-13 20:24:15 +00:00
parent b88672b637
commit 8b377f2f67
6 changed files with 6 additions and 36 deletions

View 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>

View 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 {

View 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();
}

View 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);
}

View 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());
}

View 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());