Skip to content

Commit 786776f

Browse files
committed
U several improvements and refactoring
1 parent 49528dc commit 786776f

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,44 @@
11
package io.github.fvarrui.javapackager.utils;
22

3-
import static io.github.fvarrui.javapackager.utils.CommandUtils.execute;
4-
53
import java.nio.charset.Charset;
64
import java.nio.charset.StandardCharsets;
7-
import java.util.regex.Matcher;
8-
import java.util.regex.Pattern;
95

106
import org.apache.commons.lang3.SystemUtils;
117

128
public class CharsetUtil {
13-
9+
10+
private static Charset commandLineCharset;
11+
1412
public static Charset getCommandLineCharset(){
15-
if (SystemUtils.IS_OS_WINDOWS) {
16-
try{
17-
String result = execute("cmd", "/k", "chcp");
18-
String code = find("\\d+", result);
19-
switch (code){
20-
case "037": return Charset.forName("IBM037");
21-
case "936": return Charset.forName("gb2312");
22-
case "950": return Charset.forName("big5");
23-
case "1145": return Charset.forName("IBM01145");
24-
case "1200": return StandardCharsets.UTF_16;
25-
case "51936": return Charset.forName("EUC-CN");
26-
case "65001": return StandardCharsets.UTF_8;
27-
}
28-
} catch (Exception e){
29-
// do nothing
30-
}
13+
if (commandLineCharset == null) {
14+
if (SystemUtils.IS_OS_WINDOWS) {
15+
commandLineCharset = chcp();
16+
} else {
17+
commandLineCharset = Charset.defaultCharset();
18+
}
3119
}
32-
return Charset.defaultCharset();
20+
return commandLineCharset;
3321
}
34-
35-
private static String find(String pattern,String data){
36-
Pattern r = Pattern.compile(pattern);
37-
Matcher matcher = r.matcher(data);
38-
matcher.find();
39-
return matcher.group();
22+
23+
private static Charset chcp() {
24+
try{
25+
String result = CommandUtils.run("cmd /k chcp");
26+
String code = StringUtils.find("\\d+", result);
27+
Logger.debug("'chcp' code found: " + code);
28+
switch (code){
29+
case "37":
30+
case "037": return Charset.forName("IBM037");
31+
case "936": return Charset.forName("gb2312");
32+
case "950": return Charset.forName("big5");
33+
case "1145": return Charset.forName("IBM01145");
34+
case "1200": return StandardCharsets.UTF_16;
35+
case "51936": return Charset.forName("EUC-CN");
36+
case "65001": return StandardCharsets.UTF_8;
37+
}
38+
} catch (Exception e){
39+
// do nothing
40+
}
41+
return Charset.defaultCharset();
4042
}
4143

4244
}

src/main/java/io/github/fvarrui/javapackager/utils/CommandUtils.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,13 @@ public static ExecutionResult executeWithResult(File workingDirectory, String ex
6868

6969
return result;
7070
}
71+
72+
public static String run(String command) throws IOException {
73+
Process p = Runtime.getRuntime().exec(command);
74+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
75+
String result = br.readLine();
76+
br.close();
77+
return result;
78+
}
7179

7280
}
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
package io.github.fvarrui.javapackager.utils;
22

3+
import java.util.regex.Matcher;
4+
import java.util.regex.Pattern;
5+
36
public class StringUtils {
4-
7+
58
public static String dosToUnix(String input) {
69
return input.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
710
}
811

12+
public static String find(String pattern, String data) {
13+
Pattern r = Pattern.compile(pattern);
14+
Matcher matcher = r.matcher(data);
15+
matcher.find();
16+
return matcher.group();
17+
}
18+
919
}

0 commit comments

Comments
 (0)