Examples of BATBlock


Examples of org.apache.poi.poifs.storage.BATBlock

       for(int fatAt : _header.getBATArray()) {
          readBAT(fatAt, loopDetector);
       }
      
       // Now read the XFAT blocks, and the FATs within them
       BATBlock xfat;
       int nextAt = _header.getXBATIndex();
       for(int i=0; i<_header.getXBATCount(); i++) {
          loopDetector.claim(nextAt);
          ByteBuffer fatData = getBlockAt(nextAt);
          xfat = BATBlock.createBATBlock(bigBlockSize, fatData);
          xfat.setOurBlockIndex(nextAt);
          nextAt = xfat.getValueAt(bigBlockSize.getXBATEntriesPerBlock());
          _xbat_blocks.add(xfat);
         
          for(int j=0; j<bigBlockSize.getXBATEntriesPerBlock(); j++) {
             int fatAt = xfat.getValueAt(j);
             if(fatAt == POIFSConstants.UNUSED_BLOCK) break;
             readBAT(fatAt, loopDetector);
          }
       }
      
       // We're now able to load steams
       // Use this to read in the properties
       _property_table = new NPropertyTable(_header, this);
      
       // Finally read the Small Stream FAT (SBAT) blocks
       BATBlock sfat;
       List<BATBlock> sbats = new ArrayList<BATBlock>();
       _mini_store     = new NPOIFSMiniStore(this, _property_table.getRoot(), sbats, _header);
       nextAt = _header.getSBATStart();
       for(int i=0; i<_header.getSBATCount(); i++) {
          loopDetector.claim(nextAt);
          ByteBuffer fatData = getBlockAt(nextAt);
          sfat = BATBlock.createBATBlock(bigBlockSize, fatData);
          sfat.setOurBlockIndex(nextAt);
          sbats.add(sfat);
          nextAt = getNextBlock(nextAt)
       }
    }
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

       }
    }
    private void readBAT(int batAt, ChainLoopDetector loopDetector) throws IOException {
       loopDetector.claim(batAt);
       ByteBuffer fatData = getBlockAt(batAt);
       BATBlock bat = BATBlock.createBATBlock(bigBlockSize, fatData);
       bat.setOurBlockIndex(batAt);
       _bat_blocks.add(bat);
    }
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

       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
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

       int offset = 0;
       for(int i=0; i<_bat_blocks.size(); i++) {
          int numSectors = bigBlockSize.getBATEntriesPerBlock();

          // Check this one
          BATBlock bat = _bat_blocks.get(i);
          if(bat.hasFreeSectors()) {
             // Claim one of them and return it
             for(int j=0; j<numSectors; j++) {
                int batValue = bat.getValueAt(j);
                if(batValue == POIFSConstants.UNUSED_BLOCK) {
                   // Bingo
                   return offset + j;
                }
             }
          }
         
          // Move onto the next BAT
          offset += numSectors;
       }
      
       // 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);
      
       // 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(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 {
          // Store us in the header
          int[] newBATs = new int[_header.getBATCount()+1];
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

       for(int fatAt : _header.getBATArray()) {
          readBAT(fatAt, loopDetector);
       }
      
       // Now read the XFAT blocks, and the FATs within them
       BATBlock xfat;
       int nextAt = _header.getXBATIndex();
       for(int i=0; i<_header.getXBATCount(); i++) {
          loopDetector.claim(nextAt);
          ByteBuffer fatData = getBlockAt(nextAt);
          xfat = BATBlock.createBATBlock(bigBlockSize, fatData);
          xfat.setOurBlockIndex(nextAt);
          nextAt = xfat.getValueAt(bigBlockSize.getXBATEntriesPerBlock());
          _xbat_blocks.add(xfat);
         
          for(int j=0; j<bigBlockSize.getXBATEntriesPerBlock(); j++) {
             int fatAt = xfat.getValueAt(j);
             if(fatAt == POIFSConstants.UNUSED_BLOCK) break;
             readBAT(fatAt, loopDetector);
          }
       }
      
       // We're now able to load steams
       // Use this to read in the properties
       _property_table = new NPropertyTable(_header, this);
      
       // Finally read the Small Stream FAT (SBAT) blocks
       BATBlock sfat;
       List<BATBlock> sbats = new ArrayList<BATBlock>();
       _mini_store     = new NPOIFSMiniStore(this, _property_table.getRoot(), sbats, _header);
       nextAt = _header.getSBATStart();
       for(int i=0; i<_header.getSBATCount(); i++) {
          loopDetector.claim(nextAt);
          ByteBuffer fatData = getBlockAt(nextAt);
          sfat = BATBlock.createBATBlock(bigBlockSize, fatData);
          sfat.setOurBlockIndex(nextAt);
          sbats.add(sfat);
          nextAt = getNextBlock(nextAt)
       }
    }
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

       }
    }
    private void readBAT(int batAt, ChainLoopDetector loopDetector) throws IOException {
       loopDetector.claim(batAt);
       ByteBuffer fatData = getBlockAt(batAt);
       BATBlock bat = BATBlock.createBATBlock(bigBlockSize, fatData);
       bat.setOurBlockIndex(batAt);
       _bat_blocks.add(bat);
    }
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

       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
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

       int offset = 0;
       for(int i=0; i<_bat_blocks.size(); i++) {
          int numSectors = bigBlockSize.getBATEntriesPerBlock();

          // Check this one
          BATBlock bat = _bat_blocks.get(i);
          if(bat.hasFreeSectors()) {
             // Claim one of them and return it
             for(int j=0; j<numSectors; j++) {
                int batValue = bat.getValueAt(j);
                if(batValue == POIFSConstants.UNUSED_BLOCK) {
                   // Bingo
                   return offset + j;
                }
             }
          }
         
          // Move onto the next BAT
          offset += numSectors;
       }
      
       // 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);
      
       // 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(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 {
          // Store us in the header
          int[] newBATs = new int[_header.getBATCount()+1];
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

      NPOIFSFileSystem fs = new NPOIFSFileSystem();
      NPOIFSStream stream = new NPOIFSStream(fs);
     
      // Check our filesystem has a BAT and the Properties
      assertEquals(2, fs.getFreeBlock());
      BATBlock bat = fs.getBATBlockAndIndex(0).getBlock();
      assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
      assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
      assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(2));
     
      // 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(4, fs.getFreeBlock());
      bat = fs.getBATBlockAndIndex(0).getBlock();
      assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
      assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
      assertEquals(3,                           bat.getValueAt(2));
      assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(3));
      assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(4));
     
     
      Iterator<ByteBuffer> it = stream.getBlockIterator();
      assertEquals(true, it.hasNext());
      ByteBuffer b = it.next();
