Skip to content

Commit

Permalink
chore: close streams (#12491)
Browse files Browse the repository at this point in the history
touches #12489
  • Loading branch information
caalador committed Dec 15, 2021
1 parent 16f0a51 commit 5d3a752
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean, UploadStatus> result = streamToReceiver(session,
inputStream, streamReceiver, filename, mimeType,
handledStream, streamReceiver, filename, mimeType,
contentLength);
if (result.getFirst()) {
cleanStreamVariable(session, streamReceiver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5d3a752

Please sign in to comment.