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 public final class ICUBinary 15 { 16 18 21 public static interface Authenticate 22 { 23 29 public boolean isDataVersionAcceptable(byte version[]); 30 } 31 32 34 82 public static final byte[] readHeader(InputStream inputStream, 83 byte dataFormatIDExpected[], 84 Authenticate authenticate) 85 throws IOException 86 { 87 DataInputStream input = new DataInputStream (inputStream); 88 char headersize = input.readChar(); 89 int readcount = 2; 90 byte magic1 = input.readByte(); 92 readcount ++; 93 byte magic2 = input.readByte(); 94 readcount ++; 95 if (magic1 != MAGIC1 || magic2 != MAGIC2) { 96 throw new IOException (MAGIC_NUMBER_AUTHENTICATION_FAILED_); 97 } 98 99 input.readChar(); readcount += 2; 101 input.readChar(); readcount += 2; 103 byte bigendian = input.readByte(); 104 readcount ++; 105 byte charset = input.readByte(); 106 readcount ++; 107 byte charsize = input.readByte(); 108 readcount ++; 109 input.readByte(); readcount ++; 111 112 byte dataFormatID[] = new byte[4]; 113 input.readFully(dataFormatID); 114 readcount += 4; 115 byte dataVersion[] = new byte[4]; 116 input.readFully(dataVersion); 117 readcount += 4; 118 byte unicodeVersion[] = new byte[4]; 119 input.readFully(unicodeVersion); 120 readcount += 4; 121 if (headersize < readcount) { 122 throw new IOException ("Internal Error: Header size error"); 123 } 124 input.skipBytes(headersize - readcount); 125 126 if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_ 127 || charsize != CHAR_SIZE_ 128 || !Arrays.equals(dataFormatIDExpected, dataFormatID) 129 || (authenticate != null 130 && !authenticate.isDataVersionAcceptable(dataVersion))) { 131 throw new IOException (HEADER_AUTHENTICATION_FAILED_); 132 } 133 return unicodeVersion; 134 } 135 136 138 141 private static final byte MAGIC1 = (byte)0xda; 142 private static final byte MAGIC2 = (byte)0x27; 143 144 147 private static final byte BIG_ENDIAN_ = 1; 148 private static final byte CHAR_SET_ = 0; 149 private static final byte CHAR_SIZE_ = 2; 150 151 154 private static final String MAGIC_NUMBER_AUTHENTICATION_FAILED_ = 155 "ICU data file error: Not an ICU data file"; 156 private static final String HEADER_AUTHENTICATION_FAILED_ = 157 "ICU data file error: Header authentication failed, please check if you have a valid ICU data file"; 158 } 159 | Popular Tags |