View Full Code Here

Examples of org.apache.poi.poifs.storage.BATBlock

    */
   public void testWriteThenReplace() throws Exception {
       NPOIFSFileSystem fs = new NPOIFSFileSystem();

       // Starts empty
       BATBlock bat = fs.getBATBlockAndIndex(0).getBlock();
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(2));

       // Write something that uses a main stream
       byte[] main4106 = new byte[4106];
       main4106[0] = -10;
       main4106[4105] = -11;
       DocumentEntry normal = fs.getRoot().createDocument(
               "Normal", new ByteArrayInputStream(main4106));
      
       // Should have used 9 blocks
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(3,                           bat.getValueAt(2));
       assertEquals(4,                           bat.getValueAt(3));
       assertEquals(5,                           bat.getValueAt(4));
       assertEquals(6,                           bat.getValueAt(5));
       assertEquals(7,                           bat.getValueAt(6));
       assertEquals(8,                           bat.getValueAt(7));
       assertEquals(9,                           bat.getValueAt(8));
       assertEquals(10,                          bat.getValueAt(9));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(10));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(11));
      
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       assertEquals(4106, normal.getSize());
       assertEquals(4106, ((DocumentNode)normal).getProperty().getSize());

      
       // Replace with one still big enough for a main stream, but one block smaller
       byte[] main4096 = new byte[4096];
       main4096[0] = -10;
       main4096[4095] = -11;
      
       NDocumentOutputStream nout = new NDocumentOutputStream(normal);
       nout.write(main4096);
       nout.close();
      
       // Will have dropped to 8
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(3,                           bat.getValueAt(2));
       assertEquals(4,                           bat.getValueAt(3));
       assertEquals(5,                           bat.getValueAt(4));
       assertEquals(6,                           bat.getValueAt(5));
       assertEquals(7,                           bat.getValueAt(6));
       assertEquals(8,                           bat.getValueAt(7));
       assertEquals(9,                           bat.getValueAt(8));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(9));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(10));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(11));

       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       assertEquals(4096, normal.getSize());
       assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
      
      
       // Write and check
       fs = writeOutAndReadBack(fs);
       bat = fs.getBATBlockAndIndex(0).getBlock();
      
       // Will have properties, but otherwise the same
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(3,                           bat.getValueAt(2));
       assertEquals(4,                           bat.getValueAt(3));
       assertEquals(5,                           bat.getValueAt(4));
       assertEquals(6,                           bat.getValueAt(5));
       assertEquals(7,                           bat.getValueAt(6));
       assertEquals(8,                           bat.getValueAt(7));
       assertEquals(9,                           bat.getValueAt(8));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(9)); // End of Normal
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(10)); // Props
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(11));
      
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       assertEquals(4096, normal.getSize());
       assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
      
      
       // Make longer, take 1 block after the properties too
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       nout = new NDocumentOutputStream(normal);
       nout.write(main4106);
       nout.close();

       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(3,                           bat.getValueAt(2));
       assertEquals(4,                           bat.getValueAt(3));
       assertEquals(5,                           bat.getValueAt(4));
       assertEquals(6,                           bat.getValueAt(5));
       assertEquals(7,                           bat.getValueAt(6));
       assertEquals(8,                           bat.getValueAt(7));
       assertEquals(9,                           bat.getValueAt(8));
       assertEquals(11,                          bat.getValueAt(9));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(10)); // Props
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(11)); // Normal
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(12));
      
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       assertEquals(4106, normal.getSize());
       assertEquals(4106, ((DocumentNode)normal).getProperty().getSize());

      
       // Make it small, will trigger the SBAT stream and free lots up
       byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 };
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       nout = new NDocumentOutputStream(normal);
       nout.write(mini);
       nout.close();
      
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(2)); // SBAT
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(3)); // Mini Stream
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(4));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(5));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(6));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(7));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(8));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(9));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(10)); // Props
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(11));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(12));
      
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       assertEquals(7, normal.getSize());
       assertEquals(7, ((DocumentNode)normal).getProperty().getSize());

      
       // Finally back to big again
       nout = new NDocumentOutputStream(normal);
       nout.write(main4096);
       nout.close();
      
       // Will keep the mini stream, now empty
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(2)); // SBAT
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(3)); // Mini Stream
       assertEquals(5,                           bat.getValueAt(4));
       assertEquals(6,                           bat.getValueAt(5));
       assertEquals(7,                           bat.getValueAt(6));
       assertEquals(8,                           bat.getValueAt(7));
       assertEquals(9,                           bat.getValueAt(8));
       assertEquals(11,                          bat.getValueAt(9));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(10)); // Props
       assertEquals(12,                          bat.getValueAt(11));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(12));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(13));
      
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       assertEquals(4096, normal.getSize());
       assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
      
      
       // Save, re-load, re-check
       fs = writeOutAndReadBack(fs);
       bat = fs.getBATBlockAndIndex(0).getBlock();
      
       assertEquals(POIFSConstants.FAT_SECTOR_BLOCK, bat.getValueAt(0));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(1));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(2)); // SBAT
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(3)); // Mini Stream
       assertEquals(5,                           bat.getValueAt(4));
       assertEquals(6,                           bat.getValueAt(5));
       assertEquals(7,                           bat.getValueAt(6));
       assertEquals(8,                           bat.getValueAt(7));
       assertEquals(9,                           bat.getValueAt(8));
       assertEquals(11,                          bat.getValueAt(9));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(10)); // Props
       assertEquals(12,                          bat.getValueAt(11));
       assertEquals(POIFSConstants.END_OF_CHAIN, bat.getValueAt(12));
       assertEquals(POIFSConstants.UNUSED_BLOCK, bat.getValueAt(13));
      
       normal = (DocumentEntry)fs.getRoot().getEntry("Normal");
       assertEquals(4096, normal.getSize());
       assertEquals(4096, ((DocumentNode)normal).getProperty().getSize());
      
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.