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 2010/12/29 02:34:56 UTC

svn commit: r1053504 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java

Author: nick
Date: Wed Dec 29 01:34:56 2010
New Revision: 1053504

URL: http://svn.apache.org/viewvc?rev=1053504&view=rev
Log:
More NPOIFS XBAT updates

Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/NPOIFSFileSystem.java
    poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java

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=1053504&r1=1053503&r2=1053504&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 Wed Dec 29 01:34:56 2010
@@ -312,6 +312,17 @@ public class NPOIFSFileSystem extends Bl
        bat.setOurBlockIndex(batAt);
        _bat_blocks.add(bat);
     }
+    private BATBlock createBAT(int offset, boolean isBAT) throws IOException {
+       // Create a new BATBlock
+       BATBlock newBAT = BATBlock.createEmptyBATBlock(bigBlockSize, !isBAT);
+       newBAT.setOurBlockIndex(offset);
+       // Ensure there's a spot in the file for it
+       ByteBuffer buffer = ByteBuffer.allocate(bigBlockSize.getBigBlockSize());
+       int writeTo = (1+offset) * bigBlockSize.getBigBlockSize(); // Header isn't in BATs
+       _data.write(buffer, writeTo);
+       // All done
+       return newBAT;
+    }
     
     /**
      * Load the block at the given offset.
@@ -377,12 +388,7 @@ public class NPOIFSFileSystem extends Bl
        // First up, do we have any spare ones?
        int offset = 0;
        for(int i=0; i<_bat_blocks.size(); i++) {
-          boolean isXBAT = (i >= _header.getBATCount());
-          
           int numSectors = bigBlockSize.getBATEntriesPerBlock();
-          if(isXBAT) {
-             numSectors = bigBlockSize.getXBATEntriesPerBlock();
-          }
 
           // Check this one
           BATBlock bat = _bat_blocks.get(i);
@@ -397,54 +403,62 @@ public class NPOIFSFileSystem extends Bl
              }
           }
           
-          // Move onto the next BAT/XBAT
+          // Move onto the next BAT
           offset += numSectors;
        }
        
-       // If we get here, then there aren't any
-       //  free sectors in any of the BATs or XBATs
-       // So, we need to extend the file and add another
-       boolean isBAT = true;
-       if(_header.getBATCount() >= 109) {
-          isBAT = false;
-       }
-       
-       // Create a new BATBlock
-       BATBlock newBAT = BATBlock.createEmptyBATBlock(bigBlockSize, !isBAT);
-       newBAT.setOurBlockIndex(offset);
-       // Ensure there's a spot in the file for it
-       ByteBuffer buffer = ByteBuffer.allocate(bigBlockSize.getBigBlockSize());
-       int writeTo = (1+offset) * bigBlockSize.getBigBlockSize(); // Header isn't in BATs
-       _data.write(buffer, writeTo);
+       // If we get here, then there aren't any free sectors
+       //  in any of the BATs, so we need another BAT
+       BATBlock bat = createBAT(offset, true);
+       bat.setValueAt(0, POIFSConstants.FAT_SECTOR_BLOCK);
+       _bat_blocks.add(bat);
        
-       // Allocate ourself within ourselves, at the first point
-       if(isBAT) {
-          newBAT.setValueAt(0, POIFSConstants.FAT_SECTOR_BLOCK);
+       // Now store a reference to the BAT in the required place 
+       if(_header.getBATCount() >= 109) {
+          // Needs to come from an XBAT
+          BATBlock xbat = null;
+          for(BATBlock x : _xbat_blocks) {
+             if(x.hasFreeSectors()) {
+                xbat = x;
+                break;
+             }
+          }
+          if(xbat == null) {
+             // Oh joy, we need a new XBAT too...
+             xbat = createBAT(offset+1, false);
+             xbat.setValueAt(0, offset);
+             bat.setValueAt(offset+1, POIFSConstants.DIFAT_SECTOR_BLOCK);
+             
+             // Will go one place higher as XBAT added in
+             offset++;
+             
+             // Chain it
+             if(_xbat_blocks.size() == 0) {
+                _header.setXBATStart(offset);
+             } else {
+                _xbat_blocks.get(_xbat_blocks.size()-1).setValueAt(
+                      bigBlockSize.getXBATEntriesPerBlock(), offset
+                );
+             }
+             _xbat_blocks.add(xbat);
+             _header.setXBATCount(_xbat_blocks.size());
+          }
+          // Allocate us in the XBAT
+          for(int i=0; i<bigBlockSize.getXBATEntriesPerBlock(); i++) {
+             if(xbat.getValueAt(i) == POIFSConstants.UNUSED_BLOCK) {
+                xbat.setValueAt(i, offset);
+             }
+          }
        } else {
-          newBAT.setValueAt(0, POIFSConstants.DIFAT_SECTOR_BLOCK);
-       }
-
-       // Store us
-       _bat_blocks.add(newBAT);
-       if(isBAT) {
-          // Put it in the BAT array in the header
+          // Store us in the header
           int[] newBATs = new int[_header.getBATCount()+1];
           System.arraycopy(_header.getBATArray(), 0, newBATs, 0, newBATs.length-1);
           newBATs[newBATs.length-1] = offset;
           _header.setBATArray(newBATs);
           _header.setBATCount(newBATs.length);
-       } else if(_header.getXBATCount() == 0) {
-          // Store our first XBAT offset in the header
-          _header.setXBATStart(offset);
-          _header.setXBATCount(1);
-       } else {
-          // Chain it off the last XBAT
-          BATBlock lastXBAT = _bat_blocks.get(_bat_blocks.size()-1);
-          lastXBAT.setValueAt(bigBlockSize.getNextXBATChainOffset(), offset);
-          _header.setXBATCount(_header.getXBATCount()+1);
        }
        
-       // The first offset stores us, but the 2nd is free
+       // The current offset stores us, but the next one is free
        return offset+1;
     }
     

Modified: poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java?rev=1053504&r1=1053503&r2=1053504&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/poifs/filesystem/TestNPOIFSFileSystem.java Wed Dec 29 01:34:56 2010
@@ -23,7 +23,6 @@ import java.util.Iterator;
 import junit.framework.TestCase;
 
 import org.apache.poi.POIDataSamples;
-import org.apache.poi.hpsf.DocumentSummaryInformation;
 import org.apache.poi.hpsf.PropertySet;
 import org.apache.poi.hpsf.PropertySetFactory;
 import org.apache.poi.hpsf.SummaryInformation;



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