From 38cdbd411426e30ecd960a1ecf4fc10fef382167 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 4 Dec 2007 12:55:26 +0000 Subject: [PATCH] SBAT fix from bug #11744 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@600916 13f79535-47bb-0310-9956-ffa450edef68 --- .../content/xdocs/hssf/quick-guide.xml | 2 +- .../poifs/storage/SmallBlockTableWriter.java | 3 +++ src/java/org/apache/poi/util/IOUtils.java | 20 +++++++++++++++ .../poifs/filesystem/TestEmptyDocument.java | 25 +++++++++++++++++++ 4 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/documentation/content/xdocs/hssf/quick-guide.xml b/src/documentation/content/xdocs/hssf/quick-guide.xml index eb48074dff..ddbd447fd1 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 f5bb58a76f..4f89af1545 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 7126b2b9be..42b69c850b 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 a56a1d2a3a..4f67f98767 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))); + } } -- 2.39.5