1 7 package com.ibm.icu.impl; 8 9 import java.io.InputStream ; 10 import java.io.DataInputStream ; 11 import java.io.IOException ; 12 import java.util.Arrays ; 13 14 27 28 final class UCharacterNameReader implements ICUBinary.Authenticate 29 { 30 32 public boolean isDataVersionAcceptable(byte version[]) 33 { 34 return version[0] == DATA_FORMAT_VERSION_[0]; 35 } 36 37 39 45 protected UCharacterNameReader(InputStream inputStream) 46 throws IOException 47 { 48 ICUBinary.readHeader(inputStream, DATA_FORMAT_ID_, this); 49 m_dataInputStream_ = new DataInputStream (inputStream); 50 } 51 52 54 61 protected void read(UCharacterName data) throws IOException 62 { 63 m_tokenstringindex_ = m_dataInputStream_.readInt(); 65 m_groupindex_ = m_dataInputStream_.readInt(); 66 m_groupstringindex_ = m_dataInputStream_.readInt(); 67 m_algnamesindex_ = m_dataInputStream_.readInt(); 68 69 int count = m_dataInputStream_.readChar(); 71 char token[] = new char[count]; 72 for (char i = 0; i < count; i ++) { 73 token[i] = m_dataInputStream_.readChar(); 74 } 75 int size = m_groupindex_ - m_tokenstringindex_; 76 byte tokenstr[] = new byte[size]; 77 m_dataInputStream_.readFully(tokenstr); 78 data.setToken(token, tokenstr); 79 80 count = m_dataInputStream_.readChar(); 82 data.setGroupCountSize(count, GROUP_INFO_SIZE_); 83 count *= GROUP_INFO_SIZE_; 84 char group[] = new char[count]; 85 for (int i = 0; i < count; i ++) { 86 group[i] = m_dataInputStream_.readChar(); 87 } 88 89 size = m_algnamesindex_ - m_groupstringindex_; 90 byte groupstring[] = new byte[size]; 91 m_dataInputStream_.readFully(groupstring); 92 93 data.setGroup(group, groupstring); 94 95 count = m_dataInputStream_.readInt(); 96 UCharacterName.AlgorithmName alg[] = 97 new UCharacterName.AlgorithmName[count]; 98 99 for (int i = 0; i < count; i ++) 100 { 101 UCharacterName.AlgorithmName an = readAlg(); 102 if (an == null) { 103 throw new IOException ("unames.icu read error: Algorithmic names creation error"); 104 } 105 alg[i] = an; 106 } 107 data.setAlgorithm(alg); 108 } 109 110 117 protected boolean authenticate(byte dataformatid[], 119 byte dataformatversion[]) 120 { 121 return Arrays.equals(DATA_FORMAT_ID_, dataformatid) && 122 Arrays.equals(DATA_FORMAT_VERSION_, dataformatversion); 123 } 124 126 128 131 private DataInputStream m_dataInputStream_; 132 135 private static final int GROUP_INFO_SIZE_ = 3; 136 137 140 private int m_tokenstringindex_; 141 private int m_groupindex_; 142 private int m_groupstringindex_; 143 private int m_algnamesindex_; 144 145 150 private static final int ALG_INFO_SIZE_ = 12; 151 152 156 private static final byte DATA_FORMAT_VERSION_[] = 157 {(byte)0x1, (byte)0x0, (byte)0x0, (byte)0x0}; 158 private static final byte DATA_FORMAT_ID_[] = {(byte)0x75, (byte)0x6E, 159 (byte)0x61, (byte)0x6D}; 160 161 163 168 private UCharacterName.AlgorithmName readAlg() throws IOException 169 { 170 UCharacterName.AlgorithmName result = 171 new UCharacterName.AlgorithmName(); 172 int rangestart = m_dataInputStream_.readInt(); 173 int rangeend = m_dataInputStream_.readInt(); 174 byte type = m_dataInputStream_.readByte(); 175 byte variant = m_dataInputStream_.readByte(); 176 if (!result.setInfo(rangestart, rangeend, type, variant)) { 177 return null; 178 } 179 180 int size = m_dataInputStream_.readChar(); 181 if (type == UCharacterName.AlgorithmName.TYPE_1_) 182 { 183 char factor[] = new char[variant]; 184 for (int j = 0; j < variant; j ++) { 185 factor[j] = m_dataInputStream_.readChar(); 186 } 187 188 result.setFactor(factor); 189 size -= (variant << 1); 190 } 191 192 StringBuffer prefix = new StringBuffer (); 193 char c = (char)(m_dataInputStream_.readByte() & 0x00FF); 194 while (c != 0) 195 { 196 prefix.append(c); 197 c = (char)(m_dataInputStream_.readByte() & 0x00FF); 198 } 199 200 result.setPrefix(prefix.toString()); 201 202 size -= (ALG_INFO_SIZE_ + prefix.length() + 1); 203 204 if (size > 0) 205 { 206 byte string[] = new byte[size]; 207 m_dataInputStream_.readFully(string); 208 result.setFactorString(string); 209 } 210 return result; 211 } 212 } 213 214 | Popular Tags |