| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package com.android.internal.util; |
| 19 |
|
| 20 |
import java.io.UTFDataFormatException; |
| 21 |
|
| 22 |
public class ModifiedUtf8 { |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public static String decode(byte[] in, char[] out, int offset, int utfSize) |
| 30 |
throws UTFDataFormatException { |
| 31 |
int count = 0, s = 0, a; |
| 32 |
while (count < utfSize) { |
| 33 |
if ((out[s] = (char) in[offset + count++]) < '\u0080') { |
| 34 |
s++; |
| 35 |
} else if (((a = out[s]) & 0xe0) == 0xc0) { |
| 36 |
if (count >= utfSize) { |
| 37 |
throw new UTFDataFormatException("bad second byte at " + count); |
| 38 |
} |
| 39 |
int b = in[offset + count++]; |
| 40 |
if ((b & 0xC0) != 0x80) { |
| 41 |
throw new UTFDataFormatException("bad second byte at " + (count - 1)); |
| 42 |
} |
| 43 |
out[s++] = (char) (((a & 0x1F) << 6) | (b & 0x3F)); |
| 44 |
} else if ((a & 0xf0) == 0xe0) { |
| 45 |
if (count + 1 >= utfSize) { |
| 46 |
throw new UTFDataFormatException("bad third byte at " + (count + 1)); |
| 47 |
} |
| 48 |
int b = in[offset + count++]; |
| 49 |
int c = in[offset + count++]; |
| 50 |
if (((b & 0xC0) != 0x80) || ((c & 0xC0) != 0x80)) { |
| 51 |
throw new UTFDataFormatException("bad second or third byte at " + (count - 2)); |
| 52 |
} |
| 53 |
out[s++] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F)); |
| 54 |
} else { |
| 55 |
throw new UTFDataFormatException("bad byte at " + (count - 1)); |
| 56 |
} |
| 57 |
} |
| 58 |
return new String(out, 0, s); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static long countBytes(String s, boolean shortLength) throws UTFDataFormatException { |
| 69 |
long result = 0; |
| 70 |
final int length = s.length(); |
| 71 |
for (int i = 0; i < length; ++i) { |
| 72 |
char ch = s.charAt(i); |
| 73 |
if (ch != 0 && ch <= 127) { |
| 74 |
++result; |
| 75 |
} else if (ch <= 2047) { |
| 76 |
result += 2; |
| 77 |
} else { |
| 78 |
result += 3; |
| 79 |
} |
| 80 |
if (shortLength && result > 65535) { |
| 81 |
throw new UTFDataFormatException("String more than 65535 UTF bytes long"); |
| 82 |
} |
| 83 |
} |
| 84 |
return result; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static void encode(byte[] dst, int offset, String s) { |
| 92 |
final int length = s.length(); |
| 93 |
for (int i = 0; i < length; i++) { |
| 94 |
char ch = s.charAt(i); |
| 95 |
if (ch != 0 && ch <= 127) { |
| 96 |
dst[offset++] = (byte) ch; |
| 97 |
} else if (ch <= 2047) { |
| 98 |
dst[offset++] = (byte) (0xc0 | (0x1f & (ch >> 6))); |
| 99 |
dst[offset++] = (byte) (0x80 | (0x3f & ch)); |
| 100 |
} else { |
| 101 |
dst[offset++] = (byte) (0xe0 | (0x0f & (ch >> 12))); |
| 102 |
dst[offset++] = (byte) (0x80 | (0x3f & (ch >> 6))); |
| 103 |
dst[offset++] = (byte) (0x80 | (0x3f & ch)); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
private ModifiedUtf8() { |
| 109 |
} |
| 110 |
} |