]> source.dussan.org Git - poi.git/commitdiff
SBAT fix from bug #11744
authorNick Burch <nick@apache.org>
Tue, 4 Dec 2007 12:55:26 +0000 (12:55 +0000)
committerNick Burch <nick@apache.org>
Tue, 4 Dec 2007 12:55:26 +0000 (12:55 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@600916 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/hssf/quick-guide.xml
src/java/org/apache/poi/poifs/storage/SmallBlockTableWriter.java
src/java/org/apache/poi/util/IOUtils.java
src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java

index eb48074dff14ed8430b09919ec0f921218d11c1a..ddbd447fd13427b17a66c15e759370ee3cbee2b0 100644 (file)
                                and HSSFSheet provides a <em>rowIterator()</em> method to
                                give an iterator over all the rows.</p>
                                <source>
-    HSSFSheet sheet = wb.getSheetAt(0);
+       HSSFSheet sheet = wb.getSheetAt(0);
        for (HSSFRow row : sheet.rowIterator()) {
                for (HSSFCell cell : row.cellIterator()) {
                        // Do something here
index f5bb58a76fabd45eb8013d61f62d757e80ce5107..4f89af154597f965349dc951e5224ff513dde37f 100644 (file)
@@ -19,6 +19,7 @@
 
 package org.apache.poi.poifs.storage;
 
+import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.poifs.filesystem.BATManaged;
 import org.apache.poi.poifs.filesystem.POIFSDocument;
 import org.apache.poi.poifs.property.RootProperty;
@@ -69,6 +70,8 @@ public class SmallBlockTableWriter
                 {
                     _small_blocks.add(blocks[ j ]);
                 }
+            } else {
+               doc.setStartBlock(POIFSConstants.END_OF_CHAIN);
             }
         }
         _sbat.simpleCreateBlocks();
index 7126b2b9bee6da09189af753bb1f5594a3a19730..42b69c850b8de1c67511737ed1e429c687bd347a 100644 (file)
@@ -19,6 +19,7 @@
 
 package org.apache.poi.util;
 
+import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
@@ -28,6 +29,25 @@ public class IOUtils
     {
     }
 
+    /**
+     * Reads all the data from the input stream, and returns
+     *  the bytes read.
+     */
+    public static byte[] toByteArray(InputStream stream) throws IOException {
+       ByteArrayOutputStream baos = new ByteArrayOutputStream();
+       
+       byte[] buffer = new byte[4096];
+       int read = 0;
+       while(read != -1) {
+               read = stream.read(buffer);
+               if(read > 0) {
+                       baos.write(buffer, 0, read);
+               }
+       }
+       
+       return baos.toByteArray();
+    }
+
     /**
      * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
      */
index a56a1d2a3a9b28f82d9e6268224df590ce79ce20..4f67f9876729a3f090b96c042f9d5d64e057c15c 100644 (file)
@@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.util.IOUtils;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.poifs.filesystem.POIFSWriterEvent;
 import org.apache.poi.poifs.filesystem.POIFSWriterListener;
@@ -140,4 +141,28 @@ public class TestEmptyDocument extends TestCase {
     fs.writeFilesystem(out);
     new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
   }
+
+  public void testEmptyDocumentBug11744() throws Exception {
+        byte[] testData = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
+
+        POIFSFileSystem fs = new POIFSFileSystem();
+        fs.createDocument(new ByteArrayInputStream(new byte[0]), "Empty");
+        fs.createDocument(new ByteArrayInputStream(testData), "NotEmpty");
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        fs.writeFilesystem(out);
+        out.toByteArray();
+
+        // This line caused the error.
+        fs = new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray()));
+
+        DocumentEntry entry = (DocumentEntry) fs.getRoot().getEntry("Empty");
+        assertEquals("Expected zero size", 0, entry.getSize());
+        assertEquals("Expected zero read from stream", 0,
+                     IOUtils.toByteArray(new DocumentInputStream(entry)).length);
+
+        entry = (DocumentEntry) fs.getRoot().getEntry("NotEmpty");
+        assertEquals("Expected size was wrong", testData.length, entry.getSize());
+        assertEquals("Expected different data read from stream", testData,
+                     IOUtils.toByteArray(new DocumentInputStream(entry)));
+    }
 }