1 8 9 package com.sleepycat.util; 10 11 17 public class UtfOps { 18 19 private static byte[] EMPTY_BYTES = {}; 20 private static String EMPTY_STRING = ""; 21 22 34 public static int getZeroTerminatedByteLength(byte[] bytes, int offset) 35 throws IndexOutOfBoundsException { 36 37 int len = 0; 38 while (bytes[offset++] != 0) { 39 len++; 40 } 41 return len; 42 } 43 44 52 public static int getByteLength(char[] chars) { 53 54 return getByteLength(chars, 0, chars.length); 55 } 56 57 69 public static int getByteLength(char[] chars, int offset, int length) { 70 71 int len = 0; 72 length += offset; 73 for (int i = offset; i < length; i++) { 74 int c = chars[i]; 75 if ((c >= 0x0001) && (c <= 0x007F)) { 76 len++; 77 } else if (c > 0x07FF) { 78 len += 3; 79 } else { 80 len += 2; 81 } 82 } 83 return len; 84 } 85 86 99 public static int getCharLength(byte[] bytes) 100 throws IllegalArgumentException , IndexOutOfBoundsException { 101 102 return getCharLength(bytes, 0, bytes.length); 103 } 104 105 120 public static int getCharLength(byte[] bytes, int offset, int length) 121 throws IllegalArgumentException , IndexOutOfBoundsException { 122 123 int charCount = 0; 124 length += offset; 125 while (offset < length) { 126 switch ((bytes[offset] & 0xff) >> 4) { 127 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 128 offset++; 129 break; 130 case 12: case 13: 131 offset += 2; 132 break; 133 case 14: 134 offset += 3; 135 break; 136 default: 137 throw new IllegalArgumentException (); 138 } 139 charCount++; 140 } 141 return charCount; 142 } 143 144 167 public static int bytesToChars(byte[] bytes, int byteOffset, 168 char[] chars, int charOffset, 169 int len, boolean isByteLen) 170 throws IllegalArgumentException , IndexOutOfBoundsException { 171 172 int char1, char2, char3; 173 len += isByteLen ? byteOffset : charOffset; 174 while ((isByteLen ? byteOffset : charOffset) < len) { 175 char1 = bytes[byteOffset++] & 0xff; 176 switch ((char1 & 0xff) >> 4) { 177 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 178 chars[charOffset++] = (char) char1; 179 break; 180 case 12: case 13: 181 char2 = bytes[byteOffset++]; 182 if ((char2 & 0xC0) != 0x80) { 183 throw new IllegalArgumentException (); 184 } 185 chars[charOffset++] = (char)(((char1 & 0x1F) << 6) | 186 (char2 & 0x3F)); 187 break; 188 case 14: 189 char2 = bytes[byteOffset++]; 190 char3 = bytes[byteOffset++]; 191 if (((char2 & 0xC0) != 0x80) || ((char3 & 0xC0) != 0x80)) 192 throw new IllegalArgumentException (); 193 chars[charOffset++] = (char)(((char1 & 0x0F) << 12) | 194 ((char2 & 0x3F) << 6) | 195 ((char3 & 0x3F) << 0)); 196 break; 197 default: 198 throw new IllegalArgumentException (); 199 } 200 } 201 return byteOffset; 202 } 203 204 218 public static void charsToBytes(char[] chars, int charOffset, 219 byte[] bytes, int byteOffset, 220 int charLength) { 221 charLength += charOffset; 222 for (int i = charOffset; i < charLength; i++) { 223 int c = chars[i]; 224 if ((c >= 0x0001) && (c <= 0x007F)) { 225 bytes[byteOffset++] = (byte) c; 226 } else if (c > 0x07FF) { 227 bytes[byteOffset++] = (byte) (0xE0 | ((c >> 12) & 0x0F)); 228 bytes[byteOffset++] = (byte) (0x80 | ((c >> 6) & 0x3F)); 229 bytes[byteOffset++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 230 } else { 231 bytes[byteOffset++] = (byte) (0xC0 | ((c >> 6) & 0x1F)); 232 bytes[byteOffset++] = (byte) (0x80 | ((c >> 0) & 0x3F)); 233 } 234 } 235 } 236 237 255 public static String bytesToString(byte[] bytes, int offset, int length) 256 throws IllegalArgumentException , IndexOutOfBoundsException { 257 258 if (length == 0) return EMPTY_STRING; 259 int charLen = UtfOps.getCharLength(bytes, offset, length); 260 char[] chars = new char[charLen]; 261 UtfOps.bytesToChars(bytes, offset, chars, 0, length, true); 262 return new String (chars, 0, charLen); 263 } 264 265 272 public static byte[] stringToBytes(String string) { 273 274 if (string.length() == 0) return EMPTY_BYTES; 275 char[] chars = string.toCharArray(); 276 byte[] bytes = new byte[UtfOps.getByteLength(chars)]; 277 UtfOps.charsToBytes(chars, 0, bytes, 0, chars.length); 278 return bytes; 279 } 280 } 281 | Popular Tags |