Search in sources :

Example 1 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class BATBlock method getBATBlockAndIndex.

/**
     * Returns the BATBlock that handles the specified offset,
     *  and the relative index within it.
     * The List of BATBlocks must be in sequential order
     */
public static BATBlockAndIndex getBATBlockAndIndex(final int offset, final HeaderBlock header, final List<BATBlock> bats) {
    POIFSBigBlockSize bigBlockSize = header.getBigBlockSize();
    int entriesPerBlock = bigBlockSize.getBATEntriesPerBlock();
    int whichBAT = offset / entriesPerBlock;
    int index = offset % entriesPerBlock;
    return new BATBlockAndIndex(index, bats.get(whichBAT));
}
Also used : POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize)

Example 2 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class HeaderBlockWriter method setBATBlocks.

/**
     * Set BAT block parameters. Assumes that all BAT blocks are
     * contiguous. Will construct XBAT blocks if necessary and return
     * the array of newly constructed XBAT blocks.
     *
     * @param blockCount count of BAT blocks
     * @param startBlock index of first BAT block
     *
     * @return array of XBAT blocks; may be zero length, will not be
     *         null
     */
public BATBlock[] setBATBlocks(final int blockCount, final int startBlock) {
    BATBlock[] rvalue;
    POIFSBigBlockSize bigBlockSize = _header_block.getBigBlockSize();
    _header_block.setBATCount(blockCount);
    // Set the BAT locations
    int limit = Math.min(blockCount, _max_bats_in_header);
    int[] bat_blocks = new int[limit];
    for (int j = 0; j < limit; j++) {
        bat_blocks[j] = startBlock + j;
    }
    _header_block.setBATArray(bat_blocks);
    // Now do the XBATs
    if (blockCount > _max_bats_in_header) {
        int excess_blocks = blockCount - _max_bats_in_header;
        int[] excess_block_array = new int[excess_blocks];
        for (int j = 0; j < excess_blocks; j++) {
            excess_block_array[j] = startBlock + j + _max_bats_in_header;
        }
        rvalue = BATBlock.createXBATBlocks(bigBlockSize, excess_block_array, startBlock + blockCount);
        _header_block.setXBATStart(startBlock + blockCount);
    } else {
        rvalue = BATBlock.createXBATBlocks(bigBlockSize, new int[0], 0);
        _header_block.setXBATStart(POIFSConstants.END_OF_CHAIN);
    }
    _header_block.setXBATCount(rvalue.length);
    return rvalue;
}
Also used : POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize)

Example 3 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class POIFSHeaderDumper method viewFile.

public static void viewFile(final String filename) throws Exception {
    System.out.println("Dumping headers from: " + filename);
    InputStream inp = new FileInputStream(filename);
    // Header
    HeaderBlock header_block = new HeaderBlock(inp);
    displayHeader(header_block);
    // Raw blocks
    POIFSBigBlockSize bigBlockSize = header_block.getBigBlockSize();
    RawDataBlockList data_blocks = new RawDataBlockList(inp, bigBlockSize);
    displayRawBlocksSummary(data_blocks);
    // Main FAT Table
    BlockAllocationTableReader batReader = new BlockAllocationTableReader(header_block.getBigBlockSize(), header_block.getBATCount(), header_block.getBATArray(), header_block.getXBATCount(), header_block.getXBATIndex(), data_blocks);
    displayBATReader("Big Blocks", batReader);
    // Properties Table
    PropertyTable properties = new PropertyTable(header_block, data_blocks);
    // Mini Fat
    BlockAllocationTableReader sbatReader = SmallBlockTableReader._getSmallDocumentBlockReader(bigBlockSize, data_blocks, properties.getRoot(), header_block.getSBATStart());
    displayBATReader("Small Blocks", sbatReader);
    // Summary of the properties
    displayPropertiesSummary(properties);
}
Also used : BlockAllocationTableReader(org.apache.poi.poifs.storage.BlockAllocationTableReader) PropertyTable(org.apache.poi.poifs.property.PropertyTable) HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize) FileInputStream(java.io.FileInputStream) RawDataBlockList(org.apache.poi.poifs.storage.RawDataBlockList)

Example 4 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class TestPOIFSFileSystem method test4KBlocks.

/**
	 * Most OLE2 files use 512byte blocks. However, a small number
	 *  use 4k blocks. Check that we can open these.
	 */
