1 2 17 18 19 package org.apache.poi.poifs.storage; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 24 import java.util.*; 25 26 import org.apache.poi.poifs.common.POIFSConstants; 27 import org.apache.poi.poifs.filesystem.BATManaged; 28 import org.apache.poi.util.IntList; 29 import org.apache.poi.util.LittleEndian; 30 import org.apache.poi.util.LittleEndianConsts; 31 32 47 48 public class BlockAllocationTableWriter 49 implements BlockWritable, BATManaged 50 { 51 private IntList _entries; 52 private BATBlock[] _blocks; 53 private int _start_block; 54 55 58 59 public BlockAllocationTableWriter() 60 { 61 _start_block = POIFSConstants.END_OF_CHAIN; 62 _entries = new IntList(); 63 _blocks = new BATBlock[ 0 ]; 64 } 65 66 71 72 public int createBlocks() 73 { 74 int xbat_blocks = 0; 75 int bat_blocks = 0; 76 77 while (true) 78 { 79 int calculated_bat_blocks = 80 BATBlock.calculateStorageRequirements(bat_blocks 81 + xbat_blocks 82 + _entries.size()); 83 int calculated_xbat_blocks = 84 HeaderBlockWriter 85 .calculateXBATStorageRequirements(calculated_bat_blocks); 86 87 if ((bat_blocks == calculated_bat_blocks) 88 && (xbat_blocks == calculated_xbat_blocks)) 89 { 90 91 break; 93 } 94 else 95 { 96 bat_blocks = calculated_bat_blocks; 97 xbat_blocks = calculated_xbat_blocks; 98 } 99 } 100 int startBlock = allocateSpace(bat_blocks); 101 102 allocateSpace(xbat_blocks); 103 simpleCreateBlocks(); 104 return startBlock; 105 } 106 107 114 115 public int allocateSpace(final int blockCount) 116 { 117 int startBlock = _entries.size(); 118 119 if (blockCount > 0) 120 { 121 int limit = blockCount - 1; 122 int index = startBlock + 1; 123 124 for (int k = 0; k < limit; k++) 125 { 126 _entries.add(index++); 127 } 128 _entries.add(POIFSConstants.END_OF_CHAIN); 129 } 130 return startBlock; 131 } 132 133 138 139 public int getStartBlock() 140 { 141 return _start_block; 142 } 143 144 147 148 void simpleCreateBlocks() 149 { 150 _blocks = BATBlock.createBATBlocks(_entries.toArray()); 151 } 152 153 154 155 164 165 public void writeBlocks(final OutputStream stream) 166 throws IOException 167 { 168 for (int j = 0; j < _blocks.length; j++) 169 { 170 _blocks[ j ].writeBlocks(stream); 171 } 172 } 173 174 175 176 177 182 183 public int countBlocks() 184 { 185 return _blocks.length; 186 } 187 188 193 194 public void setStartBlock(int start_block) 195 { 196 _start_block = start_block; 197 } 198 199 200 } 202 | Popular Tags |