| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.serialize.charcode; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public final class UTF8CharacterSet implements CharacterSet { |
| 15 |
|
| 16 |
private static UTF8CharacterSet theInstance = new UTF8CharacterSet(); |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private UTF8CharacterSet() { |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public static UTF8CharacterSet getInstance() { |
| 32 |
return theInstance; |
| 33 |
} |
| 34 |
|
| 35 |
public boolean inCharset(int c) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
public String getCanonicalName() { |
| 41 |
return "UTF-8"; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static int getUTF8Encoding(char in, char in2, byte[] out) { |
| 55 |
|
| 56 |
int i = (int) in; |
| 57 |
if (i <= 0x7f) { |
| 58 |
out[0] = (byte) i; |
| 59 |
return 1; |
| 60 |
} else if (i <= 0x7ff) { |
| 61 |
out[0] = (byte) (0xc0 | ((in >> 6) & 0x1f)); |
| 62 |
out[1] = (byte) (0x80 | (in & 0x3f)); |
| 63 |
return 2; |
| 64 |
} else if (i >= 0xd800 && i <= 0xdbff) { |
| 65 |
|
| 66 |
int j = (int) in2; |
| 67 |
if (!(j >= 0xdc00 && j <= 0xdfff)) { |
| 68 |
throw new IllegalArgumentException("Malformed Unicode Surrogate Pair (" + i + ',' + j + ')'); |
| 69 |
} |
| 70 |
byte xxxxxx = (byte) (j & 0x3f); |
| 71 |
byte yyyyyy = (byte) (((i & 0x03) << 4) | ((j >> 6) & 0x0f)); |
| 72 |
byte zzzz = (byte) ((i >> 2) & 0x0f); |
| 73 |
byte uuuuu = (byte) (((i >> 6) & 0x0f) + 1); |
| 74 |
out[0] = (byte) (0xf0 | ((uuuuu >> 2) & 0x07)); |
| 75 |
out[1] = (byte) (0x80 | ((uuuuu & 0x03) << 4) | zzzz); |
| 76 |
out[2] = (byte) (0x80 | yyyyyy); |
| 77 |
out[3] = (byte) (0x80 | xxxxxx); |
| 78 |
return 4; |
| 79 |
} else if (i >= 0xdc00 && i <= 0xdfff) { |
| 80 |
|
| 81 |
return 0; |
| 82 |
} else { |
| 83 |
out[0] = (byte) (0xe0 | ((in >> 12) & 0x0f)); |
| 84 |
out[1] = (byte) (0x80 | ((in >> 6) & 0x3f)); |
| 85 |
out[2] = (byte) (0x80 | (in & 0x3f)); |
| 86 |
return 3; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static int decodeUTF8(byte[] in, int used) throws IllegalArgumentException { |
| 100 |
int bottom = 0; |
| 101 |
for (int i = 1; i < used; i++) { |
| 102 |
if ((in[i] & 0xc0) != 0x80) { |
| 103 |
throw new IllegalArgumentException("Byte " + (i + 1) + " in UTF-8 sequence has wrong top bits"); |
| 104 |
} |
| 105 |
bottom = (bottom << 6) + (in[i] & 0x3f); |
| 106 |
} |
| 107 |
if ((in[0] & 0x80) == 0) { |
| 108 |
|
| 109 |
if (used == 1) { |
| 110 |
return in[0]; |
| 111 |
} else { |
| 112 |
throw new IllegalArgumentException("UTF8 single byte expected"); |
| 113 |
} |
| 114 |
} else if ((in[0] & 0xe0) == 0xc0) { |
| 115 |
|
| 116 |
if (used != 2) { |
| 117 |
throw new IllegalArgumentException("UTF8 sequence of two bytes expected"); |
| 118 |
} |
| 119 |
return ((in[0] & 0x1f) << 6) + bottom; |
| 120 |
} else if ((in[0] & 0xf0) == 0xe0) { |
| 121 |
|
| 122 |
if (used != 3) { |
| 123 |
throw new IllegalArgumentException("UTF8 sequence of three bytes expected"); |
| 124 |
} |
| 125 |
return ((in[0] & 0x0f) << 12) + bottom; |
| 126 |
} else if ((in[0] & 0xf8) == 0xf8) { |
| 127 |
|
| 128 |
if (used != 4) { |
| 129 |
throw new IllegalArgumentException("UTF8 sequence of four bytes expected"); |
| 130 |
} |
| 131 |
return ((in[0] & 0x07) << 24) + bottom; |
| 132 |
} else { |
| 133 |
throw new IllegalArgumentException("UTF8 invalid first byte"); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
} |
| 139 |
|