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;
{
_small_blocks.add(blocks[ j ]);
}
+ } else {
+ doc.setStartBlock(POIFSConstants.END_OF_CHAIN);
}
}
_sbat.simpleCreateBlocks();
package org.apache.poi.util;
+import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
{
}
+ /**
+ * 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>
*/
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;
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)));
+ }
}