1 7 8 23 package java.text; 24 25 import java.io.*; 26 import java.security.AccessController ; 27 import java.security.PrivilegedActionException ; 28 import java.security.PrivilegedExceptionAction ; 29 import java.util.MissingResourceException ; 30 import sun.text.CompactByteArray; 31 import sun.text.SupplementaryCharacterData; 32 33 44 class BreakDictionary { 45 46 50 53 private static int supportedVersion = 1; 54 55 59 private CompactByteArray columnMap = null; 60 private SupplementaryCharacterData supplementaryCharColumnMap = null; 61 62 65 private int numCols; 66 67 72 private int numColGroups; 73 74 85 private short[] table = null; 86 87 90 private short[] rowIndex = null; 91 92 98 private int[] rowIndexFlags = null; 99 100 108 private short[] rowIndexFlagsIndex = null; 109 110 114 private byte[] rowIndexShifts = null; 115 116 120 public BreakDictionary(String dictionaryName) 121 throws IOException, MissingResourceException { 122 123 readDictionaryFile(dictionaryName); 124 } 125 126 private void readDictionaryFile(final String dictionaryName) 127 throws IOException, MissingResourceException { 128 129 BufferedInputStream in; 130 try { 131 in = (BufferedInputStream)AccessController.doPrivileged( 132 new PrivilegedExceptionAction () { 133 public Object run() throws Exception { 134 return new BufferedInputStream(getClass().getResourceAsStream("/sun/text/resources/" + dictionaryName)); 135 } 136 } 137 ); 138 } 139 catch (PrivilegedActionException e) { 140 throw new InternalError (e.toString()); 141 } 142 143 byte[] buf = new byte[8]; 144 if (in.read(buf) != 8) { 145 throw new MissingResourceException ("Wrong data length", 146 dictionaryName, ""); 147 } 148 149 int version = BreakIterator.getInt(buf, 0); 151 if (version != supportedVersion) { 152 throw new MissingResourceException ("Dictionary version(" + version + ") is unsupported", 153 dictionaryName, ""); 154 } 155 156 int len = BreakIterator.getInt(buf, 4); 158 buf = new byte[len]; 159 if (in.read(buf) != len) { 160 throw new MissingResourceException ("Wrong data length", 161 dictionaryName, ""); 162 } 163 164 in.close(); 166 167 int l; 168 int offset = 0; 169 170 l = BreakIterator.getInt(buf, offset); 173 offset += 4; 174 short[] temp = new short[l]; 175 for (int i = 0; i < l; i++, offset+=2) { 176 temp[i] = BreakIterator.getShort(buf, offset); 177 } 178 l = BreakIterator.getInt(buf, offset); 179 offset += 4; 180 byte[] temp2 = new byte[l]; 181 for (int i = 0; i < l; i++, offset++) { 182 temp2[i] = buf[offset]; 183 } 184 columnMap = new CompactByteArray(temp, temp2); 185 186 numCols = BreakIterator.getInt(buf, offset); 188 offset += 4; 189 numColGroups = BreakIterator.getInt(buf, offset); 190 offset += 4; 191 192 l = BreakIterator.getInt(buf, offset); 194 offset += 4; 195 rowIndex = new short[l]; 196 for (int i = 0; i < l; i++, offset+=2) { 197 rowIndex[i] = BreakIterator.getShort(buf, offset); 198 } 199 200 l = BreakIterator.getInt(buf, offset); 202 offset += 4; 203 rowIndexFlagsIndex = new short[l]; 204 for (int i = 0; i < l; i++, offset+=2) { 205 rowIndexFlagsIndex[i] = BreakIterator.getShort(buf, offset); 206 } 207 l = BreakIterator.getInt(buf, offset); 208 offset += 4; 209 rowIndexFlags = new int[l]; 210 for (int i = 0; i < l; i++, offset+=4) { 211 rowIndexFlags[i] = BreakIterator.getInt(buf, offset); 212 } 213 214 l = BreakIterator.getInt(buf, offset); 216 offset += 4; 217 rowIndexShifts = new byte[l]; 218 for (int i = 0; i < l; i++, offset++) { 219 rowIndexShifts[i] = buf[offset]; 220 } 221 222 l = BreakIterator.getInt(buf, offset); 224 offset += 4; 225 table = new short[l]; 226 for (int i = 0; i < l; i++, offset+=2) { 227 table[i] = BreakIterator.getShort(buf, offset); 228 } 229 230 l = BreakIterator.getInt(buf, offset); 232 offset += 4; 233 int[] temp3 = new int[l]; 234 for (int i = 0; i < l; i++, offset+=4) { 235 temp3[i] = BreakIterator.getInt(buf, offset); 236 } 237 supplementaryCharColumnMap = new SupplementaryCharacterData(temp3); 238 } 239 240 244 251 public final short getNextStateFromCharacter(int row, int ch) { 252 int col; 253 if (ch < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 254 col = columnMap.elementAt((char)ch); 255 } else { 256 col = supplementaryCharColumnMap.getValue(ch); 257 } 258 return getNextState(row, col); 259 } 260 261 272 public final short getNextState(int row, int col) { 273 if (cellIsPopulated(row, col)) { 274 return internalAt(rowIndex[row], col + rowIndexShifts[row]); 281 } 282 else { 283 return 0; 284 } 285 } 286 287 291 private final boolean cellIsPopulated(int row, int col) { 292 if (rowIndexFlagsIndex[row] < 0) { 296 return col == -rowIndexFlagsIndex[row]; 297 } 298 299 else { 306 int flags = rowIndexFlags[rowIndexFlagsIndex[row] + (col >> 5)]; 307 return (flags & (1 << (col & 0x1f))) != 0; 308 } 309 } 310 311 318 private final short internalAt(int row, int col) { 319 return table[row * numCols + col]; 323 } 324 } 325 326 | Popular Tags |