From: Nick Burch Date: Thu, 10 Aug 2006 12:12:27 +0000 (+0000) Subject: If we're created with an InputStream, check to see if it's really RTF before proceeding X-Git-Tag: REL_3_0_ALPHA3~57 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=036a97a268c37bdb9d842af6166093ea466433c4;p=poi.git If we're created with an InputStream, check to see if it's really RTF before proceeding git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@430363 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java index 72cbc61803..468c4a95c8 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java @@ -19,6 +19,7 @@ package org.apache.poi.hwpf; import java.io.InputStream; import java.io.FileInputStream; +import java.io.PushbackInputStream; import java.io.IOException; import java.io.OutputStream; import java.io.ByteArrayInputStream; @@ -89,6 +90,28 @@ public class HWPFDocument extends POIDocument } + /** + * Takens an InputStream, verifies that it's not RTF, builds a + * POIFSFileSystem from it, and returns that. + */ + public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException { + // Open a PushbackInputStream, so we can peek at the first few bytes + PushbackInputStream pis = new PushbackInputStream(istream,6); + byte[] first6 = new byte[6]; + pis.read(first6); + + // Does it start with {\rtf ? If so, it's really RTF + if(first6[0] == '{' && first6[1] == '\\' && first6[2] == 'r' + && first6[3] == 't' && first6[4] == 'f') { + throw new IllegalArgumentException("The document is really a RTF file"); + } + + // OK, so it's not RTF + // Open a POIFSFileSystem on the (pushed back) stream + pis.unread(first6); + return new POIFSFileSystem(pis); + } + /** * This constructor loads a Word document from an InputStream. * @@ -99,7 +122,7 @@ public class HWPFDocument extends POIDocument public HWPFDocument(InputStream istream) throws IOException { //do Ole stuff - this( new POIFSFileSystem(istream) ); + this( verifyAndBuildPOIFS(istream) ); } /** diff --git a/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java b/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java index d1422288f4..482dd09df7 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java @@ -29,7 +29,7 @@ public class WordExtractor { * @param is InputStream containing the word file */ public WordExtractor(InputStream is) throws IOException { - this(new POIFSFileSystem(is)); + this( HWPFDocument.verifyAndBuildPOIFS(is) ); } /**