From aa84a05ad161bad4f55d5d73b675c2dbf2e04fe7 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Wed, 1 Jul 2015 00:43:33 +0000 Subject: [PATCH] To better match OPOIFS, pad to the end of a block with 0xFF/-1 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1688543 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/poifs/filesystem/NPOIFSDocument.java | 10 ++++++++++ .../poi/poifs/filesystem/TestNPOIFSMiniStore.java | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java index bdc35cee71..c94e427b92 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java @@ -23,6 +23,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.nio.ByteBuffer; +import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -143,6 +144,15 @@ public final class NPOIFSDocument implements POIFSViewable { os.write(buf, 0, readBytes); } + // Pad to the end of the block with -1s + int usedInBlock = length % _block_size; + if (usedInBlock != 0 && usedInBlock != _block_size) { + int toBlockEnd = _block_size - usedInBlock; + byte[] padding = new byte[toBlockEnd]; + Arrays.fill(padding, (byte)0xFF); + os.write(padding); + } + // Tidy and return the length os.close(); return length; diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java index 44e066a6f7..4f102084d7 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSMiniStore.java @@ -431,6 +431,7 @@ public final class TestNPOIFSMiniStore extends POITestCase { assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3)); // Mini assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(4)); + // First 2 Mini blocks will be used assertEquals(2, ministore.getFreeBlock()); // Add one more mini-stream, and check @@ -442,6 +443,7 @@ public final class TestNPOIFSMiniStore extends POITestCase { assertEquals(POIFSConstants.END_OF_CHAIN, fs.getNextBlock(3)); // Mini assertEquals(POIFSConstants.UNUSED_BLOCK, fs.getNextBlock(4)); + // One more mini-block will be used assertEquals(3, ministore.getFreeBlock()); // Check the contents too -- 2.39.5