From cec21d328885cae27d9d84e7f05d8899330f1c53 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Thu, 24 Oct 2024 00:50:38 +0300 Subject: [PATCH] [error-prone] close I/O resources properly --- .../context/StylesheetFactoryImpl.java | 12 ++--- .../xhtmlrenderer/test/DocumentDiffTest.java | 51 ++++++++----------- .../java/org/xhtmlrenderer/util/IOUtil.java | 5 ++ .../fop/nbsp/NonBreakPointsLoaderImpl.java | 14 ++--- 4 files changed, 34 insertions(+), 48 deletions(-) diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java index ded6224ab..5870ece45 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/context/StylesheetFactoryImpl.java @@ -30,7 +30,6 @@ import org.xhtmlrenderer.extend.UserAgentCallback; import org.xhtmlrenderer.resource.CSSResource; import org.xhtmlrenderer.util.Configuration; -import org.xhtmlrenderer.util.IOUtil; import org.xhtmlrenderer.util.XRLog; import org.xml.sax.InputSource; @@ -39,7 +38,6 @@ import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; -import java.io.UnsupportedEncodingException; import java.util.Map; import java.util.logging.Level; @@ -98,16 +96,12 @@ private Stylesheet parse(StylesheetInfo info) { // since the null resource stream is wrapped in a BufferedInputStream InputSource inputSource=cr.getResourceInputSource(); if (inputSource==null) return null; - InputStream is = inputSource.getByteStream(); - if (is==null) return null; - try { + try (InputStream is = inputSource.getByteStream()) { + if (is == null) return null; String charset = Configuration.valueFor("xr.stylesheets.charset-name", "UTF-8"); return parse(new InputStreamReader(is, charset), info); - } catch (UnsupportedEncodingException e) { - // Shouldn't happen + } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); - } finally { - IOUtil.close(is); } } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java index e11230715..c0bec09d6 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/test/DocumentDiffTest.java @@ -37,6 +37,7 @@ import java.io.StringWriter; import java.util.logging.Level; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.xhtmlrenderer.util.ImageUtil.withGraphics; public class DocumentDiffTest { @@ -98,45 +99,35 @@ private static String file_to_string(String filename) throws IOException { } private static String file_to_string(File file) throws IOException { - FileReader reader = null; - StringWriter writer = null; - String str; - try { - reader = new FileReader(file); - writer = new StringWriter(); - char[] buf = new char[1000]; - while (true) { - int n = reader.read(buf, 0, 1000); - if (n == -1) { - break; + try (FileReader reader = new FileReader(file, UTF_8)) { + try (StringWriter writer = new StringWriter()) { + char[] buf = new char[1000]; + while (true) { + int n = reader.read(buf, 0, 1000); + if (n == -1) { + break; + } + writer.write(buf, 0, n); } - writer.write(buf, 0, n); - } - str = writer.toString(); - } finally { - if (reader != null) { - reader.close(); - } - if (writer != null) { - writer.close(); + return writer.toString(); } } - return str; } public static void string_to_file(String text, File file) throws IOException { - try (FileWriter writer = new FileWriter(file)) { - StringReader reader = new StringReader(text); - char[] buf = new char[1000]; - while (true) { - int n = reader.read(buf, 0, 1000); - if (n == -1) { - break; + try (FileWriter writer = new FileWriter(file, UTF_8)) { + try (StringReader reader = new StringReader(text)) { + char[] buf = new char[1000]; + while (true) { + int n = reader.read(buf, 0, 1000); + if (n == -1) { + break; + } + writer.write(buf, 0, n); } - writer.write(buf, 0, n); + writer.flush(); } - writer.flush(); } } } diff --git a/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/IOUtil.java b/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/IOUtil.java index 1cd5ab34a..318d8cad3 100644 --- a/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/IOUtil.java +++ b/flying-saucer-core/src/main/java/org/xhtmlrenderer/util/IOUtil.java @@ -127,6 +127,11 @@ public static byte[] readBytes(InputStream is) throws IOException { return result.toByteArray(); } + /** + * @deprecated Use try-with-resources idiom instead. + */ + @Deprecated + @SuppressWarnings("EmptyCatch") public static void close(@Nullable Closeable in) { if (in != null) { try { diff --git a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java index 31fc2af3a..682221bf1 100644 --- a/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java +++ b/flying-saucer-fop/src/main/java/org/xhtmlrenderer/fop/nbsp/NonBreakPointsLoaderImpl.java @@ -25,7 +25,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.util.ArrayList; import java.util.List; import static java.nio.charset.StandardCharsets.UTF_8; @@ -58,17 +57,14 @@ private List loadForKey(String lang) { try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path)) { if (is == null) return null; - BufferedReader r = new BufferedReader(new InputStreamReader(is, UTF_8)); - List result = new ArrayList<>(); - String line; - while ((line = r.readLine()) != null) { - if (line.isEmpty() || line.startsWith("#")) continue; - result.add(line); + try (BufferedReader r = new BufferedReader(new InputStreamReader(is, UTF_8))) { + return r.lines() + .filter(line -> !line.isEmpty()) + .filter(line -> !line.startsWith("#")) + .toList(); } - return result; } catch (IOException e) { throw new RuntimeException("Error while loading nbsp file from path " + path, e); } } - }