add feature: UTF-8 characters support!

This commit is contained in:
hupan 2019-11-20 11:27:55 +08:00
parent 9f3302f7a9
commit 4b87e194b6
4 changed files with 111 additions and 12 deletions

1
.gitignore vendored
View file

@ -6,3 +6,4 @@
.project
.DS_Store
*.iml
/out/

View file

@ -1,5 +1,7 @@
package me.konloch.kontainer.io;
import the.bytecode.club.bytecodeviewer.util.EncodeUtils;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
@ -54,20 +56,18 @@ public class DiskReader {
/**
* Used to load from file
*/
public synchronized static String loadAsString(String fileName)
throws Exception {
String s = "";
public synchronized static String loadAsString(String fileName) throws Exception {
StringBuilder s = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(new File(
fileName)));
String add;
BufferedReader reader = new BufferedReader(new FileReader(new File(fileName)));
while ((add = reader.readLine()) != null)
s += add + System.getProperty("line.separator");
for (String add = reader.readLine(); add != null; add = reader.readLine()) {
s.append(EncodeUtils.unicodeToString(add)).append(System.getProperty("line.separator"));
}
reader.close();
return s;
return s.toString();
}
/**

View file

@ -35,6 +35,7 @@ import com.strobel.assembler.metadata.TypeDefinition;
import com.strobel.assembler.metadata.TypeReference;
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
import the.bytecode.club.bytecodeviewer.util.EncodeUtils;
import the.bytecode.club.bytecodeviewer.util.MiscUtils;
/***************************************************************************
@ -134,9 +135,8 @@ public class ProcyonDecompiler extends Decompiler {
}
StringWriter stringwriter = new StringWriter();
settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(stringwriter), decompilationOptions);
String decompiledSource = stringwriter.toString();
return decompiledSource;
return EncodeUtils.unicodeToString(stringwriter.toString());
} catch (StackOverflowError | Exception e) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));

View file

@ -0,0 +1,98 @@
package the.bytecode.club.bytecodeviewer.util;
import org.apache.commons.lang3.StringUtils;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Encoding Convert Utils
*
* @author hupan
* @date 2019-11-19 14:29
*/
public class EncodeUtils {
public static String stringToUnicode(String s) {
try {
StringBuilder out = new StringBuilder("");
byte[] bytes = s.getBytes("unicode");
for (int i = 0; i < bytes.length - 1; i += 2) {
out.append("\\u");
String str = Integer.toHexString(bytes[i + 1] & 0xff);
for (int j = str.length(); j < 2; j++) {
out.append("0");
}
String str1 = Integer.toHexString(bytes[i] & 0xff);
out.append(str1);
out.append(str);
}
return out.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
public static String unicodeToString(String str) {
Pattern pattern = Pattern.compile("(\\\\u(\\p{XDigit}{4}))");
Matcher matcher = pattern.matcher(str);
char ch;
while (matcher.find()) {
String group = matcher.group(2);
ch = (char) Integer.parseInt(group, 16);
String group1 = matcher.group(1);
str = str.replace(group1, ch + "");
}
return str;
}
public static String convertStringToUTF8(String s) {
if (s == null || StringUtils.EMPTY.equals(s)) {
return null;
}
StringBuilder sb = new StringBuilder();
try {
char c;
for (int i = 0; i < s.length(); i++) {
c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
b = Character.toString(c).getBytes(StandardCharsets.UTF_8);
for (int value : b) {
int k = value;
k = k < 0 ? k + 256 : k;
sb.append(Integer.toHexString(k).toUpperCase());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public static String convertUTF8ToString(String s) {
if (s == null || StringUtils.EMPTY.equals(s)) {
return null;
}
s = s.toUpperCase();
int total = s.length() / 2;
int pos = 0;
byte[] buffer = new byte[total];
for (int i = 0; i < total; i++) {
int start = i * 2;
buffer[i] = (byte) Integer.parseInt(s.substring(start, start + 2), 16);
pos++;
}
return new String(buffer, 0, pos, StandardCharsets.UTF_8);
}
}