|
| 1 | +package io.github.fvarrui.javapackager.utils; |
| 2 | + |
| 3 | +import static io.github.fvarrui.javapackager.utils.CommandUtils.execute; |
| 4 | + |
| 5 | +import java.nio.charset.Charset; |
| 6 | +import java.nio.charset.StandardCharsets; |
| 7 | +import java.util.regex.Matcher; |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +import org.apache.commons.lang3.SystemUtils; |
| 11 | + |
| 12 | +public class CharsetUtil { |
| 13 | + |
| 14 | + 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 | + } |
| 31 | + } |
| 32 | + return Charset.defaultCharset(); |
| 33 | + } |
| 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(); |
| 40 | + } |
| 41 | + |
| 42 | +} |
0 commit comments