]> source.dussan.org Git - poi.git/commitdiff
Correct mini stream initialisation, and enable more npoifs write tests
authorNick Burch <nick@apache.org>
Fri, 25 Apr 2014 18:14:28 +0000 (18:14 +0000)
committerNick Burch <nick@apache.org>
Fri, 25 Apr 2014 18:14:28 +0000 (18:14 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1590088 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java
src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java

index 8b7b301b8d166be3e6c6863321c748fe7c16ffd0..5c753927e2042383f110f5c71262db3bf9631a8e 100644 (file)
@@ -86,9 +86,13 @@ public class NPOIFSMiniStore extends BlockStore
      * Load the block, extending the underlying stream if needed
      */
     protected ByteBuffer createBlockIfNeeded(final int offset) throws IOException {
-       // Ensure we have our first block at this point
+       // If we are the first block to be allocated, initialise the stream
        if (_mini_stream.getStartBlock() == POIFSConstants.END_OF_CHAIN) {
-           getFreeBlock();
+           int firstBigBlock = _filesystem.getFreeBlock();
+           _filesystem.createBlockIfNeeded(firstBigBlock);
+           _filesystem.setNextBlock(firstBigBlock, POIFSConstants.END_OF_CHAIN);
+           _filesystem._get_property_table().getRoot().setStartBlock(firstBigBlock);
+           _mini_stream = new NPOIFSStream(_filesystem, firstBigBlock);
        }
         
        // Try to get it without extending the stream
@@ -189,10 +193,9 @@ public class NPOIFSMiniStore extends BlockStore
        
        // Are we the first SBAT?
        if(_header.getSBATCount() == 0) {
+          // Tell the header that we've got our first SBAT there
           _header.setSBATStart(batForSBAT);
           _header.setSBATBlockCount(1);
-          _filesystem._get_property_table().getRoot().setStartBlock(batForSBAT);
-          _mini_stream = new NPOIFSStream(_filesystem, batForSBAT);
        } else {
           // Find the end of the SBAT stream, and add the sbat in there
           ChainLoopDetector loopDetector = _filesystem.getChainLoopDetector();
index 92b8ac004bdcd33ce04e303bc3849cdd7613f7d4..6d51e55911cdf4a944bb65aa0ee0e2bfba6cd71d 100644 (file)
@@ -701,20 +701,16 @@ public final class TestNPOIFSFileSystem {
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
-      if (fs.getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE) {
-          assertEquals(4, fs.getNextBlock(3));
-          assertEquals(5, fs.getNextBlock(4));
-          assertEquals(6, fs.getNextBlock(5));
-          assertEquals(7, fs.getNextBlock(6));
-          assertEquals(8, fs.getNextBlock(7));
-          assertEquals(9, fs.getNextBlock(8));
-          assertEquals(10, fs.getNextBlock(9));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(11));
-      } else {
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(4));
-      }
+      assertEquals(4, fs.getNextBlock(3));
+      assertEquals(5, fs.getNextBlock(4));
+      assertEquals(6, fs.getNextBlock(5));
+      assertEquals(7, fs.getNextBlock(6));
+      assertEquals(8, fs.getNextBlock(7));
+      assertEquals(9, fs.getNextBlock(8));
+      assertEquals(10, fs.getNextBlock(9));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(11));
+      
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock());
 
       
@@ -727,79 +723,66 @@ public final class TestNPOIFSFileSystem {
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
-      if (fs.getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE) {
-          assertEquals(4, fs.getNextBlock(3));
-          assertEquals(5, fs.getNextBlock(4));
-          assertEquals(6, fs.getNextBlock(5));
-          assertEquals(7, fs.getNextBlock(6));
-          assertEquals(8, fs.getNextBlock(7));
-          assertEquals(9, fs.getNextBlock(8));
-          assertEquals(10, fs.getNextBlock(9));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
+      assertEquals(4, fs.getNextBlock(3));
+      assertEquals(5, fs.getNextBlock(4));
+      assertEquals(6, fs.getNextBlock(5));
+      assertEquals(7, fs.getNextBlock(6));
+      assertEquals(8, fs.getNextBlock(7));
+      assertEquals(9, fs.getNextBlock(8));
+      assertEquals(10, fs.getNextBlock(9));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
+
+      assertEquals(12, fs.getNextBlock(11));
+      assertEquals(13, fs.getNextBlock(12));
+      assertEquals(14, fs.getNextBlock(13));
+      assertEquals(15, fs.getNextBlock(14));
+      assertEquals(16, fs.getNextBlock(15));
+      assertEquals(17, fs.getNextBlock(16));
+      assertEquals(18, fs.getNextBlock(17));
+      assertEquals(19, fs.getNextBlock(18));
+      assertEquals(20, fs.getNextBlock(19));
+      assertEquals(21, fs.getNextBlock(20));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(21));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(22));
 
