Skip to content

Commit

Permalink
add try-with-resources statement to unclosed streams (#10641)
Browse files Browse the repository at this point in the history
  • Loading branch information
artwo committed Oct 21, 2021
1 parent 0a003c3 commit 108e8e5
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
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

0 comments on commit 108e8e5

Please sign in to comment.