package com zhumarin public class UTF8 public static String bytesToStr

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.zhumarin;
public class UTF8 {
public static String bytesToString(int[] bytes, int offset, int size) {
String s = "";
for (int i = 0; i < size; ) {
byte b = (byte) bytes[offset + i];
if ((b & 0x80) != 0) {
int utf8_len;
for (utf8_len = 8; utf8_len--> 0 && ((b >> utf8_len) & 1) == 1; );
utf8_len = 7 - utf8_len;
int utf8_value = 0, utf8_offset = 0;
for (int j = utf8_len; j--> 1; ) {
utf8_value |= (bytes[offset + i + j] & 0x3F) << utf8_offset;
utf8_offset += 6;
}
utf8_value |= (b & (0xFF >> (utf8_len + 1))) << utf8_offset;
if (utf8_value < 0x80) {
s += escapeChar((char) utf8_value);
} else if (utf8_value <= 0x07FF) {
if (isPrintableChar((char) utf8_value))
s += (char) utf8_value;
else
s += String.format("\\u%04X", utf8_value);
} else {
if (utf8_value >= 0xFFFF) {
for (int k = 4; k--> 0; ) {
int chr = (utf8_value >> (k * 8) & 0xFF);
if (chr == 0)
continue;
s += "\\" + chr;
}
} else {
s += String.format("\\u%04X", utf8_value);
}
}
i += utf8_len;
} else {
s += escapeChar((char) bytes[offset + i]);
++i;
}
}
return s;
}
public static String escapeChar(char c) {
if (c == '\b') {
return "\\b";
} else if (c == '\t') {
return "\\t";
} else if (c == '\n') {
return "\\n";
} else if (c == '\f') {
return "\\f";
} else if (c == '\r') {
return "\\r";
} else if (c == '\\') {
return "\\\\";
} else if (c == '"') {
return "\\\"";
} else if (c <= 0x1F) {
return "\\" + (int) c;
} else {
return "" + c;
}
}
public static boolean isPrintableChar(char c) {
Character.UnicodeBlock block = Character.UnicodeBlock.of( c );
return (!Character.isISOControl(c)) &&
// c != KeyEvent.CHAR_UNDEFINED &&
block != null &&
block != Character.UnicodeBlock.SPECIALS;
}
}