diff --git a/src/documentation/content/xdocs/hssf/quick-guide.xml b/src/documentation/content/xdocs/hssf/quick-guide.xml index eb48074df..ddbd447fd 100644 --- a/src/documentation/content/xdocs/hssf/quick-guide.xml +++ b/src/documentation/content/xdocs/hssf/quick-guide.xml @@ -247,7 +247,7 @@ and HSSFSheet provides a rowIterator() method to give an iterator over all the rows.

- HSSFSheet sheet = wb.getSheetAt(0); + HSSFSheet sheet = wb.getSheetAt(0); for (HSSFRow row : sheet.rowIterator()) { for (HSSFCell cell : row.cellIterator()) { // Do something here diff --git a/src/java/org/apache/poi/poifs/storage/SmallBlockTableWriter.java b/src/java/org/apache/poi/poifs/storage/SmallBlockTableWriter.java index f5bb58a76..4f89af154 100644 --- a/src/java/org/apache/poi/poifs/storage/SmallBlockTableWriter.java +++ b/src/java/org/apache/poi/poifs/storage/SmallBlockTableWriter.java @@ -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(); diff --git a/src/java/org/apache/poi/util/IOUtils.java b/src/java/org/apache/poi/util/IOUtils.java index 7126b2b9b..42b69c850 100644 --- a/src/java/org/apache/poi/util/IOUtils.java +++ b/src/java/org/apache/poi/util/IOUtils.java @@ -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 readFully(in, b, 0, b.length) */ diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java b/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java index a56a1d2a3..4f67f9876 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestEmptyDocument.java @@ -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))); + } }