1 19 20 package jxl.write.biff; 21 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 25 import jxl.biff.Type; 26 import jxl.biff.IntegerHelper; 27 import jxl.biff.WritableRecordData; 28 import jxl.biff.StringHelper; 29 30 33 class SSTContinueRecord extends WritableRecordData 34 { 35 38 private String firstString; 39 43 private boolean includeLength; 44 47 private int firstStringLength; 48 51 private ArrayList strings; 52 55 private ArrayList stringLengths; 56 59 private byte[] data; 60 63 private int byteCount; 64 67 private static int maxBytes = 8228 - 4; 70 76 public SSTContinueRecord() 77 { 78 super(Type.CONTINUE); 79 80 byteCount = 0; 81 strings = new ArrayList (50); 82 stringLengths = new ArrayList (50); 83 } 84 85 92 public int setFirstString(String s, boolean b) 93 { 94 includeLength = b; 95 firstStringLength = s.length(); 96 97 int bytes = 0; 98 99 if (!includeLength) 100 { 101 bytes = s.length() * 2 + 1; 102 } 103 else 104 { 105 bytes = s.length() * 2 + 3; 106 } 107 108 if (bytes <= maxBytes) 109 { 110 firstString = s; 111 byteCount += bytes; 112 return 0; 113 } 114 115 int charsAvailable = includeLength ? (maxBytes - 4) / 2 : 118 (maxBytes - 2) / 2; 119 120 firstString = s.substring(0, charsAvailable); 122 byteCount = maxBytes - 1; 123 124 return s.length() - charsAvailable; 125 } 126 127 132 public int getOffset() 133 { 134 return byteCount; 135 } 136 137 145 public int add(String s) 146 { 147 int bytes = s.length() * 2 + 3; 148 149 if (byteCount >= maxBytes - 5) 152 { 153 return s.length(); 154 } 155 156 stringLengths.add(new Integer (s.length())); 157 158 if (bytes + byteCount < maxBytes) 159 { 160 strings.add(s); 162 byteCount += bytes; 163 return 0; 164 } 165 166 int bytesLeft = maxBytes - 3 - byteCount; 168 int charsAvailable = bytesLeft % 2 == 0 ? bytesLeft / 2 : 169 (bytesLeft - 1) / 2; 170 171 strings.add(s.substring(0, charsAvailable)); 173 byteCount += charsAvailable * 2 + 3; 174 175 return s.length() - charsAvailable; 176 } 177 178 183 public byte[] getData() 184 { 185 data = new byte[byteCount]; 186 187 int pos = 0; 188 189 if (includeLength) 191 { 192 IntegerHelper.getTwoBytes(firstStringLength, data, 0); 193 data[2] = 0x01; 194 pos = 3; 195 } 196 else 197 { 198 data[0] = 0x01; 200 pos = 1; 201 } 202 203 StringHelper.getUnicodeBytes(firstString, data, pos); 204 pos += firstString.length() * 2; 205 206 Iterator i = strings.iterator(); 208 String s = null; 209 int length = 0; 210 int count = 0; 211 while (i.hasNext()) 212 { 213 s = (String ) i.next(); 214 length = ( (Integer ) stringLengths.get(count)).intValue(); 215 IntegerHelper.getTwoBytes(length, data, pos); 216 data[pos+2] = 0x01; 217 StringHelper.getUnicodeBytes(s, data, pos+3); 218 pos += s.length() * 2 + 3; 219 count++; 220 } 221 222 return data; 223 } 224 } 225 | Popular Tags |