Search in sources :

Example 1 with HeaderBlockWriter

use of org.apache.poi.poifs.storage.HeaderBlockWriter in project poi by apache.

the class OPOIFSFileSystem method writeFilesystem.

/**
     * Write the filesystem out
     *
     * @param stream the OutputStream to which the filesystem will be
     *               written
     *
     * @exception IOException thrown on errors writing to the stream
     */
public void writeFilesystem(final OutputStream stream) throws IOException {
    // get the property table ready
    _property_table.preWrite();
    // create the small block store, and the SBAT
    SmallBlockTableWriter sbtw = new SmallBlockTableWriter(bigBlockSize, _documents, _property_table.getRoot());
    // create the block allocation table
    BlockAllocationTableWriter bat = new BlockAllocationTableWriter(bigBlockSize);
    // create a list of BATManaged objects: the documents plus the
    // property table and the small block table
    List<Object> bm_objects = new ArrayList<Object>();
    bm_objects.addAll(_documents);
    bm_objects.add(_property_table);
    bm_objects.add(sbtw);
    bm_objects.add(sbtw.getSBAT());
    // walk the list, allocating space for each and assigning each
    // a starting block number
    Iterator<Object> iter = bm_objects.iterator();
    while (iter.hasNext()) {
        BATManaged bmo = (BATManaged) iter.next();
        int block_count = bmo.countBlocks();
        if (block_count != 0) {
            bmo.setStartBlock(bat.allocateSpace(block_count));
        } else {
        // Either the BATManaged object is empty or its data
        // is composed of SmallBlocks; in either case,
        // allocating space in the BAT is inappropriate
        }
    }
    // allocate space for the block allocation table and take its
    // starting block
    int batStartBlock = bat.createBlocks();
    // get the extended block allocation table blocks
    HeaderBlockWriter header_block_writer = new HeaderBlockWriter(bigBlockSize);
    BATBlock[] xbat_blocks = header_block_writer.setBATBlocks(bat.countBlocks(), batStartBlock);
    // set the property table start block
    header_block_writer.setPropertyStart(_property_table.getStartBlock());
    // set the small block allocation table start block
    header_block_writer.setSBATStart(sbtw.getSBAT().getStartBlock());
    // set the small block allocation table block count
    header_block_writer.setSBATBlockCount(sbtw.getSBATBlockCount());
    // the header is now properly initialized. Make a list of
    // writers (the header block, followed by the documents, the
    // property table, the small block store, the small block
    // allocation table, the block allocation table, and the
    // extended block allocation table blocks)
    List<Object> writers = new ArrayList<Object>();
    writers.add(header_block_writer);
    writers.addAll(_documents);
    writers.add(_property_table);
    writers.add(sbtw);
    writers.add(sbtw.getSBAT());
    writers.add(bat);
    for (int j = 0; j < xbat_blocks.length; j++) {
        writers.add(xbat_blocks[j]);
    }
    // now, write everything out
    iter = writers.iterator();
    while (iter.hasNext()) {
        BlockWritable writer = (BlockWritable) iter.next();
        writer.writeBlocks(stream);
    }
}
Also used : BlockWritable(org.apache.poi.poifs.storage.BlockWritable) SmallBlockTableWriter(org.apache.poi.poifs.storage.SmallBlockTableWriter) HeaderBlockWriter(org.apache.poi.poifs.storage.HeaderBlockWriter) BATBlock(org.apache.poi.poifs.storage.BATBlock) ArrayList(java.util.ArrayList) BlockAllocationTableWriter(org.apache.poi.poifs.storage.BlockAllocationTableWriter)

Example 2 with HeaderBlockWriter

use of org.apache.poi.poifs.storage.HeaderBlockWriter in project poi by apache.

the class NPOIFSFileSystem method syncWithDataSource.

/**
     * Has our in-memory objects write their state
     *  to their backing blocks 
     */
private void syncWithDataSource() throws IOException {
    // Mini Stream + SBATs first, as mini-stream details have
    //  to be stored in the Root Property
    _mini_store.syncWithDataSource();
    // Properties
    NPOIFSStream propStream = new NPOIFSStream(this, _header.getPropertyStart());
    _property_table.preWrite();
    _property_table.write(propStream);
    // _header.setPropertyStart has been updated on write ...
    // HeaderBlock
    HeaderBlockWriter hbw = new HeaderBlockWriter(_header);
    hbw.writeBlock(getBlockAt(-1));
    // BATs
    for (BATBlock bat : _bat_blocks) {
        ByteBuffer block = getBlockAt(bat.getOurBlockIndex());
        BlockAllocationTableWriter.writeBlock(bat, block);
    }
    // XBats
    for (BATBlock bat : _xbat_blocks) {
        ByteBuffer block = getBlockAt(bat.getOurBlockIndex());
        BlockAllocationTableWriter.writeBlock(bat, block);
    }
}
Also used : HeaderBlockWriter(org.apache.poi.poifs.storage.HeaderBlockWriter) BATBlock(org.apache.poi.poifs.storage.BATBlock) ByteBuffer(java.nio.ByteBuffer)

Aggregations

BATBlock (org.apache.poi.poifs.storage.BATBlock)2 HeaderBlockWriter (org.apache.poi.poifs.storage.HeaderBlockWriter)2 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1 BlockAllocationTableWriter (org.apache.poi.poifs.storage.BlockAllocationTableWriter)1 BlockWritable (org.apache.poi.poifs.storage.BlockWritable)1 SmallBlockTableWriter (org.apache.poi.poifs.storage.SmallBlockTableWriter)1