SBAT fix from bug #11744

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@600916 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2007-12-04 12:55:26 +00:00
parent ad49ccab2d
commit 38cdbd4114
4 changed files with 49 additions and 1 deletions

View File

@ -19,6 +19,7 @@
package org.apache.poi.poifs.storage; 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.BATManaged;
import org.apache.poi.poifs.filesystem.POIFSDocument; import org.apache.poi.poifs.filesystem.POIFSDocument;
import org.apache.poi.poifs.property.RootProperty; import org.apache.poi.poifs.property.RootProperty;
@ -69,6 +70,8 @@ public class SmallBlockTableWriter
{ {
_small_blocks.add(blocks[ j ]); _small_blocks.add(blocks[ j ]);
} }
} else {
doc.setStartBlock(POIFSConstants.END_OF_CHAIN);
} }
} }
_sbat.simpleCreateBlocks(); _sbat.simpleCreateBlocks();

View File

@ -19,6 +19,7 @@
package org.apache.poi.util; package org.apache.poi.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; 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> * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt>
*/ */

View File

@ -23,6 +23,7 @@ import java.io.ByteArrayOutputStream;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.util.IOUtils;
import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSWriterEvent; import org.apache.poi.poifs.filesystem.POIFSWriterEvent;
import org.apache.poi.poifs.filesystem.POIFSWriterListener; import org.apache.poi.poifs.filesystem.POIFSWriterListener;
@ -140,4 +141,28 @@ public class TestEmptyDocument extends TestCase {
fs.writeFilesystem(out); fs.writeFilesystem(out);
new POIFSFileSystem(new ByteArrayInputStream(out.toByteArray())); 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)));
}
} }