1 2 17 18 19 package org.apache.poi.poifs.storage; 20 21 import java.io.*; 22 23 import java.util.*; 24 25 import org.apache.poi.poifs.common.POIFSConstants; 26 import org.apache.poi.util.IntegerField; 27 import org.apache.poi.util.LittleEndianConsts; 28 import org.apache.poi.util.LongField; 29 import org.apache.poi.util.ShortField; 30 31 36 37 public class HeaderBlockWriter 38 extends BigBlock 39 implements HeaderBlockConstants 40 { 41 private static final byte _default_value = ( byte ) 0xFF; 42 43 private IntegerField _bat_count; 45 46 private IntegerField _property_start; 49 50 private IntegerField _sbat_start; 53 54 private IntegerField _sbat_block_count; 56 57 private IntegerField _xbat_start; 59 private IntegerField _xbat_count; 60 private byte[] _data; 61 62 65 66 public HeaderBlockWriter() 67 { 68 _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ]; 69 Arrays.fill(_data, _default_value); 70 new LongField(_signature_offset, _signature, _data); 71 new IntegerField(0x08, 0, _data); 72 new IntegerField(0x0c, 0, _data); 73 new IntegerField(0x10, 0, _data); 74 new IntegerField(0x14, 0, _data); 75 new ShortField(0x18, ( short ) 0x3b, _data); 76 new ShortField(0x1a, ( short ) 0x3, _data); 77 new ShortField(0x1c, ( short ) -2, _data); 78 new ShortField(0x1e, ( short ) 0x9, _data); 79 new IntegerField(0x20, 0x6, _data); 80 new IntegerField(0x24, 0, _data); 81 new IntegerField(0x28, 0, _data); 82 _bat_count = new IntegerField(_bat_count_offset, 0, _data); 83 _property_start = new IntegerField(_property_start_offset, 84 POIFSConstants.END_OF_CHAIN, 85 _data); 86 new IntegerField(0x34, 0, _data); 87 new IntegerField(0x38, 0x1000, _data); 88 _sbat_start = new IntegerField(_sbat_start_offset, 89 POIFSConstants.END_OF_CHAIN, _data); 90 _sbat_block_count = new IntegerField(_sbat_block_count_offset, 0, 91 _data); 92 _xbat_start = new IntegerField(_xbat_start_offset, 93 POIFSConstants.END_OF_CHAIN, _data); 94 _xbat_count = new IntegerField(_xbat_count_offset, 0, _data); 95 } 96 97 108 109 public BATBlock [] setBATBlocks(final int blockCount, 110 final int startBlock) 111 { 112 BATBlock[] rvalue; 113 114 _bat_count.set(blockCount, _data); 115 int limit = Math.min(blockCount, _max_bats_in_header); 116 int offset = _bat_array_offset; 117 118 for (int j = 0; j < limit; j++) 119 { 120 new IntegerField(offset, startBlock + j, _data); 121 offset += LittleEndianConsts.INT_SIZE; 122 } 123 if (blockCount > _max_bats_in_header) 124 { 125 int excess_blocks = blockCount - _max_bats_in_header; 126 int[] excess_block_array = new int[ excess_blocks ]; 127 128 for (int j = 0; j < excess_blocks; j++) 129 { 130 excess_block_array[ j ] = startBlock + j 131 + _max_bats_in_header; 132 } 133 rvalue = BATBlock.createXBATBlocks(excess_block_array, 134 startBlock + blockCount); 135 _xbat_start.set(startBlock + blockCount, _data); 136 } 137 else 138 { 139 rvalue = BATBlock.createXBATBlocks(new int[ 0 ], 0); 140 _xbat_start.set(POIFSConstants.END_OF_CHAIN, _data); 141 } 142 _xbat_count.set(rvalue.length, _data); 143 return rvalue; 144 } 145 146 152 153 public void setPropertyStart(final int startBlock) 154 { 155 _property_start.set(startBlock, _data); 156 } 157 158 164 165 public void setSBATStart(final int startBlock) 166 { 167 _sbat_start.set(startBlock, _data); 168 } 169 170 175 176 public void setSBATBlockCount(final int count) 177 { 178 _sbat_block_count.set(count, _data); 179 } 180 181 189 190 static int calculateXBATStorageRequirements(final int blockCount) 191 { 192 return (blockCount > _max_bats_in_header) 193 ? BATBlock.calculateXBATStorageRequirements(blockCount 194 - _max_bats_in_header) 195 : 0; 196 } 197 198 199 200 209 210 void writeData(final OutputStream stream) 211 throws IOException 212 { 213 doWriteData(stream, _data); 214 } 215 216 217 } 219 | Popular Tags |