*/
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
}