From 7505f91f7f987582bdea1c7f9c2a5b7a726478b0 Mon Sep 17 00:00:00 2001 From: Yoshito Umaoka Date: Tue, 16 Oct 2012 20:00:35 +0000 Subject: [PATCH] ICU-9582 Fixed resource leak warnings in demo and tools. X-SVN-Rev: 32644 --- .../com/ibm/icu/dev/demo/translit/Demo.java | 13 +++++++-- .../ibm/icu/dev/tool/docs/CodeMangler.java | 19 ++++++++++--- .../ibm/icu/dev/tool/docs/GatherAPIData.java | 14 ++++++++-- .../icu/dev/tool/docs/GatherAPIDataOld.java | 14 ++++++++-- .../ibm/icu/dev/tool/docs/ICUJDKCompare.java | 27 ++++++++++++++++--- .../dev/tool/charsetdet/mbcs/BIG5Tool.java | 14 ++++++++-- .../icu/dev/tool/charsetdet/mbcs/EUCTool.java | 14 ++++++++-- .../tool/charsetdet/sbcs/StatisticsTool.java | 12 +++++++-- .../tool/translit/UnicodeSetCloseOver.java | 3 ++- 9 files changed, 110 insertions(+), 20 deletions(-) diff --git a/icu4j/demos/src/com/ibm/icu/dev/demo/translit/Demo.java b/icu4j/demos/src/com/ibm/icu/dev/demo/translit/Demo.java index 87882f9027b..70d5e9c494e 100644 --- a/icu4j/demos/src/com/ibm/icu/dev/demo/translit/Demo.java +++ b/icu4j/demos/src/com/ibm/icu/dev/demo/translit/Demo.java @@ -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 + } + } } } diff --git a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/CodeMangler.java b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/CodeMangler.java index 0ab72be3599..54828f85988 100644 --- a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/CodeMangler.java +++ b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/CodeMangler.java @@ -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; } diff --git a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIData.java b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIData.java index 8741aa4036e..9f2f22cf2af 100644 --- a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIData.java +++ b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIData.java @@ -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; diff --git a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIDataOld.java b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIDataOld.java index b68eafaef03..ed37b7f7c39 100644 --- a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIDataOld.java +++ b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/GatherAPIDataOld.java @@ -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; diff --git a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/ICUJDKCompare.java b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/ICUJDKCompare.java index 2ed1868c53c..22a132f321b 100644 --- a/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/ICUJDKCompare.java +++ b/icu4j/tools/build/src/com/ibm/icu/dev/tool/docs/ICUJDKCompare.java @@ -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); } diff --git a/icu4j/tools/misc/src/com/ibm/icu/dev/tool/charsetdet/mbcs/BIG5Tool.java b/icu4j/tools/misc/src/com/ibm/icu/dev/tool/charsetdet/mbcs/BIG5Tool.java index 540e636ea0e..b6744cb0230 100644 --- a/icu4j/tools/misc/src/com/ibm/icu/dev/tool/charsetdet/mbcs/BIG5Tool.java +++ b/icu4j/tools/misc/src/com/ibm/icu/dev/tool/charsetdet/mbcs/BIG5Tool.java @@ -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