]> source.dussan.org Git - poi.git/commitdiff
The NPOIFS mini stream blocks need to be writable, correct that and add some tests
authorNick Burch <nick@apache.org>
Thu, 12 May 2011 20:06:06 +0000 (20:06 +0000)
committerNick Burch <nick@apache.org>
Thu, 12 May 2011 20:06:06 +0000 (20:06 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1102448 13f79535-47bb-0310-9956-ffa450edef68

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

index 156b73d9acbfc7357758c75f8298f19615c4ce75..6d43c3f714958e7e0aedb43602d6c8c5fddf92e5 100644 (file)
@@ -69,16 +69,13 @@ public class NPOIFSMiniStore extends BlockStore
           it.next();
        }
        ByteBuffer dataBlock = it.next();
-       
-       // Our blocks are small, so duplicating it is fine 
-       byte[] data = new byte[POIFSConstants.SMALL_BLOCK_SIZE];
+
+       // Position ourselves, and take a slice 
        dataBlock.position(
              dataBlock.position() + bigBlockOffset
        );
-       dataBlock.get(data, 0, data.length);
-       
-       // Return a ByteBuffer on this
-       ByteBuffer miniBuffer = ByteBuffer.wrap(data);
+       ByteBuffer miniBuffer = dataBlock.slice();
+       miniBuffer.limit(POIFSConstants.SMALL_BLOCK_SIZE);
        return miniBuffer;
     }
     
index 5b17242a0893b5acdaef99d6fbceb60071634189..ca9dd6677b7b0bdfb6202dca34cc3050a2cded03 100644 (file)
@@ -603,7 +603,135 @@ public final class TestNPOIFSStream extends TestCase {
     */
    public void testWriteMiniStreams() throws Exception {
       NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.openResourceAsStream("BlockSize512.zvi"));
+      NPOIFSMiniStore ministore = fs.getMiniStore();
+      NPOIFSStream stream = new NPOIFSStream(ministore, 178);
+      
+      // 178 -> 179 -> 180 -> end
+      assertEquals(179, ministore.getNextBlock(178));
+      assertEquals(180, ministore.getNextBlock(179));
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(180));
+      
+      
+      // Try writing 3 full blocks worth
+      byte[] data = new byte[64*3];
+      for(int i=0; i<data.length; i++) {
+         data[i] = (byte)i;
+      }
+      stream = new NPOIFSStream(ministore, 178);
+      stream.updateContents(data);
+      
+      // Check
+      assertEquals(179, ministore.getNextBlock(178));
+      assertEquals(180, ministore.getNextBlock(179));
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(180));
+      
+      stream = new NPOIFSStream(ministore, 178);
+      Iterator<ByteBuffer> it = stream.getBlockIterator();
+      ByteBuffer b178 = it.next();
+      ByteBuffer b179 = it.next();
+      ByteBuffer b180 = it.next();
+      assertEquals(false, it.hasNext());
+      
+      assertEquals((byte)0x00, b178.get());
+      assertEquals((byte)0x01, b178.get());
+      assertEquals((byte)0x40, b179.get());
+      assertEquals((byte)0x41, b179.get());
+      assertEquals((byte)0x80, b180.get());
+      assertEquals((byte)0x81, b180.get());
+
+      
+      // Try writing just into 3 blocks worth
+      data = new byte[64*2 + 12];
+      for(int i=0; i<data.length; i++) {
+         data[i] = (byte)(i+4);
+      }
+      stream = new NPOIFSStream(ministore, 178);
+      stream.updateContents(data);
+      
+      // Check
+      assertEquals(179, ministore.getNextBlock(178));
+      assertEquals(180, ministore.getNextBlock(179));
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(180));
+      
+      stream = new NPOIFSStream(ministore, 178);
+      it = stream.getBlockIterator();
+      b178 = it.next();
+      b179 = it.next();
+      b180 = it.next();
+      assertEquals(false, it.hasNext());
+      
+      assertEquals((byte)0x04, b178.get(0));
+      assertEquals((byte)0x05, b178.get(1));
+      assertEquals((byte)0x44, b179.get(0));
+      assertEquals((byte)0x45, b179.get(1));
+      assertEquals((byte)0x84, b180.get(0));
+      assertEquals((byte)0x85, b180.get(1));
+
+      
+      // Try writing 1, should truncate
+      data = new byte[12];
+      for(int i=0; i<data.length; i++) {
+         data[i] = (byte)(i+9);
+      }
+      stream = new NPOIFSStream(ministore, 178);
+      stream.updateContents(data);
+
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(178));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(179));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(180));
+      
+      stream = new NPOIFSStream(ministore, 178);
+      it = stream.getBlockIterator();
+      b178 = it.next();
+      assertEquals(false, it.hasNext());
+      
+      assertEquals((byte)0x09, b178.get(0));
+      assertEquals((byte)0x0a, b178.get(1));
+
+      
+      // Try writing 5, should extend
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(178));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(179));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(180));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(181));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(182));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, ministore.getNextBlock(183));
+      
+      data = new byte[64*4 + 12];
+      for(int i=0; i<data.length; i++) {
+         data[i] = (byte)(i+3);
+      }
+      stream = new NPOIFSStream(ministore, 178);
+      stream.updateContents(data);
+      
+      assertEquals(179, ministore.getNextBlock(178));
+      assertEquals(180, ministore.getNextBlock(179));
+      assertEquals(181, ministore.getNextBlock(180));
+      assertEquals(182, ministore.getNextBlock(181));
+      assertEquals(POIFSConstants.END_OF_CHAIN, ministore.getNextBlock(182));
+      
+      stream = new NPOIFSStream(ministore, 178);
+      it = stream.getBlockIterator();
+      b178 = it.next();
+      b179 = it.next();
+      b180 = it.next();
+      ByteBuffer b181 = it.next();
+      ByteBuffer b182 = it.next();
+      assertEquals(false, it.hasNext());
+      
+      assertEquals((byte)0x03, b178.get(0));
+      assertEquals((byte)0x04, b178.get(1));
+      assertEquals((byte)0x43, b179.get(0));
+      assertEquals((byte)0x44, b179.get(1));
+      assertEquals((byte)0x83, b180.get(0));
+      assertEquals((byte)0x84, b180.get(1));
+      assertEquals((byte)0xc3, b181.get(0));
+      assertEquals((byte)0xc4, b181.get(1));
+      assertEquals((byte)0x03, b182.get(0));
+      assertEquals((byte)0x04, b182.get(1));
+
       
+      // Write lots, so it needs another big block 
       // TODO
    }