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 SSTRecord extends WritableRecordData 34 { 35 38 private int numReferences; 39 42 private int numStrings; 43 46 private ArrayList strings; 47 50 private ArrayList stringLengths; 51 54 private byte[] data; 55 58 private int byteCount; 59 62 private static int maxBytes = 8228 - 8 - 4; 66 72 public SSTRecord(int numRefs, int s) 73 { 74 super(Type.SST); 75 76 numReferences = numRefs; 77 numStrings = s; 78 byteCount = 0; 79 strings = new ArrayList (50); 80 stringLengths = new ArrayList (50); 81 } 82 83 91 public int add(String s) 92 { 93 int bytes = s.length() * 2 + 3; 94 95 if (byteCount >= maxBytes - 5) 98 { 99 return s.length(); 100 } 101 102 stringLengths.add(new Integer (s.length())); 103 104 if (bytes + byteCount < maxBytes) 105 { 106 strings.add(s); 108 byteCount += bytes; 109 return 0; 110 } 111 112 int bytesLeft = maxBytes - 3 - byteCount; 114 int charsAvailable = bytesLeft % 2 == 0 ? bytesLeft / 2 : 115 (bytesLeft - 1) / 2; 116 117 strings.add(s.substring(0, charsAvailable)); 119 byteCount += charsAvailable * 2 + 3; 120 121 return s.length() - charsAvailable; 122 } 123 124 129 public int getOffset() 130 { 131 return byteCount + 8; 132 } 133 134 139 public byte[] getData() 140 { 141 data = new byte[byteCount+8]; 142 IntegerHelper.getFourBytes(numReferences, data, 0); 143 IntegerHelper.getFourBytes(numStrings, data, 4); 144 145 int pos = 8; 146 int count = 0; 147 148 Iterator i = strings.iterator(); 149 String s = null; 150 int length = 0; 151 while (i.hasNext()) 152 { 153 s = (String ) i.next(); 154 length = ( (Integer ) stringLengths.get(count)).intValue(); 155 IntegerHelper.getTwoBytes(length, data, pos); 156 data[pos+2] = 0x01; 157 StringHelper.getUnicodeBytes(s, data, pos+3); 158 pos += s.length() * 2 + 3; 159 count++; 160 } 161 162 return data; 163 } 164 } 165 | Popular Tags |