You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2011/03/25 19:02:46 UTC

svn commit: r1085493 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/ testcases/org/apache/poi/poifs/filesystem/

Author: nick
Date: Fri Mar 25 18:02:46 2011
New Revision: 1085493

URL: http://svn.apache.org/viewvc?rev=1085493&view=rev
Log:
Fix a couple of NPOIFS bugs relating to empty files, empty documents and non-padded stream data

Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java
    poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSStream.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java?rev=1085493&r1=1085492&r2=1085493&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSDocument.java Fri Mar 25 18:02:46 2011
@@ -24,6 +24,7 @@ import java.io.InputStream;
 import java.nio.ByteBuffer;
 import java.util.Collections;
 import java.util.Iterator;
+import java.util.List;
 
 import org.apache.poi.poifs.common.POIFSConstants;
 import org.apache.poi.poifs.dev.POIFSViewable;
@@ -106,7 +107,12 @@ public final class NPOIFSDocument implem
    }
    
    Iterator<ByteBuffer> getBlockIterator() {
-      return _stream.getBlockIterator();
+      if(getSize() > 0) {
+         return _stream.getBlockIterator();
+      } else {
+         List<ByteBuffer> empty = Collections.emptyList();
+         return empty.iterator();
+      }
    }
 
    /**

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java?rev=1085493&r1=1085492&r2=1085493&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java Fri Mar 25 18:02:46 2011
@@ -103,6 +103,9 @@ public class NPOIFSFileSystem extends Bl
         _xbat_blocks     = new ArrayList<BATBlock>();
         _bat_blocks     = new ArrayList<BATBlock>();
         _root           = null;
+        
+        // Data needs to initially hold just the header block 
+        _data           = new ByteArrayBackedDataSource(new byte[bigBlockSize.getBigBlockSize()]);
     }
 
     /**

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java?rev=1085493&r1=1085492&r2=1085493&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSStream.java Fri Mar 25 18:02:46 2011
@@ -102,7 +102,7 @@ public class NPOIFSStream implements Ite
    public void updateContents(byte[] contents) throws IOException {
       // How many blocks are we going to need?
       int blockSize = blockStore.getBlockStoreBlockSize();
-      int blocks = (int)Math.ceil(contents.length / blockSize);
+      int blocks = (int)Math.ceil( ((double)contents.length) / blockSize );
       
       // Make sure we don't encounter a loop whilst overwriting
       //  the existing blocks
@@ -141,7 +141,9 @@ public class NPOIFSStream implements Ite
          
          // Write it
          ByteBuffer buffer = blockStore.createBlockIfNeeded(thisBlock);
-         buffer.put(contents, i*blockSize, blockSize);
+         int startAt = i*blockSize;
+         int endAt = Math.min(contents.length - startAt, blockSize);
+         buffer.put(contents, startAt, endAt); 
          
          // Update pointers
          prevBlock = thisBlock;

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=1085493&r1=1085492&r2=1085493&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 Fri Mar 25 18:02:46 2011
@@ -24,6 +24,7 @@ import junit.framework.TestCase;
 
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.poifs.common.POIFSConstants;
+import org.apache.poi.poifs.storage.BATBlock;
 
 /**
  * Tests {@link NPOIFSStream}
@@ -638,4 +639,69 @@ public final class TestNPOIFSStream exte
          fail("Loop should have been detected but wasn't!");
       } catch(IllegalStateException e) {}
    }
+   
+   /**
+    * Tests adding a new stream, writing and reading it.
+    */
+   public void testReadWriteNewStream() throws Exception {
+      NPOIFSFileSystem fs = new NPOIFSFileSystem();
+      NPOIFSStream stream = new NPOIFSStream(fs);
+      
+      // Check our filesystem has a single block
+      //  to hold the BAT
+      assertEquals(1, fs.getFreeBlock());
+      BATBlock bat = fs.getBATBlockAndIndex(0).getBlock();
+      assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(1));
+      
+      // Check the stream as-is
+      assertEquals(POIFSConstants.END_OF_CHAIN, stream.getStartBlock());
+      try {
+         stream.getBlockIterator();
+         fail("Shouldn't be able to get an iterator before writing");
+      } catch(IllegalStateException e) {}
+      
+      // Write in two blocks
+      byte[] data = new byte[512+20];
+      for(int i=0; i<512; i++) {
+         data[i] = (byte)(i%256);
+      }
+      for(int i=512; i<data.length; i++) {
+         data[i] = (byte)(i%256 + 100);
+      }
+      stream.updateContents(data);
+      
+      // Check now
+      assertEquals(3, fs.getFreeBlock());
+      bat = fs.getBATBlockAndIndex(0).getBlock();
+      assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
+      assertEquals(2,                           bat.getValueAt(1));
+      assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(2));
+      assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(3));
+      
+      
+      Iterator<ByteBuffer> it = stream.getBlockIterator();
+      assertEquals(true, it.hasNext());
+      ByteBuffer b = it.next();
+      
+      byte[] read = new byte[512];
+      b.get(read);
+      for(int i=0; i<read.length; i++) {
+         assertEquals("Wrong value at " + i, data[i], read[i]);
+      }
+      
+      assertEquals(true, it.hasNext());
+      b = it.next();
+      
+      read = new byte[512];
+      b.get(read);
+      for(int i=0; i<20; i++) {
+         assertEquals(data[i+512], read[i]);
+      }
+      for(int i=20; i<read.length; i++) {
+         assertEquals(0, read[i]);
+      }
+      
+      assertEquals(false, it.hasNext());
+   }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org