The NPOIFS mini stream blocks need to be writable, correct that and add some tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1102448 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2011-05-12 20:06:06 +00:00
parent ba9275e72b
commit 6c2d2ffe7f
2 changed files with 132 additions and 7 deletions

View 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;
}

View 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
}