1 2 17 18 19 package org.apache.poi.poifs.storage; 20 21 import org.apache.poi.util.LittleEndian; 22 import org.apache.poi.util.LittleEndianConsts; 23 24 import java.io.*; 25 26 import java.util.*; 27 28 33 34 public class LocalRawDataBlockList 35 extends RawDataBlockList 36 { 37 private List _list; 38 private RawDataBlock[] _array; 39 40 45 46 public LocalRawDataBlockList() 47 throws IOException 48 { 49 super(new ByteArrayInputStream(new byte[ 0 ])); 50 _list = new ArrayList(); 51 _array = null; 52 } 53 54 63 64 public void createNewXBATBlock(final int start, final int end, 65 final int chain) 66 throws IOException 67 { 68 byte[] data = new byte[ 512 ]; 69 int offset = 0; 70 71 for (int k = start; k <= end; k++) 72 { 73 LittleEndian.putInt(data, offset, k); 74 offset += LittleEndianConsts.INT_SIZE; 75 } 76 while (offset != 508) 77 { 78 LittleEndian.putInt(data, offset, -1); 79 offset += LittleEndianConsts.INT_SIZE; 80 } 81 LittleEndian.putInt(data, offset, chain); 82 add(new RawDataBlock(new ByteArrayInputStream(data))); 83 } 84 85 92 93 public void createNewBATBlock(final int start_index) 94 throws IOException 95 { 96 byte[] data = new byte[ 512 ]; 97 int offset = 0; 98 99 for (int j = 0; j < 128; j++) 100 { 101 int index = start_index + j; 102 103 if (index % 256 == 0) 104 { 105 LittleEndian.putInt(data, offset, -1); 106 } 107 else if (index % 256 == 255) 108 { 109 LittleEndian.putInt(data, offset, -2); 110 } 111 else 112 { 113 LittleEndian.putInt(data, offset, index + 1); 114 } 115 offset += LittleEndianConsts.INT_SIZE; 116 } 117 add(new RawDataBlock(new ByteArrayInputStream(data))); 118 } 119 120 127 128 public void fill(final int count) 129 throws IOException 130 { 131 int limit = 128 * count; 132 133 for (int j = _list.size(); j < limit; j++) 134 { 135 add(new RawDataBlock(new ByteArrayInputStream(new byte[ 0 ]))); 136 } 137 } 138 139 144 145 public void add(RawDataBlock block) 146 { 147 _list.add(block); 148 } 149 150 159 160 public ListManagedBlock remove(final int index) 161 throws IOException 162 { 163 ensureArrayExists(); 164 RawDataBlock rvalue = null; 165 166 try 167 { 168 rvalue = _array[ index ]; 169 if (rvalue == null) 170 { 171 throw new IOException("index " + index + " is null"); 172 } 173 _array[ index ] = null; 174 } 175 catch (ArrayIndexOutOfBoundsException ignored) 176 { 177 throw new IOException("Cannot remove block[ " + index 178 + " ]; out of range"); 179 } 180 return rvalue; 181 } 182 183 189 190 public void zap(final int index) 191 { 192 ensureArrayExists(); 193 if ((index >= 0) && (index < _array.length)) 194 { 195 _array[ index ] = null; 196 } 197 } 198 199 private void ensureArrayExists() 200 { 201 if (_array == null) 202 { 203 _array = ( RawDataBlock [] ) _list.toArray(new RawDataBlock[ 0 ]); 204 } 205 } 206 } 207 | Popular Tags |