1 8 package com.ibm.icu.impl; 9 10 import java.io.BufferedInputStream ; 11 import java.io.DataInputStream ; 12 import java.io.IOException ; 13 import java.io.InputStream ; 14 15 import com.ibm.icu.util.ULocale; 16 import com.ibm.icu.util.VersionInfo; 17 18 19 20 119 public final class ICUResourceBundleReader implements ICUBinary.Authenticate{ 120 121 125 private static final byte DATA_FORMAT_ID[] = {(byte)0x52, (byte)0x65, 126 (byte)0x73, (byte)0x42}; 127 128 private static final String ICU_RESOURCE_SUFFIX = ".res"; 129 130 131 private static final int URES_INDEX_LENGTH = 0; 132 private static final int URES_INDEX_STRINGS_TOP = 1; 133 134 private static final int URES_INDEX_RESOURCES_TOP = 2; 135 private static final int URES_INDEX_BUNDLE_TOP = 3; 136 137 private static final int URES_INDEX_MAX_TABLE_LENGTH = 4; 138 private static final int URES_INDEX_ATTRIBUTES = 5; 139 private static final int URES_INDEX_TOP = 6; 140 141 143 154 private static final int URES_ATT_NO_FALLBACK = 1; 155 156 private static final boolean DEBUG = false; 157 158 private byte[] dataVersion; 159 160 private int rootRes; 161 private int[] indexes; 162 private boolean noFallback; 163 164 private byte[] data; 165 166 private ICUResourceBundleReader(InputStream stream, String resolvedName){ 167 168 BufferedInputStream bs = new BufferedInputStream (stream); 169 try{ 170 if(DEBUG) System.out.println("The InputStream class is: " + stream.getClass().getName()); 171 if(DEBUG) System.out.println("The BufferedInputStream class is: " + bs.getClass().getName()); 172 if(DEBUG) System.out.println("The bytes avialable in stream before reading the header: " + bs.available()); 173 174 dataVersion = ICUBinary.readHeader(bs,DATA_FORMAT_ID,this); 175 176 if(DEBUG) System.out.println("The bytes available in stream after reading the header: " + bs.available()); 177 178 readData(bs); 179 stream.close(); 180 }catch(IOException ex){ 181 throw new RuntimeException ("Data file "+ resolvedName+ " is corrupt."); 185 } 187 } 188 public static ICUResourceBundleReader getReader(String baseName, String localeName, ClassLoader root){ 189 String resolvedName = getFullName(baseName, localeName); 190 InputStream stream = ICUData.getStream(root,resolvedName); 191 192 if(stream==null){ 193 return null; 194 } 195 ICUResourceBundleReader reader = new ICUResourceBundleReader(stream, resolvedName); 196 return reader; 197 } 198 199 private static void writeInt(int i, byte[] bytes, int offset) { 200 bytes[offset++]=(byte)(i>>24); 201 bytes[offset++]=(byte)(i>>16); 202 bytes[offset++]=(byte)(i>>8); 203 bytes[offset]=(byte)i; 204 } 205 206 private void readData(InputStream stream) 207 throws IOException { 208 209 DataInputStream ds = new DataInputStream (stream); 210 211 if(DEBUG) System.out.println("The DataInputStream class is: " + ds.getClass().getName()); 212 if(DEBUG) System.out.println("The available bytes in the stream before reading the data: "+ds.available()); 213 214 222 rootRes = ds.readInt(); 223 224 int indexLength = ds.readInt(); 226 ds.mark((indexLength-1)*4); 227 228 indexes = new int[indexLength]; 229 indexes[URES_INDEX_LENGTH] = indexLength; 230 231 for(int i=1; i<indexLength; i++){ 232 indexes[i] = ds.readInt(); 233 } 234 235 noFallback = 238 indexLength > URES_INDEX_ATTRIBUTES && 239 (indexes[URES_INDEX_ATTRIBUTES]&URES_ATT_NO_FALLBACK)!=0; 240 241 int length = indexes[URES_INDEX_BUNDLE_TOP]*4; 245 if(DEBUG) System.out.println("The number of bytes in the bundle: "+length); 246 247 data = new byte[length]; 248 writeInt(rootRes, data, 0); 249 writeInt(indexLength, data, 4); 250 251 ds.reset(); 253 ds.readFully(data, 8, length-8); 254 } 255 256 259 public static String getFullName(String baseName, String localeName){ 260 if(baseName==null || baseName.length()==0){ 261 if(localeName.length()==0){ 262 return ULocale.getDefault().toString()+ICU_RESOURCE_SUFFIX; 263 }else{ 264 return localeName+ICU_RESOURCE_SUFFIX; 265 } 266 }else{ 267 if(baseName.indexOf('.')==-1){ 268 if(baseName.charAt(baseName.length()-1)!= '/'){ 269 return baseName+"/"+localeName+ICU_RESOURCE_SUFFIX; 270 }else{ 271 return baseName+localeName+ICU_RESOURCE_SUFFIX; 272 } 273 }else{ 274 baseName = baseName.replace('.','/'); 275 if(localeName.length()==0){ 276 return baseName+ICU_RESOURCE_SUFFIX; 277 }else{ 278 return baseName+"_"+localeName+ICU_RESOURCE_SUFFIX; 279 } 280 } 281 } 282 } 283 284 public VersionInfo getVersion(){ 285 return VersionInfo.getInstance(dataVersion[0],dataVersion[1],dataVersion[2],dataVersion[3]); 286 } 287 public boolean isDataVersionAcceptable(byte version[]){ 288 return version[0] == 1 && version[1] >= 1; 292 } 293 294 public byte[] getData(){ 295 return data; 296 } 297 public int getRootResource() { 298 return rootRes; 299 } 300 public boolean getNoFallback() { 301 return noFallback; 302 } 303 } 304 | Popular Tags |