001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.storage;
019:
020: import org.apache.poi.poifs.common.POIFSConstants;
021: import org.apache.poi.poifs.filesystem.BATManaged;
022: import org.apache.poi.poifs.filesystem.POIFSDocument;
023: import org.apache.poi.poifs.property.RootProperty;
024:
025: import java.util.*;
026:
027: import java.io.*;
028:
029: /**
030: * This class implements storage for writing the small blocks used by
031: * small documents.
032: *
033: * @author Marc Johnson (mjohnson at apache dot org)
034: */
035:
036: public class SmallBlockTableWriter implements BlockWritable, BATManaged {
037: private BlockAllocationTableWriter _sbat;
038: private List _small_blocks;
039: private int _big_block_count;
040: private RootProperty _root;
041:
042: /**
043: * Creates new SmallBlockTable
044: *
045: * @param documents a List of POIFSDocument instances
046: * @param root the Filesystem's root property
047: */
048:
049: public SmallBlockTableWriter(final List documents,
050: final RootProperty root) {
051: _sbat = new BlockAllocationTableWriter();
052: _small_blocks = new ArrayList();
053: _root = root;
054: Iterator iter = documents.iterator();
055:
056: while (iter.hasNext()) {
057: POIFSDocument doc = (POIFSDocument) iter.next();
058: BlockWritable[] blocks = doc.getSmallBlocks();
059:
060: if (blocks.length != 0) {
061: doc.setStartBlock(_sbat.allocateSpace(blocks.length));
062: for (int j = 0; j < blocks.length; j++) {
063: _small_blocks.add(blocks[j]);
064: }
065: } else {
066: doc.setStartBlock(POIFSConstants.END_OF_CHAIN);
067: }
068: }
069: _sbat.simpleCreateBlocks();
070: _root.setSize(_small_blocks.size());
071: _big_block_count = SmallDocumentBlock.fill(_small_blocks);
072: }
073:
074: /**
075: * Get the number of SBAT blocks
076: *
077: * @return number of SBAT big blocks
078: */
079:
080: public int getSBATBlockCount() {
081: return (_big_block_count + 15) / 16;
082: }
083:
084: /**
085: * Get the SBAT
086: *
087: * @return the Small Block Allocation Table
088: */
089:
090: public BlockAllocationTableWriter getSBAT() {
091: return _sbat;
092: }
093:
094: /* ********** START implementation of BATManaged ********** */
095:
096: /**
097: * Return the number of BigBlock's this instance uses
098: *
099: * @return count of BigBlock instances
100: */
101:
102: public int countBlocks() {
103: return _big_block_count;
104: }
105:
106: /**
107: * Set the start block for this instance
108: *
109: * @param start_block
110: */
111:
112: public void setStartBlock(int start_block) {
113: _root.setStartBlock(start_block);
114: }
115:
116: /* ********** END implementation of BATManaged ********** */
117: /* ********** START implementation of BlockWritable ********** */
118:
119: /**
120: * Write the storage to an OutputStream
121: *
122: * @param stream the OutputStream to which the stored data should
123: * be written
124: *
125: * @exception IOException on problems writing to the specified
126: * stream
127: */
128:
129: public void writeBlocks(final OutputStream stream)
130: throws IOException {
131: Iterator iter = _small_blocks.iterator();
132:
133: while (iter.hasNext()) {
134: ((BlockWritable) iter.next()).writeBlocks(stream);
135: }
136: }
137:
138: /* ********** END implementation of BlockWritable ********** */
139: }
|