-          assertEquals(12, fs.getNextBlock(11));
-          assertEquals(13, fs.getNextBlock(12));
-          assertEquals(14, fs.getNextBlock(13));
-          assertEquals(15, fs.getNextBlock(14));
-          assertEquals(16, fs.getNextBlock(15));
-          assertEquals(17, fs.getNextBlock(16));
-          assertEquals(18, fs.getNextBlock(17));
-          assertEquals(19, fs.getNextBlock(18));
-          assertEquals(20, fs.getNextBlock(19));
-          assertEquals(21, fs.getNextBlock(20));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(21));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(22));
-      } else {
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3));
-          assertEquals(5,                           fs.getNextBlock(4));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(5));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(6));
-      }
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getRoot().getProperty().getStartBlock());
       
       
       // Now Add a mini stream
-      byte[] mini = new byte[] { 0, 1, 2, 3, 4 };
+      byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
       testDir.createDocument("Mini", new ByteArrayInputStream(mini));
       
       // Mini stream will get one block for fat + one block for data
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
-      if (fs.getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE) {
-          assertEquals(4, fs.getNextBlock(3));
-          assertEquals(5, fs.getNextBlock(4));
-          assertEquals(6, fs.getNextBlock(5));
-          assertEquals(7, fs.getNextBlock(6));
-          assertEquals(8, fs.getNextBlock(7));
-          assertEquals(9, fs.getNextBlock(8));
-          assertEquals(10, fs.getNextBlock(9));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
+      assertEquals(4, fs.getNextBlock(3));
+      assertEquals(5, fs.getNextBlock(4));
+      assertEquals(6, fs.getNextBlock(5));
+      assertEquals(7, fs.getNextBlock(6));
+      assertEquals(8, fs.getNextBlock(7));
+      assertEquals(9, fs.getNextBlock(8));
+      assertEquals(10, fs.getNextBlock(9));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
+
+      assertEquals(12, fs.getNextBlock(11));
+      assertEquals(13, fs.getNextBlock(12));
+      assertEquals(14, fs.getNextBlock(13));
+      assertEquals(15, fs.getNextBlock(14));
+      assertEquals(16, fs.getNextBlock(15));
+      assertEquals(17, fs.getNextBlock(16));
+      assertEquals(18, fs.getNextBlock(17));
+      assertEquals(19, fs.getNextBlock(18));
+      assertEquals(20, fs.getNextBlock(19));
+      assertEquals(21, fs.getNextBlock(20));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(21));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(22));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(23));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(24));
 
-          assertEquals(12, fs.getNextBlock(11));
-          assertEquals(13, fs.getNextBlock(12));
-          assertEquals(14, fs.getNextBlock(13));
-          assertEquals(15, fs.getNextBlock(14));
-          assertEquals(16, fs.getNextBlock(15));
-          assertEquals(17, fs.getNextBlock(16));
-          assertEquals(18, fs.getNextBlock(17));
-          assertEquals(19, fs.getNextBlock(18));
-          assertEquals(20, fs.getNextBlock(19));
-          assertEquals(21, fs.getNextBlock(20));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(21));
-          assertEquals(23,                          fs.getNextBlock(22));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(23));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(24));
-      } else {
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3));
-          assertEquals(5,                           fs.getNextBlock(4));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(5));
-          assertEquals(7,                           fs.getNextBlock(6));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(7));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(8));
-      }
       // Check the mini stream location was set
-      assertEquals(22, fs.getRoot().getProperty().getStartBlock());
+      // (22 is mini fat, 23 is first mini stream block)
+      assertEquals(23, fs.getRoot().getProperty().getStartBlock());
       
       
       // Write and read back
