# HG changeset patch # Parent cf9c506dda26e9ddcd25b8a0d1835fad3f70fa6d RT-36637: combed through css code to ensure stream resources are closed diff -r cf9c506dda26 -r 63953303870b modules/graphics/src/main/java/com/sun/javafx/css/StyleManager.java --- a/modules/graphics/src/main/java/com/sun/javafx/css/StyleManager.java Thu Apr 10 15:40:34 2014 -0400 +++ b/modules/graphics/src/main/java/com/sun/javafx/css/StyleManager.java Fri Apr 11 10:22:40 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(); + } } diff -r cf9c506dda26 -r 63953303870b modules/graphics/src/main/java/com/sun/javafx/css/Stylesheet.java --- a/modules/graphics/src/main/java/com/sun/javafx/css/Stylesheet.java Thu Apr 10 15:40:34 2014 -0400 +++ b/modules/graphics/src/main/java/com/sun/javafx/css/Stylesheet.java Fri Apr 11 10:22:40 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 diff -r cf9c506dda26 -r 63953303870b modules/graphics/src/main/java/com/sun/javafx/css/parser/CSSParser.java --- a/modules/graphics/src/main/java/com/sun/javafx/css/parser/CSSParser.java Thu Apr 10 15:40:34 2014 -0400 +++ b/modules/graphics/src/main/java/com/sun/javafx/css/parser/CSSParser.java Fri Apr 11 10:22:40 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 rules = new ArrayList(); - 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 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)) {