Author: nick
Date: Thu Aug 10 05:12:27 2006
New Revision: 430363

URL: http://svn.apache.org/viewvc?rev=430363&view=rev
Log:
If we're created with an InputStream, check to see if it's really RTF before 
proceeding

Modified:
    jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
    
jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java

Modified: 
jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
URL: 
http://svn.apache.org/viewvc/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java?rev=430363&r1=430362&r2=430363&view=diff
==============================================================================
--- jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java 
(original)
+++ jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java 
Thu Aug 10 05:12:27 2006
@@ -19,6 +19,7 @@
 
 import java.io.InputStream;
 import java.io.FileInputStream;
+import java.io.PushbackInputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.ByteArrayInputStream;
@@ -90,6 +91,28 @@
   }
 
   /**
+   * 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.
    *
    * @param istream The InputStream that contains the Word document.
@@ -99,7 +122,7 @@
   public HWPFDocument(InputStream istream) throws IOException
   {
     //do Ole stuff
-    this( new POIFSFileSystem(istream) );
+    this( verifyAndBuildPOIFS(istream) );
   }
 
   /**

Modified: 
jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java
URL: 
http://svn.apache.org/viewvc/jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java?rev=430363&r1=430362&r2=430363&view=diff
==============================================================================
--- 
jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java
 (original)
+++ 
jakarta/poi/trunk/src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java
 Thu Aug 10 05:12:27 2006
@@ -29,7 +29,7 @@
         * @param is InputStream containing the word file
         */
        public WordExtractor(InputStream is) throws IOException {
-               this(new POIFSFileSystem(is));
+               this( HWPFDocument.verifyAndBuildPOIFS(is) );
        }
 
        /**



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
Mailing List:    http://jakarta.apache.org/site/mail2.html#poi
The Apache Jakarta POI Project: http://jakarta.apache.org/poi/

Reply via email to