Author: nick
Date: Thu May 12 20:06:06 2011
New Revision: 1102448
URL: http://svn.apache.org/viewvc?rev=1102448&view=rev
Log:
The NPOIFS mini stream blocks need to be writable, correct that and add some
tests
Modified:
poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java
Modified:
poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java?rev=1102448&r1=1102447&r2=1102448&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java
(original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java Thu
May 12 20:06:06 2011
@@ -69,16 +69,13 @@ public class NPOIFSMiniStore extends Blo
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;
}
Modified:
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java
URL:
http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java?rev=1102448&r1=1102447&r2=1102448&view=diff
==============================================================================
---
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java
(original)
+++
poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java
Thu May 12 20:06:06 2011
@@ -603,7 +603,135 @@ public final class TestNPOIFSStream exte
*/
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
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]