changeset 6731:a76f2fa17b56

RT-36637: combed through css code to ensure stream resources are closed
author David Grieve<david.grieve@oracle.com>
date Mon, 14 Apr 2014 17:32:24 -0400
parents 1ea500f788c5
children f8f8ab5638cf
files modules/graphics/src/main/java/com/sun/javafx/css/StyleManager.java modules/graphics/src/main/java/com/sun/javafx/css/Stylesheet.java modules/graphics/src/main/java/com/sun/javafx/css/parser/CSSParser.java
diffstat 3 files changed, 31 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/modules/graphics/src/main/java/com/sun/javafx/css/StyleManager.java	Tue Apr 15 14:31:54 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/javafx/css/StyleManager.java	Mon Apr 14 17:32:24 2014 -0400
@@ -815,12 +815,13 @@
             // We only care about stylesheets from file: URLs.
             if (url != null && "file".equals(url.getProtocol())) {
 
-                final InputStream stream = url.openStream();
+                try (InputStream stream = url.openStream()) {
 
-                // not looking for security, just a checksum. MD5 should be faster than SHA
-                final DigestInputStream dis = new DigestInputStream(stream, MessageDigest.getInstance("MD5"));
-                while (dis.read() != -1) { /* empty loop body is intentional */ }
-                return dis.getMessageDigest().digest();
+                    // not looking for security, just a checksum. MD5 should be faster than SHA
+                    final DigestInputStream dis = new DigestInputStream(stream, MessageDigest.getInstance("MD5"));
+                    while (dis.read() != -1) { /* empty loop body is intentional */ }
+                    return dis.getMessageDigest().digest();
+                }
 
             }
 
--- a/modules/graphics/src/main/java/com/sun/javafx/css/Stylesheet.java	Tue Apr 15 14:31:54 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/javafx/css/Stylesheet.java	Mon Apr 14 17:32:24 2014 -0400
@@ -242,15 +242,10 @@
         if (url == null) return null;
 
         Stylesheet stylesheet = null;
-        InputStream inputStream = null;
-        BufferedInputStream bufferedInputStream = null;
-        DataInputStream dataInputStream = null;
-        try {
-            inputStream = url.openStream();
-            // current bss file is 33k so this leaves a little scope at 40k
-            bufferedInputStream = new BufferedInputStream(inputStream, 40 * 1024);
 
-            dataInputStream = new DataInputStream(bufferedInputStream);
+        try (DataInputStream dataInputStream =
+                     new DataInputStream(new BufferedInputStream(url.openStream(), 40 * 1024))) {
+
             // read file version
             final int bssVersion = dataInputStream.readShort();
             if (bssVersion > Stylesheet.BINARY_CSS_VERSION) {
@@ -263,7 +258,6 @@
             // read binary data
             stylesheet = new Stylesheet(url.toExternalForm());
 
-            boolean retry = false;
             try {
 
                 dataInputStream.mark(Integer.MAX_VALUE);
@@ -286,11 +280,6 @@
         } catch (FileNotFoundException fnfe) {
             // This comes from url.openStream() and is expected.
             // It just means that the .bss file doesn't exist.
-        } finally {
-            try {
-                if (dataInputStream != null) dataInputStream.close();
-            } catch (IOException ignored) {
-            }
         }
 
         // return stylesheet
--- a/modules/graphics/src/main/java/com/sun/javafx/css/parser/CSSParser.java	Tue Apr 15 14:31:54 2014 +0200
+++ b/modules/graphics/src/main/java/com/sun/javafx/css/parser/CSSParser.java	Mon Apr 14 17:32:24 2014 -0400
@@ -197,8 +197,11 @@
         final Stylesheet stylesheet = new Stylesheet();
         if (stylesheetText != null && !stylesheetText.trim().isEmpty()) {
             setInputSource(stylesheetText);
-            Reader reader = new CharArrayReader(stylesheetText.toCharArray());
-            parse(stylesheet, reader);
+            try (Reader reader = new CharArrayReader(stylesheetText.toCharArray())) {
+                parse(stylesheet, reader);
+            } catch (IOException ioe) {
+                // this method doesn't explicitly throw IOException
+            }
         }
         return stylesheet;
     }
@@ -214,8 +217,9 @@
         final Stylesheet stylesheet = new Stylesheet(docbase);
         if (stylesheetText != null && !stylesheetText.trim().isEmpty()) {
             setInputSource(docbase, stylesheetText);
-            Reader reader = new CharArrayReader(stylesheetText.toCharArray());
-            parse(stylesheet, reader);
+            try (Reader reader = new CharArrayReader(stylesheetText.toCharArray())) {
+                parse(stylesheet, reader);
+            }
         }
         return stylesheet;
     }
@@ -234,8 +238,9 @@
         final Stylesheet stylesheet = new Stylesheet(path);
         if (url != null) {
             setInputSource(path, null);
-            Reader reader = new BufferedReader(new InputStreamReader(url.openStream()));
-            parse(stylesheet, reader);
+            try (Reader reader = new BufferedReader(new InputStreamReader(url.openStream()))) {
+                parse(stylesheet, reader);
+            }
         }
         return stylesheet;
     }
@@ -248,8 +253,6 @@
 
         try {
             this.parse(stylesheet, lex);
-            reader.close();
-        } catch (IOException ioe) {
         } catch (Exception ex) {
             // Sometimes bad syntax causes an exception. The code should be
             // fixed to handle the bad syntax, but the fallback is
@@ -269,10 +272,9 @@
         if (stylesheetText != null && !stylesheetText.trim().isEmpty()) {
             setInputSource(node);
             final List<Rule> rules = new ArrayList<Rule>();
-            final Reader reader = new CharArrayReader(stylesheetText.toCharArray());
-            final CSSLexer lexer = CSSLexer.getInstance();
-            lexer.setReader(reader);
-            try {
+            try (Reader reader = new CharArrayReader(stylesheetText.toCharArray())) {
+                final CSSLexer lexer = CSSLexer.getInstance();
+                lexer.setReader(reader);
                 currentToken = nextToken(lexer);
                 final List<Declaration> declarations = declarations(lexer);
                 if (declarations != null && !declarations.isEmpty()) {
@@ -283,7 +285,6 @@
                     );
                     rules.add(rule);
                 }
-                reader.close();
             } catch (IOException ioe) {
             } catch (Exception ex) {
                 // Sometimes bad syntax causes an exception. The code should be
@@ -303,22 +304,21 @@
 
     /** convenience method for unit tests */
     public ParsedValueImpl parseExpr(String property, String expr) {
+        if (property == null || expr == null) return null;
+
         ParsedValueImpl value = null;
-        try {
-            setInputSource(null, property + ": " + expr);
-            char buf[] = new char[expr.length() + 1];
-            System.arraycopy(expr.toCharArray(), 0, buf, 0, expr.length());
-            buf[buf.length-1] = ';';
-
-            Reader reader = new CharArrayReader(buf);
+        setInputSource(null, property + ": " + expr);
+        char buf[] = new char[expr.length() + 1];
+        System.arraycopy(expr.toCharArray(), 0, buf, 0, expr.length());
+        buf[buf.length-1] = ';';
+
+        try (Reader reader = new CharArrayReader(buf)) {
             CSSLexer lex = CSSLexer.getInstance();
             lex.setReader(reader);
 
             currentToken = nextToken(lex);
             CSSParser.Term term = this.expr(lex);
             value = valueFor(property, term);
-
-            reader.close();
         } catch (IOException ioe) {
         } catch (ParseException e) {
             if (LOGGER.isLoggable(Level.WARNING)) {