NPOIFSFileSystem fs = new NPOIFSFileSystem();
NPOIFSStream stream = new NPOIFSStream(fs);
// Check our filesystem has a BAT and the Properties
assertEquals(2, fs.getFreeBlock());
BATBlock bat = fs.getBATBlockAndIndex(0).getBlock();
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(2));
// Check the stream as-is
assertEquals(POIFSConstants.END_OF_CHAIN, stream.getStartBlock());
try {
stream.getBlockIterator();
fail("Shouldn't be able to get an iterator before writing");
} catch(IllegalStateException e) {}
// Write in two blocks
byte[] data = new byte[512+20];
for(int i=0; i<512; i++) {
data[i] = (byte)(i%256);
}
for(int i=512; i<data.length; i++) {
data[i] = (byte)(i%256 + 100);
}
stream.updateContents(data);
// Check now
assertEquals(4, fs.getFreeBlock());
bat = fs.getBATBlockAndIndex(0).getBlock();
assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
assertEquals(3, bat.getValueAt(2));
assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(3));
assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(4));
Iterator<ByteBuffer> it = stream.getBlockIterator();
assertEquals(true, it.hasNext());
ByteBuffer b = it.next();