From d464da3bb042beeed804af549e1d1dfcafd7f29a Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 12 May 2011 20:06:06 +0000 Subject: [PATCH] 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 --- .../poi/poifs/filesystem/NPOIFSMiniStore.java | 11 +- .../poifs/filesystem/TestNPOIFSStream.java | 128 ++++++++++++++++++ 2 files changed, 132 insertions(+), 7 deletions(-) diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java index 156b73d9a..6d43c3f71 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 5b17242a0..ca9dd6677 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