@@ -808,42 +791,34 @@ public final class TestNPOIFSFileSystem {
       // Check it's all unchanged
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
-if (1==0) {
+if(1==0) { // TODO FIX      
       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
-      if (fs.getBigBlockSize() == POIFSConstants.SMALLER_BIG_BLOCK_SIZE) {
-          assertEquals(4, fs.getNextBlock(3));
-          assertEquals(5, fs.getNextBlock(4));
-          assertEquals(6, fs.getNextBlock(5));
-          assertEquals(7, fs.getNextBlock(6));
-          assertEquals(8, fs.getNextBlock(7));
-          assertEquals(9, fs.getNextBlock(8));
-          assertEquals(10, fs.getNextBlock(9));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
-
-          assertEquals(12, fs.getNextBlock(11));
-          assertEquals(13, fs.getNextBlock(12));
-          assertEquals(14, fs.getNextBlock(13));
-          assertEquals(15, fs.getNextBlock(14));
-          assertEquals(16, fs.getNextBlock(15));
-          assertEquals(17, fs.getNextBlock(16));
-          assertEquals(18, fs.getNextBlock(17));
-          assertEquals(19, fs.getNextBlock(18));
-          assertEquals(20, fs.getNextBlock(19));
-          assertEquals(21, fs.getNextBlock(20));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(21));
-          assertEquals(23,                          fs.getNextBlock(22));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(23));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(24));
-      } else {
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3));
-          assertEquals(5,                           fs.getNextBlock(4));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(5));
-          assertEquals(7,                           fs.getNextBlock(6));
-          assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(7));
-          assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(8));
-      }
+      assertEquals(4, fs.getNextBlock(3));
+      assertEquals(5, fs.getNextBlock(4));
+      assertEquals(6, fs.getNextBlock(5));
+      assertEquals(7, fs.getNextBlock(6));
+      assertEquals(8, fs.getNextBlock(7));
+      assertEquals(9, fs.getNextBlock(8));
+      assertEquals(10, fs.getNextBlock(9));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(10));
+
+      assertEquals(12, fs.getNextBlock(11));
+      assertEquals(13, fs.getNextBlock(12));
+      assertEquals(14, fs.getNextBlock(13));
+      assertEquals(15, fs.getNextBlock(14));
+      assertEquals(16, fs.getNextBlock(15));
+      assertEquals(17, fs.getNextBlock(16));
+      assertEquals(18, fs.getNextBlock(17));
+      assertEquals(19, fs.getNextBlock(18));
+      assertEquals(20, fs.getNextBlock(19));
+      assertEquals(21, fs.getNextBlock(20));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(21));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(22));
+      assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(23));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(24));
 }
-      assertEquals(22, fs.getRoot().getProperty().getStartBlock());
+
+      assertEquals(23, fs.getRoot().getProperty().getStartBlock());
 
       
       // Check some data
@@ -899,10 +874,10 @@ if (1==0) {
        
        // Check the mini stream was added, then the main stream
        assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
-       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
-       assertEquals(3,                           fs.getNextBlock(2));
-       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3));
-       assertEquals(5,                           fs.getNextBlock(4));
+       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1)); 
+       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2)); // Mini Fat
+       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3)); // Mini Stream
+       assertEquals(5,                           fs.getNextBlock(4)); // Main Stream
        assertEquals(6,                           fs.getNextBlock(5));
        assertEquals(7,                           fs.getNextBlock(6));
        assertEquals(8,                           fs.getNextBlock(7));
@@ -946,7 +921,7 @@ if (1==0) {
        //  the properties stream going in at the end
        assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, fs.getNextBlock(0));
        assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(1));
-       assertEquals(3,                           fs.getNextBlock(2));
+       assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(2));
        assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3));
        assertEquals(5,                           fs.getNextBlock(4));
        assertEquals(6,                           fs.getNextBlock(5));
@@ -976,10 +951,7 @@ if (1==0) {
        inp = new NDocumentInputStream(miniDoc);
        miniRead = new byte[miniDoc.getSize()];
        assertEquals(miniDoc.getSize(), inp.read(miniRead));
-       // TODO Fix mini stream write/read corruption
-if(1==0) {       
        assertThat(mini, equalTo(miniRead));
-}
        inp.close();
 
        normDoc = (DocumentEntry)testDir.getEntry("Normal4096");