From 8536301aabebf2483b1359a15a2349f9dc364985 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 20 May 2008 15:42:16 +0000 Subject: [PATCH] Fix bug 44898 - Correctly handle short last blocks in POIFS git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@658285 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/changes.xml | 2 +- src/documentation/content/xdocs/status.xml | 1 + .../poi/poifs/storage/RawDataBlock.java | 23 ++++++++++++------- .../poi/poifs/storage/RawDataBlockList.java | 10 +++++--- .../poifs/filesystem/TestPOIFSFileSystem.java | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml index 3385827124..b9ed583d5f 100644 --- a/src/documentation/content/xdocs/changes.xml +++ b/src/documentation/content/xdocs/changes.xml @@ -37,7 +37,7 @@ - + 44898 - Correctly handle short last blocks in POIFS 44306 - fixed reading/writing of AttrPtg(type=choose) and method toFormulaString() for CHOOSE formulas diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 7cd3c2a7a1..aea0a9f1ab 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 44898 - Correctly handle short last blocks in POIFS 44306 - fixed reading/writing of AttrPtg(type=choose) and method toFormulaString() for CHOOSE formulas diff --git a/src/java/org/apache/poi/poifs/storage/RawDataBlock.java b/src/java/org/apache/poi/poifs/storage/RawDataBlock.java index b4630a78b4..5ca1781d07 100644 --- a/src/java/org/apache/poi/poifs/storage/RawDataBlock.java +++ b/src/java/org/apache/poi/poifs/storage/RawDataBlock.java @@ -37,6 +37,7 @@ public class RawDataBlock { private byte[] _data; private boolean _eof; + private boolean _hasData; private static POILogger log = POILogFactory.getLogger(RawDataBlock.class); /** @@ -66,6 +67,7 @@ public class RawDataBlock throws IOException { _data = new byte[ blockSize ]; int count = IOUtils.readFully(stream, _data); + _hasData = (count > 0); if (count == -1) { _eof = true; @@ -94,16 +96,21 @@ public class RawDataBlock /** * When we read the data, did we hit end of file? * - * @return true if no data was read because we were at the end of - * the file, else false - * - * @exception IOException + * @return true if the EoF was hit during this block, or + * false if not. If you have a dodgy short last block, then + * it's possible to both have data, and also hit EoF... */ - public boolean eof() - throws IOException - { + public boolean eof() { return _eof; } + /** + * Did we actually find any data to read? It's possible, + * in the event of a short last block, to both have hit + * the EoF, but also to have data + */ + public boolean hasData() { + return _hasData; + } /* ********** START implementation of ListManagedBlock ********** */ @@ -117,7 +124,7 @@ public class RawDataBlock public byte [] getData() throws IOException { - if (eof()) + if (! hasData()) { throw new IOException("Cannot return empty data"); } diff --git a/src/java/org/apache/poi/poifs/storage/RawDataBlockList.java b/src/java/org/apache/poi/poifs/storage/RawDataBlockList.java index 76ab219562..66eb237a8e 100644 --- a/src/java/org/apache/poi/poifs/storage/RawDataBlockList.java +++ b/src/java/org/apache/poi/poifs/storage/RawDataBlockList.java @@ -51,12 +51,16 @@ public class RawDataBlockList while (true) { RawDataBlock block = new RawDataBlock(stream, bigBlockSize); + + // If there was data, add the block to the list + if(block.hasData()) { + blocks.add(block); + } - if (block.eof()) - { + // If the stream is now at the End Of File, we're done + if (block.eof()) { break; } - blocks.add(block); } setBlocks(( RawDataBlock [] ) blocks.toArray(new RawDataBlock[ 0 ])); } diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java index 1cde86918b..4a948ba8c6 100755 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestPOIFSFileSystem.java @@ -130,7 +130,7 @@ public final class TestPOIFSFileSystem extends TestCase { * The other is to fix the handling of the last block in * POIFS, since it seems to be slight wrong */ - public void DISABLEDtestShortLastBlock() throws Exception { + public void testShortLastBlock() throws Exception { String[] files = new String[] { "ShortLastBlock.qwp", "ShortLastBlock.wps" }; -- 2.39.5