]> source.dussan.org Git - poi.git/commitdiff
Fix testing for NPOIFS zero-length stream writing
authorNick Burch <nick@apache.org>
Tue, 26 May 2015 13:56:40 +0000 (13:56 +0000)
committerNick Burch <nick@apache.org>
Tue, 26 May 2015 13:56:40 +0000 (13:56 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1681762 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java
src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java
src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java

index 9492c2598339fbfeeee6b2b53fadac9b0c31fb89..bdc35cee712bd67ebf474888ee54863ef6485430 100644 (file)
@@ -143,10 +143,6 @@ public final class NPOIFSDocument implements POIFSViewable {
            os.write(buf, 0, readBytes);
        }
        
-       // If this is an empty document, write a single byte
-       //  to force a block allocation for this document
-       if (length == 0) os.write(0);
-       
        // Tidy and return the length
        os.close();
        return length;
index 5d444b8cb95e88e8cb5d6d08518dd6f8830bd4e0..da24fc3836b469eb39c6b591307ac178829aade7 100644 (file)
@@ -256,8 +256,10 @@ public class NPOIFSStream implements Iterable<ByteBuffer>
             NPOIFSStream toFree = new NPOIFSStream(blockStore, nextBlock);
             toFree.free(loopDetector);
             
-            // Mark the end of the stream
-            blockStore.setNextBlock(prevBlock, POIFSConstants.END_OF_CHAIN);
+            // Mark the end of the stream, if we have any data
+            if (prevBlock != POIFSConstants.END_OF_CHAIN) {
+                blockStore.setNextBlock(prevBlock, POIFSConstants.END_OF_CHAIN);
+            }
         }
    }
 }
index 69c6cacf688e9a2d69ab021f987aba81d023387a..75161b9f8373974e52c69703c9c6fc21a28ae385 100644 (file)
@@ -1336,8 +1336,6 @@ public final class TestNPOIFSFileSystem {
        }
    }
    
-   // TODO Should these have a mini-sbat entry or not?
-   // TODO Is the reading of zero-length properties exactly correct?
    @Test
    public void writeZeroLengthEntries() throws Exception {
        NPOIFSFileSystem fs = new NPOIFSFileSystem();
@@ -1378,6 +1376,37 @@ public final class TestNPOIFSFileSystem {
        emptyDoc = (DocumentEntry)testDir.getEntry("empty-3");
        assertContentsMatches(empty, emptyDoc);
        
+       // Look at the properties entry, and check the empty ones
+       //  have zero size and no start block
+       NPropertyTable props = fs._get_property_table();
+       Iterator<Property> propsIt = props.getRoot().getChildren();
+       
+       Property prop = propsIt.next();
+       assertEquals("Mini2", prop.getName());
+       assertEquals(0, prop.getStartBlock());
+       assertEquals(7, prop.getSize());
+       
+       prop = propsIt.next();
+       assertEquals("Normal4106", prop.getName());
+       assertEquals(4, prop.getStartBlock()); // BAT, Props, SBAT, MIni
+       assertEquals(4106, prop.getSize());
+       
+       prop = propsIt.next();
+       assertEquals("empty-1", prop.getName());
+       assertEquals(POIFSConstants.END_OF_CHAIN, prop.getStartBlock());
+       assertEquals(0, prop.getSize());
+       
+       prop = propsIt.next();
+       assertEquals("empty-2", prop.getName());
+       assertEquals(POIFSConstants.END_OF_CHAIN, prop.getStartBlock());
+       assertEquals(0, prop.getSize());
+       
+       prop = propsIt.next();
+       assertEquals("empty-3", prop.getName());
+       assertEquals(POIFSConstants.END_OF_CHAIN, prop.getStartBlock());
+       assertEquals(0, prop.getSize());
+       
+       
        // Save and re-check
        fs = writeOutAndReadBack(fs);
        testDir = fs.getRoot();