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
This commit is contained in:
Nick Burch 2011-10-15 10:27:52 +00:00
parent e483b6c41d
commit ee76e11d17
1 changed files with 34 additions and 19 deletions

View File

@ -17,12 +17,13 @@
package org.apache.poi.poifs.filesystem; package org.apache.poi.poifs.filesystem;
import org.apache.poi.util.*;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; 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 * Represents an Ole10Native record which is wrapped around certain binary
@ -57,19 +58,33 @@ public class Ole10Native {
* @throws Ole10NativeException on invalid or unexcepted data format * @throws Ole10NativeException on invalid or unexcepted data format
*/ */
public static Ole10Native createFromEmbeddedOleObject(POIFSFileSystem poifs) throws IOException, Ole10NativeException { public static Ole10Native createFromEmbeddedOleObject(POIFSFileSystem poifs) throws IOException, Ole10NativeException {
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; boolean plain = false;
try { try {
poifs.getRoot().getEntry("\u0001Ole10ItemName"); directory.getEntry("\u0001Ole10ItemName");
plain = true; plain = true;
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
plain = false; plain = false;
} }
DocumentInputStream dis = poifs.createDocumentInputStream(OLE10_NATIVE); DocumentEntry nativeEntry =
ByteArrayOutputStream bos = new ByteArrayOutputStream(); (DocumentEntry)directory.getEntry(OLE10_NATIVE);
IOUtils.copy(dis, bos); byte[] data = new byte[nativeEntry.getSize()];
byte[] data = bos.toByteArray(); directory.createDocumentInputStream(nativeEntry).read(data);
return new Ole10Native(data, 0, plain); return new Ole10Native(data, 0, plain);
} }