1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import org.apache.poi.util.LittleEndian; 22 23 import java.util.ArrayList ; 24 25 37 38 public class ExtSSTRecord 39 extends Record 40 { 41 public static final int DEFAULT_BUCKET_SIZE = 8; 42 public static final int MAX_BUCKETS = 128; 45 public final static short sid = 0xff; 46 private short field_1_strings_per_bucket = DEFAULT_BUCKET_SIZE; 47 private ArrayList field_2_sst_info; 48 49 50 public ExtSSTRecord() 51 { 52 field_2_sst_info = new ArrayList (); 53 } 54 55 62 63 public ExtSSTRecord(short id, short size, byte [] data) 64 { 65 super(id, size, data); 66 } 67 68 76 77 public ExtSSTRecord(short id, short size, byte [] data, int offset) 78 { 79 super(id, size, data, offset); 80 } 81 82 protected void validateSid(short id) 83 { 84 if (id != sid) 85 { 86 throw new RecordFormatException("NOT An EXTSST RECORD"); 87 } 88 } 89 90 protected void fillFields(byte [] data, short size, int offset) 91 { 92 field_2_sst_info = new ArrayList (); 93 field_1_strings_per_bucket = LittleEndian.getShort(data, 0 + offset); 94 for (int k = 2; k < (size-offset); k += 8) 95 { 96 byte[] tempdata = new byte[ 8 + offset ]; 97 98 System.arraycopy(data, k, tempdata, 0, 8); 99 ExtSSTInfoSubRecord rec = new ExtSSTInfoSubRecord(( short ) 0, 100 ( short ) 8, tempdata); 101 102 field_2_sst_info.add(rec); 103 } 104 } 105 106 public void setNumStringsPerBucket(short numStrings) 107 { 108 field_1_strings_per_bucket = numStrings; 109 } 110 111 public void addInfoRecord(ExtSSTInfoSubRecord rec) 112 { 113 field_2_sst_info.add(rec); 114 } 115 116 public short getNumStringsPerBucket() 117 { 118 return field_1_strings_per_bucket; 119 } 120 121 public int getNumInfoRecords() 122 { 123 return field_2_sst_info.size(); 124 } 125 126 public ExtSSTInfoSubRecord getInfoRecordAt(int elem) 127 { 128 return ( ExtSSTInfoSubRecord ) field_2_sst_info.get(elem); 129 } 130 131 public String toString() 132 { 133 StringBuffer buffer = new StringBuffer (); 134 135 buffer.append("[EXTSST]\n"); 136 buffer.append(" .dsst = ") 137 .append(Integer.toHexString(getNumStringsPerBucket())) 138 .append("\n"); 139 buffer.append(" .numInfoRecords = ").append(getNumInfoRecords()) 140 .append("\n"); 141 for (int k = 0; k < getNumInfoRecords(); k++) 142 { 143 buffer.append(" .inforecord = ").append(k).append("\n"); 144 buffer.append(" .streampos = ") 145 .append(Integer 146 .toHexString(getInfoRecordAt(k).getStreamPos())).append("\n"); 147 buffer.append(" .sstoffset = ") 148 .append(Integer 149 .toHexString(getInfoRecordAt(k).getBucketSSTOffset())) 150 .append("\n"); 151 } 152 buffer.append("[/EXTSST]\n"); 153 return buffer.toString(); 154 } 155 156 public int serialize(int offset, byte [] data) 157 { 158 LittleEndian.putShort(data, 0 + offset, sid); 159 LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4)); 160 LittleEndian.putShort(data, 4 + offset, field_1_strings_per_bucket); 161 int pos = 6; 162 163 for (int k = 0; k < getNumInfoRecords(); k++) 164 { 165 ExtSSTInfoSubRecord rec = getInfoRecordAt(k); 166 pos += rec.serialize(pos + offset, data); 167 } 168 return pos; 169 } 170 171 172 public int getRecordSize() 173 { 174 return 6 + 8*getNumInfoRecords(); 175 } 176 177 public static final int getNumberOfInfoRecsForStrings(int numStrings) { 178 int infoRecs = (numStrings / DEFAULT_BUCKET_SIZE); 179 if ((numStrings % DEFAULT_BUCKET_SIZE) != 0) 180 infoRecs ++; 181 if (infoRecs > MAX_BUCKETS) 184 infoRecs = MAX_BUCKETS; 185 return infoRecs; 186 } 187 188 189 public static final int getRecordSizeForStrings(int numStrings) { 190 return 4 + 2 + (getNumberOfInfoRecsForStrings(numStrings) * 8); 191 } 192 193 public short getSid() 194 { 195 return sid; 196 } 197 198 public void setBucketOffsets( int[] bucketAbsoluteOffsets, int[] bucketRelativeOffsets ) 199 { 200 this.field_2_sst_info = new ArrayList (bucketAbsoluteOffsets.length); 201 for ( int i = 0; i < bucketAbsoluteOffsets.length; i++ ) 202 { 203 ExtSSTInfoSubRecord r = new ExtSSTInfoSubRecord(); 204 r.setBucketRecordOffset((short)bucketRelativeOffsets[i]); 205 r.setStreamPos(bucketAbsoluteOffsets[i]); 206 field_2_sst_info.add(r); 207 } 208 } 209 210 } 211 | Popular Tags |