public void writeFilesystem(OutputStream stream) throws IOException {
// get the property table ready
_property_table.preWrite();
// create the small block store, and the SBAT
SmallBlockTable sbt = new SmallBlockTable(_documents,
_property_table.getRoot());
// create the block allocation table
BlockAllocationTable bat = new BlockAllocationTable();
// create a list of BATManaged objects: the documents plus the
// property table and the small block table
List bm_objects = new ArrayList();
bm_objects.addAll(_documents);
bm_objects.add(_property_table);
bm_objects.add(sbt);
bm_objects.add(sbt.getSBAT());
// walk the list, allocating space for each and assigning each
// a starting block number
Iterator 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
HeaderBlock header_block = new HeaderBlock();
BATBlock[] xbat_blocks =
header_block.setBATBlocks(bat.countBlocks(), batStartBlock);
// set the property table start block
header_block.setPropertyStart(_property_table.getStartBlock());
// set the small block allocation table start block
header_block.setSBATStart(sbt.getSBAT().getStartBlock());
// 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 writers = new ArrayList();
writers.add(header_block);
writers.addAll(_documents);
writers.add(_property_table);
writers.add(sbt);
writers.add(sbt.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);
}
}
|