From 87c1d28b3a4e6e6c693ac83c8b45e8306854ecc4 Mon Sep 17 00:00:00 2001 From: caalador Date: Thu, 16 Dec 2021 10:58:41 +0200 Subject: [PATCH] chore: close streams (#12491) (#12579) touches #12489 --- .../RewriteLegacyAnnotationsStepTest.java | 5 ++-- .../vaadin/flow/server/DevModeHandler.java | 7 ++--- .../communication/StreamReceiverHandler.java | 4 +-- .../flow/server/frontend/FrontendUtils.java | 26 +++++++++---------- .../webcomponent/WebComponentGenerator.java | 8 +++--- .../flow/testutil/TestBenchHelpers.java | 5 ++-- 6 files changed, 28 insertions(+), 27 deletions(-) diff --git a/flow-migration/src/test/java/com/vaadin/flow/migration/RewriteLegacyAnnotationsStepTest.java b/flow-migration/src/test/java/com/vaadin/flow/migration/RewriteLegacyAnnotationsStepTest.java index fc828d15131..4e54739a614 100644 --- a/flow-migration/src/test/java/com/vaadin/flow/migration/RewriteLegacyAnnotationsStepTest.java +++ b/flow-migration/src/test/java/com/vaadin/flow/migration/RewriteLegacyAnnotationsStepTest.java @@ -286,8 +286,9 @@ private File makeSourceJavaFile(File root, Class clazz) File sourceFile = new File(sourceFolder, name); - InputStream inputStream = clazz.getResourceAsStream(name); - Files.copy(inputStream, sourceFile.toPath()); + try (InputStream inputStream = clazz.getResourceAsStream(name)) { + Files.copy(inputStream, sourceFile.toPath()); + } return sourceFile; } diff --git a/flow-server/src/main/java/com/vaadin/flow/server/DevModeHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/DevModeHandler.java index fff5d49dff3..bafc346f636 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/DevModeHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/DevModeHandler.java @@ -240,9 +240,10 @@ public boolean handleRequest(VaadinSession session, VaadinRequest request, } return false; } else { - InputStream inputStream = DevModeHandler.class - .getResourceAsStream("dev-mode-not-ready.html"); - IOUtils.copy(inputStream, response.getOutputStream()); + try (InputStream inputStream = DevModeHandler.class + .getResourceAsStream("dev-mode-not-ready.html")) { + IOUtils.copy(inputStream, response.getOutputStream()); + } response.setContentType("text/html;charset=utf-8"); return true; } diff --git a/flow-server/src/main/java/com/vaadin/flow/server/communication/StreamReceiverHandler.java b/flow-server/src/main/java/com/vaadin/flow/server/communication/StreamReceiverHandler.java index 8527cbb7793..70838a2c62c 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/communication/StreamReceiverHandler.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/communication/StreamReceiverHandler.java @@ -357,11 +357,11 @@ private boolean handleFileUploadValidationAndData(VaadinSession session, } finally { session.unlock(); } - try { + try (InputStream handledStream = inputStream) { // Store ui reference so we can do cleanup even if node is // detached in some event handler Pair result = streamToReceiver(session, - inputStream, streamReceiver, filename, mimeType, + handledStream, streamReceiver, filename, mimeType, contentLength); if (result.getFirst()) { cleanStreamVariable(session, streamReceiver); diff --git a/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendUtils.java b/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendUtils.java index 6d95274d85d..9ba2c5b0f0a 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendUtils.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/frontend/FrontendUtils.java @@ -307,8 +307,8 @@ private static String getNonNull(String... valueArray) { */ public static String streamToString(InputStream inputStream) { String ret = ""; - try { - return IOUtils.toString(inputStream, StandardCharsets.UTF_8) + try (InputStream handledStream = inputStream) { + return IOUtils.toString(handledStream, StandardCharsets.UTF_8) .replaceAll("\\R", System.lineSeparator()); } catch (IOException exception) { // ignore exception on close() @@ -532,22 +532,22 @@ private static InputStream getStatsFromClassPath(VaadinService service) URL statsUrl = resourceProvider .getApplicationResource(service.getContext(), stats); InputStream stream = null; - try { - stream = statsUrl == null ? null : statsUrl.openStream(); - } catch (IOException exception) { - getLogger().warn("Couldn't read content of stats file {}", stats, - exception); - stream = null; + if (statsUrl != null) { + try (InputStream statsStream = statsUrl.openStream()) { + byte[] buffer = IOUtils.toByteArray(statsStream); + statistics = new Stats(buffer, null); + service.getContext().setAttribute(statistics); + stream = new ByteArrayInputStream(buffer); + } catch (IOException exception) { + getLogger().warn("Couldn't read content of stats file {}", + stats, exception); + stream = null; + } } if (stream == null) { getLogger().error( "Cannot get the 'stats.json' from the classpath '{}'", stats); - } else { - byte[] buffer = IOUtils.toByteArray(stream); - statistics = new Stats(buffer, null); - service.getContext().setAttribute(statistics); - stream = new ByteArrayInputStream(buffer); } return stream; } diff --git a/flow-server/src/main/java/com/vaadin/flow/server/webcomponent/WebComponentGenerator.java b/flow-server/src/main/java/com/vaadin/flow/server/webcomponent/WebComponentGenerator.java index c8ba3f312b3..99dbb77c3f7 100644 --- a/flow-server/src/main/java/com/vaadin/flow/server/webcomponent/WebComponentGenerator.java +++ b/flow-server/src/main/java/com/vaadin/flow/server/webcomponent/WebComponentGenerator.java @@ -16,6 +16,7 @@ package com.vaadin.flow.server.webcomponent; import java.io.IOException; +import java.io.InputStream; import java.io.Serializable; import java.nio.charset.StandardCharsets; import java.util.HashMap; @@ -61,10 +62,9 @@ private WebComponentGenerator() { } private static String getStringResource(String name) { - try { - return IOUtils.toString( - WebComponentGenerator.class.getResourceAsStream(name), - StandardCharsets.UTF_8); + try (InputStream resourceStream = WebComponentGenerator.class + .getResourceAsStream(name)) { + return IOUtils.toString(resourceStream, StandardCharsets.UTF_8); } catch (IOException e) { throw new IllegalArgumentException( "Couldn't load string resource '" + name + "'!", e); diff --git a/flow-test-util/src/main/java/com/vaadin/flow/testutil/TestBenchHelpers.java b/flow-test-util/src/main/java/com/vaadin/flow/testutil/TestBenchHelpers.java index 1a2160ad62a..c81580b813a 100644 --- a/flow-test-util/src/main/java/com/vaadin/flow/testutil/TestBenchHelpers.java +++ b/flow-test-util/src/main/java/com/vaadin/flow/testutil/TestBenchHelpers.java @@ -437,9 +437,8 @@ private static class LazyDndSimulationLoad { "/dnd-simulation.js"); private static String loadDndScript(String scriptLocation) { - InputStream stream = TestBenchHelpers.class - .getResourceAsStream(scriptLocation); - try { + try (InputStream stream = TestBenchHelpers.class + .getResourceAsStream(scriptLocation)) { return IOUtils.readLines(stream, StandardCharsets.UTF_8) .stream().collect(Collectors.joining("\n")); } catch (IOException e) {