diff options
author | Nick Burch <nick@apache.org> | 2008-01-16 12:46:43 +0000 |
---|---|---|
committer | Nick Burch <nick@apache.org> | 2008-01-16 12:46:43 +0000 |
commit | 0034e00a5ce061552d3ff33e6b795a34232cfb57 (patch) | |
tree | 414d6a87ede2e61bfa930f2893bcc189b72cce5f /src/java/org/apache/poi | |
parent | 83107c9daec42ed33c1c1cbc0d6b2bf3a1ec6fca (diff) | |
download | poi-0034e00a5ce061552d3ff33e6b795a34232cfb57.tar.gz poi-0034e00a5ce061552d3ff33e6b795a34232cfb57.zip |
Add methods to check to see if a given InputStream has a OOXML file header, or a OLE2 file header, so that a future factory method could figure out which class to instantiate for a given InputStraeam
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@612438 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi')
3 files changed, 41 insertions, 2 deletions
diff --git a/src/java/org/apache/poi/poifs/common/POIFSConstants.java b/src/java/org/apache/poi/poifs/common/POIFSConstants.java index bc1bf6dad5..399f52be4b 100644 --- a/src/java/org/apache/poi/poifs/common/POIFSConstants.java +++ b/src/java/org/apache/poi/poifs/common/POIFSConstants.java @@ -31,4 +31,7 @@ public interface POIFSConstants public static final int END_OF_CHAIN = -2; public static final int PROPERTY_SIZE = 0x0080; public static final int UNUSED_BLOCK = -1; + + public static final byte[] OOXML_FILE_HEADER = + new byte[] { 0x50, 0x4b, 0x03, 0x04 }; } // end public interface POIFSConstants; diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java index 981c51d39a..3d4f1aac60 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java @@ -34,6 +34,7 @@ import org.apache.poi.poifs.storage.BlockAllocationTableReader; import org.apache.poi.poifs.storage.BlockAllocationTableWriter; import org.apache.poi.poifs.storage.BlockList; import org.apache.poi.poifs.storage.BlockWritable; +import org.apache.poi.poifs.storage.HeaderBlockConstants; import org.apache.poi.poifs.storage.HeaderBlockReader; import org.apache.poi.poifs.storage.HeaderBlockWriter; import org.apache.poi.poifs.storage.RawDataBlock; @@ -41,6 +42,9 @@ import org.apache.poi.poifs.storage.RawDataBlockList; import org.apache.poi.poifs.storage.SmallBlockTableReader; import org.apache.poi.poifs.storage.SmallBlockTableWriter; import org.apache.poi.poifs.storage.SmallDocumentBlock; +import org.apache.poi.util.IOUtils; +import org.apache.poi.util.LongField; +import org.apache.xmlbeans.impl.common.IOUtil; /** * This is the main class of the POIFS system; it manages the entire @@ -106,6 +110,35 @@ public class POIFSFileSystem .getSBATStart()), data_blocks, properties.getRoot() .getChildren(), null); } + + /** + * Checks that the supplied InputStream (which MUST + * support mark and reset, or be a PushbackInputStream) + * has a POIFS (OLE2) header at the start of it. + * If your InputStream does not support mark / reset, + * then wrap it in a PushBackInputStream, then be + * sure to always use that, and not the original! + * @param inp An InputStream which supports either mark/reset, or is a PushbackInputStream + */ + public static boolean hasPOIFSHeader(InputStream inp) throws IOException { + // We want to peek at the first 8 bytes + inp.mark(8); + + byte[] header = new byte[8]; + IOUtils.readFully(inp, header); + LongField signature = new LongField(HeaderBlockConstants._signature_offset, header); + + // Wind back those 8 bytes + if(inp instanceof PushbackInputStream) { + PushbackInputStream pin = (PushbackInputStream)inp; + pin.unread(header); + } else { + inp.reset(); + } + + // Did it match the signature? + return (signature.get() == HeaderBlockConstants._signature); + } /** * Create a new document to be added to the root directory diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java b/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java index 16c94e2c23..0d5bb817b4 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlockReader.java @@ -91,8 +91,11 @@ public class HeaderBlockReader if (signature.get() != _signature) { // Is it one of the usual suspects? - if(_data[0] == 0x50 && _data[1] == 0x4b && _data[2] == 0x03 && - _data[3] == 0x04) { + byte[] OOXML_FILE_HEADER = POIFSConstants.OOXML_FILE_HEADER; + if(_data[0] == OOXML_FILE_HEADER[0] && + _data[1] == OOXML_FILE_HEADER[1] && + _data[2] == OOXML_FILE_HEADER[2] && + _data[3] == OOXML_FILE_HEADER[3]) { throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. POI only supports OLE2 Office documents"); } |