提交 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;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.file.Files;
import java.io.UnsupportedEncodingException;
import java.nio.charset.UnsupportedCharsetException;
import sun.tools.native2ascii.A2NFilter;
import sun.tools.native2ascii.N2AFilter;
/**
* Main program of the native2ascii
......@@ -94,7 +91,7 @@ public class Main {
/**
* Run the converter
*/
public synchronized boolean convert(String argv[]){
public synchronized boolean convert(String argv[]) {
List<String> v = new ArrayList<>(2);
File outputFile = null;
boolean createOutputFile = false;
......@@ -102,14 +99,14 @@ public class Main {
// Parse arguments
for (int i = 0; i < argv.length; i++) {
if (argv[i].equals("-encoding")) {
if ((i + 1) < argv.length){
if ((i + 1) < argv.length) {
encodingString = argv[++i];
} else {
error(getMsg("err.bad.arg"));
usage();
return false;
}
} else if (argv[i].equals("-reverse")){
} else if (argv[i].equals("-reverse")) {
reverse = true;
} else {
if (v.size() > 1) {
......@@ -119,15 +116,18 @@ public class Main {
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();
try {
initializeConverter();
if (v.size() == 1)
if (v.size() == 1) {
inputFileName = v.get(0);
}
if (v.size() == 2) {
inputFileName = v.get(0);
......@@ -137,40 +137,38 @@ public class Main {
if (createOutputFile) {
outputFile = new File(outputFileName);
if (outputFile.exists() && !outputFile.canWrite()) {
throw new Exception(formatMsg("err.cannot.write", outputFileName));
}
if (outputFile.exists() && !outputFile.canWrite()) {
throw new Exception(formatMsg("err.cannot.write", outputFileName));
}
}
if (reverse){
BufferedReader reader = getA2NInput(inputFileName);
Writer osw = getA2NOutput(outputFileName);
String line;
while ((line = reader.readLine()) != null) {
osw.write(line.toCharArray());
osw.write(lineBreak);
if (outputFileName == null) { // flush stdout
osw.flush();
if (reverse) {
try (BufferedReader reader = getA2NInput(inputFileName);
Writer osw = getA2NOutput(outputFileName);) {
String line;
while ((line = reader.readLine()) != null) {
osw.write(line.toCharArray());
osw.write(lineBreak);
if (outputFileName == null) { // flush stdout
osw.flush();
}
}
}
reader.close(); // Close the stream.
osw.close();
} else {
//N2A
String inLine;
BufferedReader in = getN2AInput(inputFileName);
BufferedWriter out = getN2AOutput(outputFileName);
while ((inLine = in.readLine()) != null) {
out.write(inLine.toCharArray());
out.write(lineBreak);
if (outputFileName == null) { // flush stdout
out.flush();
// N2A
try (BufferedReader in = getN2AInput(inputFileName);
BufferedWriter out = getN2AOutput(outputFileName);) {
String inLine;
while ((inLine = in.readLine()) != null) {
out.write(inLine.toCharArray());
out.write(lineBreak);
if (outputFileName == null) { // flush stdout
out.flush();
}
}
}
out.close();
}
// Since we are done rename temporary file to desired output file
if (createOutputFile) {
if (outputFile.exists()) {
......@@ -182,8 +180,7 @@ public class Main {
}
tempFile.renameTo(outputFile);
}
} catch(Exception e){
} catch (Exception e) {
error(e.toString());
return false;
}
......
......@@ -24,7 +24,7 @@
#
# @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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册