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 authored Dec 14, 2021
1 parent b11caf2 commit 62eeb17
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ void loadProperties() {
.getApplicationResource(PROPERTIES_FILENAME);
if (applicationResource != null) {
getLogger().debug("Properties loaded from classpath.");
try {
loadProperties(applicationResource.openStream());
try (InputStream propertiesStream = applicationResource
.openStream()) {
loadProperties(propertiesStream);
return;
} catch (IOException e) {
throw new UncheckedIOException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,11 @@ protected 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 @@ -383,8 +383,8 @@ public static boolean isWindows() {
*/
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 @@ -598,18 +598,17 @@ private static InputStream getStatsFromClassPath(VaadinService service) {
.getAttribute(Lookup.class).lookup(ResourceProvider.class);
URL statsUrl = resourceProvider.getApplicationResource(stats);
InputStream stream = null;
try {
stream = statsUrl == null ? null : statsUrl.openStream();
if (stream != null) {
byte[] buffer = IOUtils.toByteArray(stream);
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;
}
} catch (IOException exception) {
getLogger().warn("Couldn't read content of stats file {}", stats,
exception);
stream = null;
}
if (stream == null) {
getLogger().error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -49,8 +50,10 @@ public class TaskGenerateIndexHtml extends AbstractTaskClientGenerator {

@Override
protected String getFileContent() throws IOException {
return IOUtils.toString(getClass().getResourceAsStream(INDEX_HTML),
UTF_8);
try (InputStream indexStream = getClass()
.getResourceAsStream(INDEX_HTML)) {
return IOUtils.toString(indexStream, UTF_8);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -76,8 +77,11 @@ protected boolean shouldGenerate() {

@Override
protected String getFileContent() throws IOException {
String indexTemplate = IOUtils
.toString(getClass().getResourceAsStream(INDEX_TS), UTF_8);
String indexTemplate;
try (InputStream indexTsStream = getClass()
.getResourceAsStream(INDEX_TS)) {
indexTemplate = IOUtils.toString(indexTsStream, UTF_8);
}
String relativizedImport = ensureValidRelativePath(
FrontendUtils.getUnixRelativePath(buildDirectory.toPath(),
generatedImports.toPath()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -52,8 +53,10 @@ public class TaskGenerateServiceWorker extends AbstractTaskClientGenerator {

@Override
protected String getFileContent() throws IOException {
return IOUtils.toString(
getClass().getResourceAsStream(SERVICE_WORKER_SRC), UTF_8);
try (InputStream swStream = getClass()
.getResourceAsStream(SERVICE_WORKER_SRC)) {
return IOUtils.toString(swStream, UTF_8);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -46,8 +47,10 @@ public class TaskGenerateTsConfig extends AbstractTaskClientGenerator {

@Override
protected String getFileContent() throws IOException {
return IOUtils.toString(getClass().getResourceAsStream(TSCONFIG_JSON),
UTF_8);
try (InputStream tsConfStream = getClass()
.getResourceAsStream(TSCONFIG_JSON)) {
return IOUtils.toString(tsConfStream, UTF_8);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -47,8 +48,10 @@ public class TaskGenerateTsDefinitions extends AbstractTaskClientGenerator {

@Override
protected String getFileContent() throws IOException {
return IOUtils.toString(getClass().getResourceAsStream(TS_DEFINITIONS),
UTF_8);
try (InputStream tsDefinitionStream = getClass()
.getResourceAsStream(TS_DEFINITIONS)) {
return IOUtils.toString(tsDefinitionStream, UTF_8);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

Expand Down Expand Up @@ -63,9 +64,10 @@ protected boolean shouldGenerate() {

@Override
protected String getFileContent() throws IOException {
return IOUtils.toString(
getClass().getResourceAsStream(FrontendUtils.VITE_DEVMODE_TS),
UTF_8);
try (InputStream devModeStream = getClass()
.getResourceAsStream(FrontendUtils.VITE_DEVMODE_TS)) {
return IOUtils.toString(devModeStream, UTF_8);
}
}

}
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

0 comments on commit 62eeb17

Please sign in to comment.