From: Nick Burch Date: Thu, 12 May 2011 20:06:06 +0000 (+0000) Subject: The NPOIFS mini stream blocks need to be writable, correct that and add some tests X-Git-Tag: REL_3_8_BETA3~35 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=6c2d2ffe7fd9af4ff0614752736f164f338d937c;p=poi.git 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 --- diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java index 156b73d9ac..6d43c3f714 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java @@ -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; } diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java index 5b17242a08..ca9dd6677b 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java @@ -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 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