ICU-9582 Fixed resource leak warnings in demo and tools.

X-SVN-Rev: 32644
This commit is contained in:
Yoshito Umaoka 2012-10-16 20:00:35 +00:00
parent e1eb8b2fab
commit 7505f91f7f
9 changed files with 110 additions and 20 deletions

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1996-2010, International Business Machines Corporation and *
* Copyright (C) 1996-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -588,10 +588,11 @@ public class Demo extends Frame {
static final byte NONE = 0, TITLEWORD = 1, TITLELINE = 2;
static void genTestFile(File sourceFile, Transliterator translit, String variant) {
BufferedReader in = null;
try {
System.out.println("Reading: " + sourceFile.getCanonicalPath());
BufferedReader in = new BufferedReader(
in = new BufferedReader(
new InputStreamReader(
new FileInputStream(sourceFile), "UTF-8"));
String targetFile = sourceFile.getCanonicalPath();
@ -757,6 +758,14 @@ public class Demo extends Frame {
System.out.println("Done Writing");
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
// ignore
}
}
}
}

View File

@ -1,6 +1,6 @@
/**
*******************************************************************************
* Copyright (C) 2004-2010, International Business Machines Corporation and *
* Copyright (C) 2004-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -138,8 +138,9 @@ public class CodeMangler {
if (arg.charAt(0) == '@') {
File argfile = new File(arg.substring(1));
if (argfile.exists() && !argfile.isDirectory()) {
BufferedReader br = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(argfile)));
br = new BufferedReader(new InputStreamReader(new FileInputStream(argfile)));
ArrayList list = new ArrayList();
for (int x = 0; x < args.length; ++x) {
list.add(args[x]);
@ -157,6 +158,15 @@ public class CodeMangler {
catch (IOException e) {
System.err.println("error reading arg file: " + e);
}
finally {
if (br != null) {
try {
br.close();
} catch (Exception e){
// ignore
}
}
}
}
} else {
names.add(arg);
@ -367,7 +377,7 @@ public class CodeMangler {
}
if (line.equals(headerline)) {
if (verbose) System.out.println("no changes necessary to " + infile.getCanonicalPath());
instream.close();
reader.close();
return false; // nothing to do
}
if (verbose) {
@ -382,6 +392,7 @@ public class CodeMangler {
File outp = new File(outpname);
if (!(outp.exists() || outp.mkdirs())) {
System.err.println("could not create directory: '" + outpname + "'");
reader.close();
return false;
}
}
@ -394,6 +405,7 @@ public class CodeMangler {
}
catch (IOException ex) {
System.err.println(ex.getMessage());
reader.close();
return false;
}
}
@ -560,6 +572,7 @@ public class CodeMangler {
if (oldMap != null) {
map = oldMap;
}
reader.close();
outstream.close();
return false;
}

View File

@ -1,6 +1,6 @@
/**
*******************************************************************************
* Copyright (C) 2004-2010, International Business Machines Corporation and *
* Copyright (C) 2004-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -145,9 +145,10 @@ public class GatherAPIData {
OutputStream os = System.out;
if (output != null) {
ZipOutputStream zos = null;
try {
if (zip) {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output + ".zip"));
zos = new ZipOutputStream(new FileOutputStream(output + ".zip"));
zos.putNextEntry(new ZipEntry(output));
os = zos;
} else if (gzip) {
@ -161,6 +162,15 @@ public class GatherAPIData {
re.initCause(e);
throw re;
}
finally {
if (zos != null) {
try {
zos.close();
} catch (Exception e) {
// ignore
}
}
}
}
BufferedWriter bw = null;

View File

@ -1,6 +1,6 @@
/**
*******************************************************************************
* Copyright (C) 2004-2010, International Business Machines Corporation and *
* Copyright (C) 2004-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -136,9 +136,10 @@ public class GatherAPIDataOld {
OutputStream os = System.out;
if (output != null) {
ZipOutputStream zos = null;
try {
if (zip) {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(output + ".zip"));
zos = new ZipOutputStream(new FileOutputStream(output + ".zip"));
zos.putNextEntry(new ZipEntry(output));
os = zos;
} else if (gzip) {
@ -152,6 +153,15 @@ public class GatherAPIDataOld {
re.initCause(e);
throw re;
}
finally {
if (zos != null) {
try {
zos.close();
} catch (Exception e) {
// ignore
}
}
}
}
BufferedWriter bw = null;

View File

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2005-2010, International Business Machines Corporation and *
* Copyright (C) 2005-2012, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
@ -155,12 +155,13 @@ public class ICUJDKCompare {
if (ignorelist != null) {
if (ignorelist.charAt(0) == '@') { // a file containing ignoreinfo
BufferedReader br = null;
try {
ArrayList nl = new ArrayList();
File f = new File(namelist.substring(1));
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
br = new BufferedReader(isr);
String line = null;
while (null != (line = br.readLine())) {
nl.add(line);
@ -171,6 +172,15 @@ public class ICUJDKCompare {
System.err.println(e);
throw new IllegalStateException();
}
finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
// ignore
}
}
}
} else { // a list of ignoreinfo separated by semicolons
ignore = ignorelist.split("\\s*;\\s*");
}
@ -179,12 +189,13 @@ public class ICUJDKCompare {
if (namelist != null) {
String[] names = null;
if (namelist.charAt(0) == '@') { // a file
BufferedReader br = null;
try {
ArrayList nl = new ArrayList();
File f = new File(namelist.substring(1));
FileInputStream fis = new FileInputStream(f);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
br = new BufferedReader(isr);
String line = null;
while (null != (line = br.readLine())) {
nl.add(line);
@ -194,11 +205,19 @@ public class ICUJDKCompare {
catch (Exception e) {
System.err.println(e);
throw new IllegalStateException();
} finally {
if (br != null) {
try {
br.close();
} catch (Exception e) {
// ignore
}
}
}
} else { // a list of names separated by semicolons
names = namelist.split("\\s*;\\s*");
}
processPairInfo(names);
}

View File

@ -1,7 +1,7 @@
/*
***********************************************************************
*
* Copyright (C) 2006, International Business Machines Corporation and
* Copyright (C) 2006-2012, International Business Machines Corporation and
* others. All Rights Reserved.
*
***********************************************************************
@ -110,9 +110,10 @@ public class BIG5Tool {
System.out.println(dir.getName());
File[] files = dir.listFiles();
for (i=0; i<files.length; i++) {
FileInputStream is = null;
try {
if (files[i].isFile()) {
FileInputStream is = new FileInputStream(files[i]);
is = new FileInputStream(files[i]);
fileSize = is.read(buf);
if (option_v) {
System.out.println(files[i].getPath());
@ -161,6 +162,15 @@ public class BIG5Tool {
System.err.println("Exception:" + e);
}
finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
// ignore
}
}
}
}
//

View File

@ -1,7 +1,7 @@
/*
***********************************************************************
*
* Copyright (C) 2005-2010, International Business Machines Corporation and
* Copyright (C) 2005-2012, International Business Machines Corporation and
* others. All Rights Reserved.
*
***********************************************************************
@ -110,9 +110,10 @@ public class EUCTool {
System.out.println(dir.getName());
File[] files = dir.listFiles();
for (i=0; i<files.length; i++) {
FileInputStream is = null;
try {
if (files[i].isFile()) {
FileInputStream is = new FileInputStream(files[i]);
is = new FileInputStream(files[i]);
fileSize = is.read(buf);
if (option_v) {
System.out.println(files[i].getPath());
@ -161,6 +162,15 @@ public class EUCTool {
System.err.println("Exception:" + e);
}
finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
// ignore
}
}
}
}
//

View File

@ -1,6 +1,6 @@
/*
***********************************************************************
* Copyright (C) 2005-2010, International Business Machines *
* Copyright (C) 2005-2012, International Business Machines *
* Corporation and others. All Rights Reserved. *
***********************************************************************
*
@ -192,7 +192,7 @@ public class StatisticsTool implements NGramParser.NGramParserClient, NGramList.
int extension = filename.lastIndexOf(".");
String outputFileName = filename.substring(0, extension) + "-" + inputFile.getEncoding() +
(visual? "-visual.dat" : ".dat");
PrintStream output;
PrintStream output = null;
try {
output = new PrintStream(
@ -200,6 +200,14 @@ public class StatisticsTool implements NGramParser.NGramParserClient, NGramList.
} catch (IOException e) {
System.out.println("? Could not open " + outputFileName + " for writing.");
return;
} finally {
if (output != null) {
try {
output.close();
} catch (Exception e) {
// ignore
}
}
}
int i = 0;

View File

@ -1,6 +1,6 @@
/*
**********************************************************************
* Copyright (c) 2003-2010, International Business Machines
* Copyright (c) 2003-2012, International Business Machines
* Corporation and others. All Rights Reserved.
**********************************************************************
* Author: Alan Liu
@ -178,6 +178,7 @@ class UnicodeSetCloseOver {
}
}
@SuppressWarnings("resource")
static void generateCaseData() throws IOException {
Map equivClasses = createCaseFoldEquivalencyClasses();