Skip to content

Commit 2d25ac8

Browse files
authored
Merge pull request #271 from lhDream/master
Solve the problem of garbled characters
2 parents 5ef4520 + bfe2383 commit 2d25ac8

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

src/main/java/io/github/fvarrui/javapackager/gradle/PackagePlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import edu.sc.seis.launch4j.tasks.Launch4jLibraryTask;
44
import io.github.fvarrui.javapackager.packagers.Context;
5+
import io.github.fvarrui.javapackager.utils.Logger;
56
import org.gradle.api.Plugin;
67
import org.gradle.api.Project;
78

@@ -18,7 +19,6 @@ public class PackagePlugin implements Plugin<Project> {
1819

1920
@Override
2021
public void apply(Project project) {
21-
2222
Context.setContext(new GradleContext(project));
2323

2424
project.getPluginManager().apply("java");
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.github.fvarrui.javapackager.utils;
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
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+
public class CharSetUtil {
11+
12+
public static Charset getCommandLineChartSet(){
13+
try{
14+
Process p = Runtime.getRuntime().exec("cmd /k chcp");
15+
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
16+
String res = br.readLine();
17+
String code = find("\\d+",res);
18+
switch (code){
19+
case "037": return Charset.forName("IBM037");
20+
case "936": return Charset.forName("gb2312");
21+
case "950": return Charset.forName("big5");
22+
case "1145": return Charset.forName("IBM01145");
23+
case "1200": return StandardCharsets.UTF_16;
24+
case "51936": return Charset.forName("EUC-CN");
25+
case "65001": return StandardCharsets.UTF_8;
26+
}
27+
}catch (Exception e){
28+
}
29+
return Charset.defaultCharset();
30+
}
31+
32+
private static String find(String pattern,String data){
33+
Pattern r = Pattern.compile(pattern);
34+
Matcher matcher = r.matcher(data);
35+
matcher.find();
36+
return matcher.group();
37+
}
38+
39+
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package io.github.fvarrui.javapackager.utils;
22

3-
import java.io.BufferedReader;
4-
import java.io.File;
5-
import java.io.IOException;
6-
import java.io.InputStreamReader;
3+
import java.io.*;
74

85
import org.apache.commons.lang3.StringUtils;
96
import org.codehaus.plexus.util.cli.CommandLineException;
@@ -47,7 +44,7 @@ public static ExecutionResult executeWithResult(File workingDirectory, String ex
4744

4845
Process process = command.execute();
4946

50-
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream()));
47+
BufferedReader output = new BufferedReader(new InputStreamReader(process.getInputStream(), CharSetUtil.getCommandLineChartSet()));
5148
BufferedReader error = new BufferedReader(new InputStreamReader(process.getErrorStream()));
5249
while (process.isAlive() || output.ready() || error.ready()) {
5350
if (output.ready()) {

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
import static org.apache.commons.io.FileUtils.writeStringToFile;
44

5-
import java.io.File;
6-
import java.io.IOException;
5+
import java.io.*;
76
import java.util.ArrayList;
87
import java.util.UUID;
98

@@ -67,10 +66,22 @@ public static void render(String templatePath, File output, Object info) throws
6766
try {
6867
String data = render(templatePath, info);
6968
data = data.replaceAll("\\r\\n", "\n").replaceAll("\\r", "\n");
70-
writeStringToFile(output, data, "UTF-8");
69+
writeString(output,data);
7170
} catch (IOException e) {
7271
throw new Exception(e.getMessage(), e);
7372
}
7473
}
74+
75+
public static void writeString(File output,String data) throws Exception{
76+
if(!output.exists()){
77+
output.getParentFile().mkdirs();
78+
}
79+
FileOutputStream fileOutputStream = new FileOutputStream(output);
80+
// write utf-8 BOM
81+
byte[] uft8bom={(byte)0xef,(byte)0xbb,(byte)0xbf};
82+
fileOutputStream.write(uft8bom);
83+
fileOutputStream.write(data.getBytes());
84+
fileOutputStream.close();
85+
}
7586

7687
}

0 commit comments

Comments
 (0)