提交 6a9523f6 编写于 作者: I igerasim

8067964: Native2ascii doesn't close one of the streams it opens

Summary: Change sun.tools.native2ascii.Main.convert() to use try-with-resources; also clean up code formatting.
Reviewed-by: chegar
上级 1efd2b49
...@@ -71,11 +71,8 @@ import java.text.MessageFormat; ...@@ -71,11 +71,8 @@ import java.text.MessageFormat;
import java.nio.charset.CharsetEncoder; import java.nio.charset.CharsetEncoder;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException; import java.nio.charset.IllegalCharsetNameException;
import java.nio.file.Files;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException; import java.nio.charset.UnsupportedCharsetException;
import sun.tools.native2ascii.A2NFilter;
import sun.tools.native2ascii.N2AFilter;
/** /**
* Main program of the native2ascii * Main program of the native2ascii
...@@ -94,7 +91,7 @@ public class Main { ...@@ -94,7 +91,7 @@ public class Main {
/** /**
* Run the converter * Run the converter
*/ */
public synchronized boolean convert(String argv[]){ public synchronized boolean convert(String argv[]) {
List<String> v = new ArrayList<>(2); List<String> v = new ArrayList<>(2);
File outputFile = null; File outputFile = null;
boolean createOutputFile = false; boolean createOutputFile = false;
...@@ -102,14 +99,14 @@ public class Main { ...@@ -102,14 +99,14 @@ public class Main {
// Parse arguments // Parse arguments
for (int i = 0; i < argv.length; i++) { for (int i = 0; i < argv.length; i++) {
if (argv[i].equals("-encoding")) { if (argv[i].equals("-encoding")) {
if ((i + 1) < argv.length){ if ((i + 1) < argv.length) {
encodingString = argv[++i]; encodingString = argv[++i];
} else { } else {
error(getMsg("err.bad.arg")); error(getMsg("err.bad.arg"));
usage(); usage();
return false; return false;
} }
} else if (argv[i].equals("-reverse")){ } else if (argv[i].equals("-reverse")) {
reverse = true; reverse = true;
} else { } else {
if (v.size() > 1) { if (v.size() > 1) {
...@@ -119,15 +116,18 @@ public class Main { ...@@ -119,15 +116,18 @@ public class Main {
v.add(argv[i]); v.add(argv[i]);
} }
} }
if (encodingString == null)
defaultEncoding = Charset.defaultCharset().name();
if (encodingString == null) {
defaultEncoding = Charset.defaultCharset().name();
}
char[] lineBreak = System.getProperty("line.separator").toCharArray(); char[] lineBreak = System.getProperty("line.separator").toCharArray();
try { try {
initializeConverter(); initializeConverter();
if (v.size() == 1) if (v.size() == 1) {
inputFileName = v.get(0); inputFileName = v.get(0);
}
if (v.size() == 2) { if (v.size() == 2) {
inputFileName = v.get(0); inputFileName = v.get(0);
...@@ -137,40 +137,38 @@ public class Main { ...@@ -137,40 +137,38 @@ public class Main {
if (createOutputFile) { if (createOutputFile) {
outputFile = new File(outputFileName); outputFile = new File(outputFileName);
if (outputFile.exists() && !outputFile.canWrite()) { if (outputFile.exists() && !outputFile.canWrite()) {
throw new Exception(formatMsg("err.cannot.write", outputFileName)); throw new Exception(formatMsg("err.cannot.write", outputFileName));
} }
} }
if (reverse){ if (reverse) {
BufferedReader reader = getA2NInput(inputFileName); try (BufferedReader reader = getA2NInput(inputFileName);
Writer osw = getA2NOutput(outputFileName); Writer osw = getA2NOutput(outputFileName);) {
String line; String line;
while ((line = reader.readLine()) != null) {
while ((line = reader.readLine()) != null) { osw.write(line.toCharArray());
osw.write(line.toCharArray()); osw.write(lineBreak);
osw.write(lineBreak); if (outputFileName == null) { // flush stdout
if (outputFileName == null) { // flush stdout osw.flush();
osw.flush(); }
} }
} }
reader.close(); // Close the stream.
osw.close();
} else { } else {
//N2A // N2A
String inLine; try (BufferedReader in = getN2AInput(inputFileName);
BufferedReader in = getN2AInput(inputFileName); BufferedWriter out = getN2AOutput(outputFileName);) {
BufferedWriter out = getN2AOutput(outputFileName); String inLine;
while ((inLine = in.readLine()) != null) {
while ((inLine = in.readLine()) != null) { out.write(inLine.toCharArray());
out.write(inLine.toCharArray()); out.write(lineBreak);
out.write(lineBreak); if (outputFileName == null) { // flush stdout
if (outputFileName == null) { // flush stdout out.flush();
out.flush(); }
} }
} }
out.close();
} }
// Since we are done rename temporary file to desired output file // Since we are done rename temporary file to desired output file
if (createOutputFile) { if (createOutputFile) {
if (outputFile.exists()) { if (outputFile.exists()) {
...@@ -182,8 +180,7 @@ public class Main { ...@@ -182,8 +180,7 @@ public class Main {
} }
tempFile.renameTo(outputFile); tempFile.renameTo(outputFile);
} }
} catch (Exception e) {
} catch(Exception e){
error(e.toString()); error(e.toString());
return false; return false;
} }
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
# #
# @test # @test
# @bug 4630463 4630971 4636448 4701617 4721296 4710890 6247817 7021987 # @bug 4630463 4630971 4636448 4701617 4721296 4710890 6247817 7021987 8067964
# @summary Tests miscellaneous native2ascii bugfixes and regressions # @summary Tests miscellaneous native2ascii bugfixes and regressions
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册