From e942647b66f5066ab39b956ccd21d53db76c548d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 6 Jul 2015 21:39:42 +0000 Subject: [PATCH] When writing the mini-stream, set the size of it on the root property #58061 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1689505 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/poifs/dev/POIFSHeaderDumper.java | 1 + .../poi/poifs/filesystem/NPOIFSFileSystem.java | 7 ++++--- .../poi/poifs/filesystem/NPOIFSMiniStore.java | 16 ++++++++++++++-- .../poifs/filesystem/TestNPOIFSFileSystem.java | 4 ++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java b/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java index 54ab23149..e321c8b9b 100644 --- a/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java +++ b/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java @@ -160,6 +160,7 @@ public class POIFSHeaderDumper { public static void displayPropertiesSummary(PropertyTable properties) { System.out.println("Mini Stream starts at " + properties.getRoot().getStartBlock()); + System.out.println("Mini Stream length is " + properties.getRoot().getSize()); System.out.println(); System.out.println("Properties and their block start:"); diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java index f7e19137a..c27ba3cd1 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java @@ -766,6 +766,10 @@ public class NPOIFSFileSystem extends BlockStore * to their backing blocks */ private void syncWithDataSource() throws IOException { + // Mini Stream + SBATs first, as mini-stream details have + // to be stored in the Root Property + _mini_store.syncWithDataSource(); + // Properties NPOIFSStream propStream = new NPOIFSStream(this, _header.getPropertyStart()); _property_table.preWrite(); @@ -786,9 +790,6 @@ public class NPOIFSFileSystem extends BlockStore ByteBuffer block = getBlockAt(bat.getOurBlockIndex()); BlockAllocationTableWriter.writeBlock(bat, block); } - - // SBATs - _mini_store.syncWithDataSource(); } /** diff --git a/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java b/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java index 308ae15c7..b784058bc 100644 --- a/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java +++ b/src/java/org/apache/poi/poifs/filesystem/NPOIFSMiniStore.java @@ -242,12 +242,24 @@ public class NPOIFSMiniStore extends BlockStore } /** - * Writes the SBATs to their backing blocks + * Writes the SBATs to their backing blocks, and updates + * the mini-stream size in the properties. Stream size is + * based on full blocks used, not the data within the streams */ protected void syncWithDataSource() throws IOException { - for(BATBlock sbat : _sbat_blocks) { + int blocksUsed = 0; + for (BATBlock sbat : _sbat_blocks) { ByteBuffer block = _filesystem.getBlockAt(sbat.getOurBlockIndex()); BlockAllocationTableWriter.writeBlock(sbat, block); + + if (!sbat.hasFreeSectors()) { + blocksUsed += _filesystem.getBigBlockSizeDetails().getBATEntriesPerBlock(); + } else { + blocksUsed += sbat.getUsedSectors(false); + } } + // Set the size on the root in terms of the number of SBAT blocks + // RootProperty.setSize does the sbat -> bytes conversion for us + _filesystem._get_property_table().getRoot().setSize(blocksUsed); } } diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java index a476233ec..ed598ed6c 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java @@ -1428,6 +1428,10 @@ public final class TestNPOIFSFileSystem { emptyDoc = (DocumentEntry)testDir.getEntry("empty-3"); assertContentsMatches(empty, emptyDoc); + // Check that a mini-stream was assigned, with one block used + assertEquals(3, testDir.getProperty().getStartBlock()); + assertEquals(64, testDir.getProperty().getSize()); + // All done fs.close(); }