changeset 730:87170fc5a587

6764062: Revise usage of java.io.*.close Summary: Handle closing multiple open I/O streams in case close() throws IOException Reviewed-by: ksrini
author mchung
date Fri, 05 Dec 2008 10:28:15 -0800
parents a99a2d2f3249
children baa10242c544
files src/share/classes/com/sun/servicetag/Installer.java src/share/classes/com/sun/servicetag/SunConnection.java src/share/classes/com/sun/servicetag/Util.java src/share/classes/com/sun/servicetag/WindowsSystemEnvironment.java
diffstat 4 files changed, 58 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/com/sun/servicetag/Installer.java	Thu Dec 04 17:58:10 2008 +0100
+++ b/src/share/classes/com/sun/servicetag/Installer.java	Fri Dec 05 10:28:15 2008 -0800
@@ -475,7 +475,7 @@
 
         String filename = "/com/sun/servicetag/resources/javase_" +
                 version + "_swordfish.properties";
-        InputStream in = Installer.class.getClass().getResourceAsStream(filename);
+        InputStream in = Installer.class.getResourceAsStream(filename);
         if (in == null) {
             return null;
         }
@@ -813,7 +813,7 @@
                                    locale,
                                    String.valueOf(version)).toString();
             try {
-                in = Installer.class.getClass().getResourceAsStream(resource + ".html");
+                in = Installer.class.getResourceAsStream(resource + ".html");
                 if (in == null) {
                     // if the resource file is missing
                     if (isVerbose()) {
@@ -825,34 +825,39 @@
                     System.out.println("Generating " + f + " from " + resource + ".html");
                 }
 
-                br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
-                pw = new PrintWriter(f, "UTF-8");
-                String line = null;
-                while ((line = br.readLine()) != null) {
-                    String output = line;
-                    if (line.contains(JDK_VERSION_KEY)) {
-                        output = line.replace(JDK_VERSION_KEY, jdkVersion);
-                    } else if (line.contains(JDK_HEADER_PNG_KEY)) {
-                        output = line.replace(JDK_HEADER_PNG_KEY, headerImageSrc);
-                    } else if (line.contains(REGISTRATION_URL_KEY)) {
-                        output = line.replace(REGISTRATION_URL_KEY, registerURL);
-                    } else if (line.contains(REGISTRATION_PAYLOAD_KEY)) {
-                        output = line.replace(REGISTRATION_PAYLOAD_KEY, payload.toString());
+                try {
+                    br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
+                    pw = new PrintWriter(f, "UTF-8");
+                    String line = null;
+                    while ((line = br.readLine()) != null) {
+                        String output = line;
+                        if (line.contains(JDK_VERSION_KEY)) {
+                            output = line.replace(JDK_VERSION_KEY, jdkVersion);
+                        } else if (line.contains(JDK_HEADER_PNG_KEY)) {
+                            output = line.replace(JDK_HEADER_PNG_KEY, headerImageSrc);
+                        } else if (line.contains(REGISTRATION_URL_KEY)) {
+                            output = line.replace(REGISTRATION_URL_KEY, registerURL);
+                        } else if (line.contains(REGISTRATION_PAYLOAD_KEY)) {
+                            output = line.replace(REGISTRATION_PAYLOAD_KEY, payload.toString());
+                        }
+                        pw.println(output);
                     }
-                    pw.println(output);
+                    f.setReadOnly();
+                    pw.flush();
+                } finally {
+                    // It's safe for this finally block to have two close statements
+                    // consecutively as PrintWriter.close doesn't throw IOException.
+                    if (pw != null) {
+                        pw.close();
+                    }
+                    if (br!= null) {
+                        br.close();
+                    }
                 }
-                f.setReadOnly();
-                pw.flush();
             } finally {
-                if (pw != null) {
-                    pw.close();
-                }
                 if (in != null) {
                     in.close();
                 }
-                if (br!= null) {
-                    br.close();
-                }
             }
         }
     }
--- a/src/share/classes/com/sun/servicetag/SunConnection.java	Thu Dec 04 17:58:10 2008 +0100
+++ b/src/share/classes/com/sun/servicetag/SunConnection.java	Fri Dec 05 10:28:15 2008 -0800
@@ -213,10 +213,16 @@
             con.setRequestProperty("Content-Type", "text/xml;charset=\"utf-8\"");
             con.connect();
 
-            OutputStream out = con.getOutputStream();
-            registration.storeToXML(out);
-            out.flush();
-            out.close();
+            OutputStream out = null;
+            try {
+                out = con.getOutputStream();
+                registration.storeToXML(out);
+                out.flush();
+            } finally {
+                if (out != null) {
+                    out.close();
+                }
+            }
 
             int returnCode = con.getResponseCode();
             if (Util.isVerbose()) {
--- a/src/share/classes/com/sun/servicetag/Util.java	Thu Dec 04 17:58:10 2008 +0100
+++ b/src/share/classes/com/sun/servicetag/Util.java	Fri Dec 05 10:28:15 2008 -0800
@@ -140,11 +140,14 @@
             }
             return e.getMessage();
         } finally {
-            if (r != null) {
-                r.close();
-            }
-            if (err != null) {
-                err.close();
+            try {
+                if (r != null) {
+                    r.close();
+                }
+            } finally {
+                if (err != null) {
+                    err.close();
+                }
             }
         }
     }
--- a/src/share/classes/com/sun/servicetag/WindowsSystemEnvironment.java	Thu Dec 04 17:58:10 2008 +0100
+++ b/src/share/classes/com/sun/servicetag/WindowsSystemEnvironment.java	Fri Dec 05 10:28:15 2008 -0800
@@ -107,11 +107,17 @@
             Process p = pb.start();
             // need this for executing windows commands (at least
             // needed for executing wmic command)
-            BufferedWriter bw = new BufferedWriter(
-                new OutputStreamWriter(p.getOutputStream()));
-            bw.write(13);
-            bw.flush();
-            bw.close();
+            BufferedWriter bw = null;
+            try {
+                bw = new BufferedWriter(
+                         new OutputStreamWriter(p.getOutputStream()));
+                bw.write(13);
+                bw.flush();
+            } finally {
+                if (bw != null) {
+                    bw.close();
+                }
+            }
 
             p.waitFor();
             if (p.exitValue() == 0) {