public void test4KBlocks() throws Exception {
    POIDataSamples _samples = POIDataSamples.getPOIFSInstance();
    InputStream inp = _samples.openResourceAsStream("BlockSize4096.zvi");
    try {
        // First up, check that we can process the header properly
        HeaderBlock header_block = new HeaderBlock(inp);
        POIFSBigBlockSize bigBlockSize = header_block.getBigBlockSize();
        assertEquals(4096, bigBlockSize.getBigBlockSize());
        // Check the fat info looks sane
        assertEquals(1, header_block.getBATArray().length);
        assertEquals(1, header_block.getBATCount());
        assertEquals(0, header_block.getXBATCount());
        // Now check we can get the basic fat
        RawDataBlockList data_blocks = new RawDataBlockList(inp, bigBlockSize);
        assertEquals(15, data_blocks.blockCount());
        // Now try and open properly
        OPOIFSFileSystem fs = new OPOIFSFileSystem(_samples.openResourceAsStream("BlockSize4096.zvi"));
        assertTrue(fs.getRoot().getEntryCount() > 3);
        // Check we can get at all the contents
        checkAllDirectoryContents(fs.getRoot());
        // Finally, check we can do a similar 512byte one too
        fs = new OPOIFSFileSystem(_samples.openResourceAsStream("BlockSize512.zvi"));
        assertTrue(fs.getRoot().getEntryCount() > 3);
        checkAllDirectoryContents(fs.getRoot());
    } finally {
        inp.close();
    }
}
Also used : HeaderBlock(org.apache.poi.poifs.storage.HeaderBlock) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize) POIDataSamples(org.apache.poi.POIDataSamples) RawDataBlockList(org.apache.poi.poifs.storage.RawDataBlockList)

Example 5 with POIFSBigBlockSize

use of org.apache.poi.poifs.common.POIFSBigBlockSize in project poi by apache.

the class TestBATBlock method testUsedSectors.

public void testUsedSectors() throws Exception {
    POIFSBigBlockSize b512 = POIFSConstants.SMALLER_BIG_BLOCK_SIZE_DETAILS;
    POIFSBigBlockSize b4096 = POIFSConstants.LARGER_BIG_BLOCK_SIZE_DETAILS;
    // Try first with 512 block sizes, which can hold 128 entries
    BATBlock block512 = BATBlock.createEmptyBATBlock(b512, false);
    assertEquals(true, block512.hasFreeSectors());
    assertEquals(0, block512.getUsedSectors(false));
    // Allocate a few
    block512.setValueAt(0, 42);
    block512.setValueAt(10, 42);
    block512.setValueAt(20, 42);
    assertEquals(true, block512.hasFreeSectors());
    assertEquals(3, block512.getUsedSectors(false));
    // Allocate all
    for (int i = 0; i < b512.getBATEntriesPerBlock(); i++) {
        block512.setValueAt(i, 82);
    }
    // Check
    assertEquals(false, block512.hasFreeSectors());
    assertEquals(128, block512.getUsedSectors(false));
    assertEquals(127, block512.getUsedSectors(true));
    // Release one
    block512.setValueAt(10, POIFSConstants.UNUSED_BLOCK);
    assertEquals(true, block512.hasFreeSectors());
    assertEquals(127, block512.getUsedSectors(false));
    assertEquals(126, block512.getUsedSectors(true));
    // Now repeat with 4096 block sizes
    BATBlock block4096 = BATBlock.createEmptyBATBlock(b4096, false);
    assertEquals(true, block4096.hasFreeSectors());
    assertEquals(0, block4096.getUsedSectors(false));
    block4096.setValueAt(0, 42);
    block4096.setValueAt(10, 42);
    block4096.setValueAt(20, 42);
    assertEquals(true, block4096.hasFreeSectors());
    assertEquals(3, block4096.getUsedSectors(false));
    // Allocate all
    for (int i = 0; i < b4096.getBATEntriesPerBlock(); i++) {
        block4096.setValueAt(i, 82);
    }
    // Check
    assertEquals(false, block4096.hasFreeSectors());
    assertEquals(1024, block4096.getUsedSectors(false));
    assertEquals(1023, block4096.getUsedSectors(true));
}
Also used : POIFSBigBlockSize(org.apache.poi.poifs.common.POIFSBigBlockSize)

Aggregations

POIFSBigBlockSize (org.apache.poi.poifs.common.POIFSBigBlockSize)8 InputStream (java.io.InputStream)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 HeaderBlock (org.apache.poi.poifs.storage.HeaderBlock)2 RawDataBlockList (org.apache.poi.poifs.storage.RawDataBlockList)2 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 POIDataSamples (org.apache.poi.POIDataSamples)1 PropertyTable (org.apache.poi.poifs.property.PropertyTable)1 BlockAllocationTableReader (org.apache.poi.poifs.storage.BlockAllocationTableReader)1