Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add try-with-resources to unclosed streams #10641

Merged
merged 1 commit into from
Oct 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ public void execute() {
parentFolder.mkdirs();
}

try (Writer writer = new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(out), StandardCharsets.UTF_8))) {
try (FileOutputStream fileOutputStream = new FileOutputStream(out);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
Writer writer = new BufferedWriter(outputStreamWriter)) {
writer.write(sb.toString());
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public class ZipUtil {
public void compressFiles(List<File> listFiles, String destZipFile)
throws IOException {

try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(destZipFile))) {
try (FileOutputStream fileOutputStream = new FileOutputStream(destZipFile);
ZipOutputStream zos = new ZipOutputStream(fileOutputStream)) {

for (File file : listFiles) {
if (file.isDirectory()) {
Expand Down Expand Up @@ -84,7 +85,8 @@ private void addFolderToZip(File folder, String parentFolder, ZipOutputStream zo

zos.putNextEntry(new ZipEntry(parentFolder + "/" + file.getName()));

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
try (FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fileInputStream)) {
byte[] bytesIn = new byte[BUFFER_SIZE];
int read;
while ((read = bis.read(bytesIn)) != -1) {
Expand All @@ -109,7 +111,8 @@ private static void addFileToZip(File file, ZipOutputStream zos) throws FileNotF
IOException {
zos.putNextEntry(new ZipEntry(file.getName()));

try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
try (FileInputStream fileInputStream = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fileInputStream)) {
byte[] bytesIn = new byte[BUFFER_SIZE];
int read;
while ((read = bis.read(bytesIn)) != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.slf4j.LoggerFactory;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.*;

Expand Down Expand Up @@ -105,7 +105,9 @@ private void loadFromFile(File targetIgnoreFile) {
}

void loadCodegenRules(final File codegenIgnore) throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(codegenIgnore), Charset.forName("UTF-8")))) {
try (FileInputStream fileInputStream = new FileInputStream(codegenIgnore);
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, StandardCharsets.UTF_8);
BufferedReader reader = new BufferedReader(inputStreamReader)) {
String line;

// NOTE: Comments that start with a : (e.g. //:) are pulled from git documentation for .gitignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,8 @@ public void postProcessFile(File file, String fileType) {
Process p = Runtime.getRuntime().exec(command);
int exitValue = p.waitFor();
if (exitValue != 0) {
try(BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8))) {
try (InputStreamReader inputStreamReader = new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(inputStreamReader)) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -875,13 +875,15 @@ public void postProcessFile(File file, String fileType) {
Process p = Runtime.getRuntime().exec(command);
int exitValue = p.waitFor();
if (exitValue != 0) {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
try (InputStreamReader inputStreamReader = new InputStreamReader(p.getErrorStream(), StandardCharsets.UTF_8);
BufferedReader br = new BufferedReader(inputStreamReader)) {
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line);
}
LOGGER.error("Error running the command ({}). Exit value: {}, Error output: {}", command, exitValue, sb.toString());
}
LOGGER.error("Error running the command ({}). Exit value: {}, Error output: {}", command, exitValue, sb.toString());
} else {
LOGGER.info("Successfully executed: {}", command);
}
Expand Down