2121import java .net .URLDecoder ;
2222import java .net .URLEncoder ;
2323import java .nio .charset .Charset ;
24+ import java .nio .charset .StandardCharsets ;
2425import java .util .ArrayList ;
2526import java .util .List ;
2627import java .util .StringTokenizer ;
@@ -36,8 +37,6 @@ public class CompilerJavacForked {
3637
3738 private static final String EOL = "\n " ;
3839
39- private static final String ENCODING = "UTF-8" ;
40-
4140 public static class CompilerConfiguration {
4241
4342 private final Charset encoding ;
@@ -65,8 +64,7 @@ public Iterable<File> getSources() {
6564 }
6665
6766 public void write (File file ) throws IOException {
68- Writer writer = newWriter (file );
69- try {
67+ try (Writer writer = newWriter (file )) {
7068 // encoding
7169 if (encoding != null ) {
7270 writer .write ('C' );
@@ -87,18 +85,15 @@ public void write(File file) throws IOException {
8785 writer .write (source .getCanonicalPath ());
8886 writer .write (EOL );
8987 }
90- } finally {
91- writer .close ();
9288 }
9389 }
9490
9591 public static CompilerConfiguration read (File file ) throws IOException {
9692 Charset encoding = null ;
97- List <String > options = new ArrayList <String >();
98- List <File > sources = new ArrayList <File >();
93+ List <String > options = new ArrayList <>();
94+ List <File > sources = new ArrayList <>();
9995
100- BufferedReader reader = newBufferedReader (file );
101- try {
96+ try (BufferedReader reader = newBufferedReader (file )) {
10297 String str ;
10398 while ((str = reader .readLine ()) != null ) {
10499 String value = str .substring (1 );
@@ -114,8 +109,6 @@ public static CompilerConfiguration read(File file) throws IOException {
114109 break ;
115110 }
116111 }
117- } finally {
118- reader .close ();
119112 }
120113
121114 return new CompilerConfiguration (encoding , options , sources );
@@ -133,9 +126,12 @@ public CompilerOutput(File file) throws IOException {
133126 public void processOutput (File inputFile , File outputFile ) {
134127 try {
135128 writer .write ('O' );
136- writer .write (inputFile != null ? URLEncoder .encode (inputFile .getCanonicalPath (), ENCODING ) : "." );
129+ writer .write (
130+ inputFile != null
131+ ? URLEncoder .encode (inputFile .getCanonicalPath (), StandardCharsets .UTF_8 )
132+ : "." );
137133 writer .write (' ' );
138- writer .write (URLEncoder .encode (outputFile .getCanonicalPath (), ENCODING ));
134+ writer .write (URLEncoder .encode (outputFile .getCanonicalPath (), StandardCharsets . UTF_8 ));
139135 writer .write (EOL );
140136 } catch (IOException e ) {
141137 handleException (e );
@@ -145,7 +141,7 @@ public void processOutput(File inputFile, File outputFile) {
145141 public void addMessage (String path , int line , int column , String message , Kind kind ) {
146142 try {
147143 writer .write ('M' );
148- writer .write (URLEncoder .encode (path , ENCODING ));
144+ writer .write (URLEncoder .encode (path , StandardCharsets . UTF_8 ));
149145 writer .write (' ' );
150146 writer .write (Integer .toString (line ));
151147 writer .write (' ' );
@@ -163,7 +159,7 @@ public void addMessage(String path, int line, int column, String message, Kind k
163159 break ;
164160 }
165161 writer .write (' ' );
166- writer .write (URLEncoder .encode (message , ENCODING ));
162+ writer .write (URLEncoder .encode (message , StandardCharsets . UTF_8 ));
167163 writer .write (EOL );
168164 } catch (IOException e ) {
169165 handleException (e );
@@ -190,27 +186,26 @@ private void handleException(IOException e) {
190186 }
191187
192188 public static void process (File file , CompilerOutputProcessor callback ) throws IOException {
193- BufferedReader reader = newBufferedReader (file );
194- try {
189+ try (BufferedReader reader = newBufferedReader (file )) {
195190 String str ;
196191 while ((str = reader .readLine ()) != null ) {
197192 String value = str .substring (1 );
198193 switch (str .charAt (0 )) {
199194 case 'O' : {
200195 StringTokenizer st = new StringTokenizer (value , " " );
201- String inputPath = URLDecoder .decode (st .nextToken (), ENCODING );
202- String outputPath = URLDecoder .decode (st .nextToken (), ENCODING );
196+ String inputPath = URLDecoder .decode (st .nextToken (), StandardCharsets . UTF_8 );
197+ String outputPath = URLDecoder .decode (st .nextToken (), StandardCharsets . UTF_8 );
203198 callback .processOutput (
204199 !"." .equals (inputPath ) ? new File (inputPath ) : null , new File (outputPath ));
205200 break ;
206201 }
207202 case 'M' : {
208203 StringTokenizer st = new StringTokenizer (value , " " );
209- String path = URLDecoder .decode (st .nextToken (), ENCODING );
204+ String path = URLDecoder .decode (st .nextToken (), StandardCharsets . UTF_8 );
210205 int line = Integer .parseInt (st .nextToken ());
211206 int column = Integer .parseInt (st .nextToken ());
212207 MessageSeverity severity = toSeverity (st .nextToken ());
213- String message = URLDecoder .decode (st .nextToken (), ENCODING );
208+ String message = URLDecoder .decode (st .nextToken (), StandardCharsets . UTF_8 );
214209 callback .addMessage (path , line , column , message , severity );
215210 break ;
216211 }
@@ -222,8 +217,6 @@ public static void process(File file, CompilerOutputProcessor callback) throws I
222217 throw new IllegalArgumentException ();
223218 }
224219 }
225- } finally {
226- reader .close ();
227220 }
228221 }
229222
@@ -239,20 +232,20 @@ private static MessageSeverity toSeverity(String token) {
239232 }
240233 }
241234
242- public static interface CompilerOutputProcessor {
243- public void processOutput (File inputFile , File outputFile );
235+ public interface CompilerOutputProcessor {
236+ void processOutput (File inputFile , File outputFile );
244237
245- public void addMessage (String path , int line , int column , String message , MessageSeverity kind );
238+ void addMessage (String path , int line , int column , String message , MessageSeverity kind );
246239
247- public void addLogMessage (String message );
240+ void addLogMessage (String message );
248241 }
249242
250243 static Writer newWriter (File file ) throws IOException {
251- return new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file ), ENCODING ));
244+ return new BufferedWriter (new OutputStreamWriter (new FileOutputStream (file ), StandardCharsets . UTF_8 ));
252245 }
253246
254247 static BufferedReader newBufferedReader (File file ) throws IOException {
255- return new BufferedReader (new InputStreamReader (new FileInputStream (file ), ENCODING ));
248+ return new BufferedReader (new InputStreamReader (new FileInputStream (file ), StandardCharsets . UTF_8 ));
256249 }
257250
258251 public static void main (String [] args ) throws IOException {
@@ -280,7 +273,7 @@ private static void compile(final CompilerConfiguration config, final CompilerOu
280273 }
281274
282275 final Charset sourceEncoding = config .getSourceEncoding ();
283- final DiagnosticCollector <JavaFileObject > diagnosticCollector = new DiagnosticCollector <JavaFileObject >();
276+ final DiagnosticCollector <JavaFileObject > diagnosticCollector = new DiagnosticCollector <>();
284277 final StandardJavaFileManager standardFileManager =
285278 compiler .getStandardFileManager (diagnosticCollector , null , sourceEncoding );
286279 final Iterable <? extends JavaFileObject > fileObjects =
0 commit comments