FileDocCategorySizeDatePackage
BlockAllocationTableWriter.javaAPI DocApache Poi 3.0.15627Mon Jan 01 12:39:36 GMT 2007org.apache.poi.poifs.storage

BlockAllocationTableWriter

public class BlockAllocationTableWriter extends Object implements BlockWritable, BATManaged
This class manages and creates the Block Allocation Table, which is basically a set of linked lists of block indices.

Each block of the filesystem has an index. The first block, the header, is skipped; the first block after the header is index 0, the next is index 1, and so on.

A block's index is also its index into the Block Allocation Table. The entry that it finds in the Block Allocation Table is the index of the next block in the linked list of blocks making up a file, or it is set to -2: end of list.

author
Marc Johnson (mjohnson at apache dot org)

Fields Summary
private IntList
_entries
private BATBlock[]
_blocks
private int
_start_block
Constructors Summary
public BlockAllocationTableWriter()
create a BlockAllocationTableWriter

        _start_block = POIFSConstants.END_OF_CHAIN;
        _entries     = new IntList();
        _blocks      = new BATBlock[ 0 ];
    
Methods Summary
public intallocateSpace(int blockCount)
Allocate space for a block of indices

param
blockCount the number of blocks to allocate space for
return
the starting index of the blocks

        int startBlock = _entries.size();

        if (blockCount > 0)
        {
            int limit = blockCount - 1;
            int index = startBlock + 1;

            for (int k = 0; k < limit; k++)
            {
                _entries.add(index++);
            }
            _entries.add(POIFSConstants.END_OF_CHAIN);
        }
        return startBlock;
    
public intcountBlocks()
Return the number of BigBlock's this instance uses

return
count of BigBlock instances

        return _blocks.length;
    
public intcreateBlocks()
Create the BATBlocks we need

return
start block index of BAT blocks

        int xbat_blocks = 0;
        int bat_blocks  = 0;

        while (true)
        {
            int calculated_bat_blocks  =
                BATBlock.calculateStorageRequirements(bat_blocks
                                                      + xbat_blocks
                                                      + _entries.size());
            int calculated_xbat_blocks =
                HeaderBlockWriter
                    .calculateXBATStorageRequirements(calculated_bat_blocks);

            if ((bat_blocks == calculated_bat_blocks)
                    && (xbat_blocks == calculated_xbat_blocks))
            {

                // stable ... we're OK
                break;
            }
            else
            {
                bat_blocks  = calculated_bat_blocks;
                xbat_blocks = calculated_xbat_blocks;
            }
        }
        int startBlock = allocateSpace(bat_blocks);

        allocateSpace(xbat_blocks);
        simpleCreateBlocks();
        return startBlock;
    
public intgetStartBlock()
get the starting block

return
the starting block index

        return _start_block;
    
public voidsetStartBlock(int start_block)
Set the start block for this instance

param
start_block

        _start_block = start_block;
    
voidsimpleCreateBlocks()
create the BATBlocks

        _blocks = BATBlock.createBATBlocks(_entries.toArray());
    
public voidwriteBlocks(java.io.OutputStream stream)
Write the storage to an OutputStream

param
stream the OutputStream to which the stored data should be written
exception
IOException on problems writing to the specified stream

        for (int j = 0; j < _blocks.length; j++)
        {
            _blocks[ j ].writeBlocks(stream);
        }