org.apache.poi.poifs.storage
Class BATBlock

java.lang.Object
  extended by org.apache.poi.poifs.storage.BATBlock
All Implemented Interfaces:
BlockWritable

public final class BATBlock
extends java.lang.Object

A block of block allocation table entries. BATBlocks are created only through a static factory method: createBATBlocks.

Author:
Marc Johnson (mjohnson at apache dot org)

Nested Class Summary
static class BATBlock.BATBlockAndIndex
           
 
Field Summary
protected  POIFSBigBlockSize bigBlockSize
          Either 512 bytes (POIFSConstants.SMALLER_BIG_BLOCK_SIZE) or 4096 bytes (POIFSConstants.LARGER_BIG_BLOCK_SIZE)
 
Method Summary
static int calculateMaximumSize(HeaderBlock header)
           
static int calculateMaximumSize(POIFSBigBlockSize bigBlockSize, int numBATs)
          Calculates the maximum size of a file which is addressable given the number of FAT (BAT) sectors specified.
static int calculateStorageRequirements(POIFSBigBlockSize bigBlockSize, int entryCount)
          Calculate how many BATBlocks are needed to hold a specified number of BAT entries.
static int calculateXBATStorageRequirements(POIFSBigBlockSize bigBlockSize, int entryCount)
          Calculate how many XBATBlocks are needed to hold a specified number of BAT entries.
static BATBlock createBATBlock(POIFSBigBlockSize bigBlockSize, java.nio.ByteBuffer data)
          Create a single BATBlock from the byte buffer, which must hold at least one big block of data to be read.
static BATBlock[] createBATBlocks(POIFSBigBlockSize bigBlockSize, int[] entries)
          Create an array of BATBlocks from an array of int block allocation table entries
static BATBlock createEmptyBATBlock(POIFSBigBlockSize bigBlockSize, boolean isXBAT)
          Creates a single BATBlock, with all the values set to empty.
static BATBlock[] createXBATBlocks(POIFSBigBlockSize bigBlockSize, int[] entries, int startBlock)
          Create an array of XBATBlocks from an array of int block allocation table entries
protected  void doWriteData(java.io.OutputStream stream, byte[] data)
          Default implementation of write for extending classes that contain their data in a simple array of bytes.
static BATBlock.BATBlockAndIndex getBATBlockAndIndex(int offset, HeaderBlock header, java.util.List<BATBlock> bats)
          Returns the BATBlock that handles the specified offset, and the relative index within it.
 int getOurBlockIndex()
          Retrieve where in the file we live
static BATBlock.BATBlockAndIndex getSBATBlockAndIndex(int offset, HeaderBlock header, java.util.List<BATBlock> sbats)
          Returns the BATBlock that handles the specified offset, and the relative index within it, for the mini stream.
 int getValueAt(int relativeOffset)
           
 boolean hasFreeSectors()
          Does this BATBlock have any free sectors in it, or is it full?
 void setOurBlockIndex(int index)
          Record where in the file we live
 void setValueAt(int relativeOffset, int value)
           
 void writeBlocks(java.io.OutputStream stream)
          Write the storage to an OutputStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

bigBlockSize

protected POIFSBigBlockSize bigBlockSize
Either 512 bytes (POIFSConstants.SMALLER_BIG_BLOCK_SIZE) or 4096 bytes (POIFSConstants.LARGER_BIG_BLOCK_SIZE)

Method Detail

createBATBlock

public static BATBlock createBATBlock(POIFSBigBlockSize bigBlockSize,
                                      java.nio.ByteBuffer data)
Create a single BATBlock from the byte buffer, which must hold at least one big block of data to be read.


createEmptyBATBlock

public static BATBlock createEmptyBATBlock(POIFSBigBlockSize bigBlockSize,
                                           boolean isXBAT)
Creates a single BATBlock, with all the values set to empty.


createBATBlocks

public static BATBlock[] createBATBlocks(POIFSBigBlockSize bigBlockSize,
                                         int[] entries)
Create an array of BATBlocks from an array of int block allocation table entries

Parameters:
entries - the array of int entries
Returns:
the newly created array of BATBlocks

createXBATBlocks

public static BATBlock[] createXBATBlocks(POIFSBigBlockSize bigBlockSize,
                                          int[] entries,
                                          int startBlock)
Create an array of XBATBlocks from an array of int block allocation table entries

Parameters:
entries - the array of int entries
startBlock - the start block of the array of XBAT blocks
Returns:
the newly created array of BATBlocks

calculateStorageRequirements

public static int calculateStorageRequirements(POIFSBigBlockSize bigBlockSize,
                                               int entryCount)
Calculate how many BATBlocks are needed to hold a specified number of BAT entries.

Parameters:
entryCount - the number of entries
Returns:
the number of BATBlocks needed

calculateXBATStorageRequirements

public static int calculateXBATStorageRequirements(POIFSBigBlockSize bigBlockSize,
                                                   int entryCount)
Calculate how many XBATBlocks are needed to hold a specified number of BAT entries.

Parameters:
entryCount - the number of entries
Returns:
the number of XBATBlocks needed

calculateMaximumSize

public static int calculateMaximumSize(POIFSBigBlockSize bigBlockSize,
                                       int numBATs)
Calculates the maximum size of a file which is addressable given the number of FAT (BAT) sectors specified. (We don't care if those BAT blocks come from the 109 in the header, or from header + XBATS, it won't affect the calculation) The actual file size will be between [size of fatCount-1 blocks] and [size of fatCount blocks]. For 512 byte block sizes, this means we may over-estimate by up to 65kb. For 4096 byte block sizes, this means we may over-estimate by up to 4mb


calculateMaximumSize

public static int calculateMaximumSize(HeaderBlock header)

getBATBlockAndIndex

public static BATBlock.BATBlockAndIndex getBATBlockAndIndex(int offset,
                                                            HeaderBlock header,
                                                            java.util.List<BATBlock> bats)
Returns the BATBlock that handles the specified offset, and the relative index within it. The List of BATBlocks must be in sequential order


getSBATBlockAndIndex

public static BATBlock.BATBlockAndIndex getSBATBlockAndIndex(int offset,
                                                             HeaderBlock header,
                                                             java.util.List<BATBlock> sbats)
Returns the BATBlock that handles the specified offset, and the relative index within it, for the mini stream. The List of BATBlocks must be in sequential order


hasFreeSectors

public boolean hasFreeSectors()
Does this BATBlock have any free sectors in it, or is it full?


getValueAt

public int getValueAt(int relativeOffset)

setValueAt

public void setValueAt(int relativeOffset,
                       int value)

setOurBlockIndex

public void setOurBlockIndex(int index)
Record where in the file we live


getOurBlockIndex

public int getOurBlockIndex()
Retrieve where in the file we live


doWriteData

protected void doWriteData(java.io.OutputStream stream,
                           byte[] data)
                    throws java.io.IOException
Default implementation of write for extending classes that contain their data in a simple array of bytes.

Parameters:
stream - the OutputStream to which the data should be written.
data - the byte array of to be written.
Throws:
java.io.IOException - on problems writing to the specified stream.

writeBlocks

public void writeBlocks(java.io.OutputStream stream)
                 throws java.io.IOException
Write the storage to an OutputStream

Specified by:
writeBlocks in interface BlockWritable
Parameters:
stream - the OutputStream to which the stored data should be written
Throws:
java.io.IOException - on problems writing to the specified stream


Copyright 2012 The Apache Software Foundation or its licensors, as applicable.