Skip to content

Commit 4b87e19

Browse files
committed
add feature: UTF-8 characters support!
1 parent 9f3302f commit 4b87e19

4 files changed

Lines changed: 111 additions & 12 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
.classpath
66
.project
77
.DS_Store
8-
*.iml
8+
*.iml
9+
/out/

src/me/konloch/kontainer/io/DiskReader.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package me.konloch.kontainer.io;
22

3+
import the.bytecode.club.bytecodeviewer.util.EncodeUtils;
4+
35
import java.io.BufferedReader;
46
import java.io.FileReader;
57
import java.io.File;
@@ -54,20 +56,18 @@ public synchronized static ArrayList<String> loadArrayList(String fileName,
5456
/**
5557
* Used to load from file
5658
*/
57-
public synchronized static String loadAsString(String fileName)
58-
throws Exception {
59-
String s = "";
59+
public synchronized static String loadAsString(String fileName) throws Exception {
60+
StringBuilder s = new StringBuilder();
6061

61-
BufferedReader reader = new BufferedReader(new FileReader(new File(
62-
fileName)));
63-
String add;
62+
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
6463

65-
while ((add = reader.readLine()) != null)
66-
s += add + System.getProperty("line.separator");
64+
for (String add = reader.readLine(); add != null; add = reader.readLine()) {
65+
s.append(EncodeUtils.unicodeToString(add)).append(System.getProperty("line.separator"));
66+
}
6767

6868
reader.close();
6969

70-
return s;
70+
return s.toString();
7171
}
7272

7373
/**

src/the/bytecode/club/bytecodeviewer/decompilers/ProcyonDecompiler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.strobel.assembler.metadata.TypeReference;
3636

3737
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
38+
import the.bytecode.club.bytecodeviewer.util.EncodeUtils;
3839
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
3940

4041
/***************************************************************************
@@ -134,9 +135,8 @@ public String decompileClassNode(ClassNode cn, byte[] b) {
134135
}
135136
StringWriter stringwriter = new StringWriter();
136137
settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(stringwriter), decompilationOptions);
137-
String decompiledSource = stringwriter.toString();
138138

139-
return decompiledSource;
139+
return EncodeUtils.unicodeToString(stringwriter.toString());
140140
} catch (StackOverflowError | Exception e) {
141141
StringWriter sw = new StringWriter();
142142
e.printStackTrace(new PrintWriter(sw));
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package the.bytecode.club.bytecodeviewer.util;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
5+
import java.io.UnsupportedEncodingException;
6+
import java.nio.charset.StandardCharsets;
7+
import java.util.regex.Matcher;
8+
import java.util.regex.Pattern;
9+
10+
/**
11+
* Encoding Convert Utils
12+
*
13+
* @author hupan
14+
* @date 2019-11-19 14:29
15+
*/
16+
public class EncodeUtils {
17+
18+
public static String stringToUnicode(String s) {
19+
try {
20+
StringBuilder out = new StringBuilder("");
21+
byte[] bytes = s.getBytes("unicode");
22+
23+
for (int i = 0; i < bytes.length - 1; i += 2) {
24+
out.append("\\u");
25+
String str = Integer.toHexString(bytes[i + 1] & 0xff);
26+
for (int j = str.length(); j < 2; j++) {
27+
out.append("0");
28+
}
29+
String str1 = Integer.toHexString(bytes[i] & 0xff);
30+
out.append(str1);
31+
out.append(str);
32+
}
33+
return out.toString();
34+
} catch (UnsupportedEncodingException e) {
35+
e.printStackTrace();
36+
return null;
37+
}
38+
}
39+
40+
public static String unicodeToString(String str) {
41+
42+
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
43+
Matcher matcher = pattern.matcher(str);
44+
char ch;
45+
while (matcher.find()) {
46+
String group = matcher.group(2);
47+
ch = (char) Integer.parseInt(group, 16);
48+
String group1 = matcher.group(1);
49+
str = str.replace(group1, ch + "");
50+
}
51+
return str;
52+
}
53+
54+
public static String convertStringToUTF8(String s) {
55+
if (s == null || StringUtils.EMPTY.equals(s)) {
56+
return null;
57+
}
58+
StringBuilder sb = new StringBuilder();
59+
try {
60+
char c;
61+
for (int i = 0; i < s.length(); i++) {
62+
c = s.charAt(i);
63+
if (c >= 0 && c <= 255) {
64+
sb.append(c);
65+
} else {
66+
byte[] b;
67+
b = Character.toString(c).getBytes(StandardCharsets.UTF_8);
68+
for (int value : b) {
69+
int k = value;
70+
k = k < 0 ? k + 256 : k;
71+
sb.append(Integer.toHexString(k).toUpperCase());
72+
}
73+
}
74+
}
75+
} catch (Exception e) {
76+
e.printStackTrace();
77+
}
78+
return sb.toString();
79+
}
80+
81+
public static String convertUTF8ToString(String s) {
82+
if (s == null || StringUtils.EMPTY.equals(s)) {
83+
return null;
84+
}
85+
s = s.toUpperCase();
86+
int total = s.length() / 2;
87+
int pos = 0;
88+
byte[] buffer = new byte[total];
89+
for (int i = 0; i < total; i++) {
90+
int start = i * 2;
91+
buffer[i] = (byte) Integer.parseInt(s.substring(start, start + 2), 16);
92+
pos++;
93+
}
94+
95+
return new String(buffer, 0, pos, StandardCharsets.UTF_8);
96+
}
97+
98+
}

0 commit comments

Comments
 (0)