From ee76e11d177c2827454ad6e5f890a389a64f4a39 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sat, 15 Oct 2011 10:27:52 +0000 Subject: [PATCH] Allow Ole10Native to be created from a DirectoryNode, not only a a POIFS git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1183618 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/poifs/filesystem/Ole10Native.java | 51 ++++++++++++------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java b/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java index 488bfb3053..db05d97d9f 100644 --- a/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java +++ b/src/java/org/apache/poi/poifs/filesystem/Ole10Native.java @@ -17,12 +17,13 @@ package org.apache.poi.poifs.filesystem; -import org.apache.poi.util.*; - -import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; -import java.util.Arrays; + +import org.apache.poi.util.HexDump; +import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.LittleEndianConsts; +import org.apache.poi.util.StringUtil; /** * Represents an Ole10Native record which is wrapped around certain binary @@ -57,23 +58,37 @@ public class Ole10Native { * @throws Ole10NativeException on invalid or unexcepted data format */ public static Ole10Native createFromEmbeddedOleObject(POIFSFileSystem poifs) throws IOException, Ole10NativeException { - boolean plain = false; - - try { - poifs.getRoot().getEntry("\u0001Ole10ItemName"); - plain = true; - } catch (FileNotFoundException ex) { - plain = false; - } + return createFromEmbeddedOleObject(poifs.getRoot()); + } + + /** + * Creates an instance of this class from an embedded OLE Object. The OLE Object is expected + * to include a stream "{01}Ole10Native" which contains the actual + * data relevant for this class. + * + * @param poifs POI Filesystem object + * @return Returns an instance of this class + * @throws IOException on IO error + * @throws Ole10NativeException on invalid or unexcepted data format + */ + public static Ole10Native createFromEmbeddedOleObject(DirectoryNode directory) throws IOException, Ole10NativeException { + boolean plain = false; - DocumentInputStream dis = poifs.createDocumentInputStream(OLE10_NATIVE); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - IOUtils.copy(dis, bos); - byte[] data = bos.toByteArray(); + try { + directory.getEntry("\u0001Ole10ItemName"); + plain = true; + } catch (FileNotFoundException ex) { + plain = false; + } + + DocumentEntry nativeEntry = + (DocumentEntry)directory.getEntry(OLE10_NATIVE); + byte[] data = new byte[nativeEntry.getSize()]; + directory.createDocumentInputStream(nativeEntry).read(data); - return new Ole10Native(data, 0, plain); + return new Ole10Native(data, 0, plain); } - + /** * Creates an instance and fills the fields based on the data in the given buffer. * -- 2.39.5