]> source.dussan.org Git - poi.git/commitdiff
If we're created with an InputStream, check to see if it's really RTF before proceeding
authorNick Burch <nick@apache.org>
Thu, 10 Aug 2006 12:12:27 +0000 (12:12 +0000)
committerNick Burch <nick@apache.org>
Thu, 10 Aug 2006 12:12:27 +0000 (12:12 +0000)
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@430363 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java
src/scratchpad/src/org/apache/poi/hwpf/extractor/WordExtractor.java

index 72cbc6180303662a3c04a70489949977c8b9666a..468c4a95c8b9fd92d53952e79c34ef651aad3b7a 100644 (file)
@@ -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) );
   }
 
   /**
index d1422288f4f111de2a591866b8b2adcb41ee9854..482dd09df7366ea2313654e324534d595af38a61 100644 (file)
@@ -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) );
        }
 
        /**