refactor: tweaks IO handling (#3723)

Use BrutIO where possible to improve and simplify stream handling.
Ensure streams are closed when no longer needed.

Some minor formatting tweaks and naming consistency.

No functionality changes.
This commit is contained in:
Igor Eisberg
2024-11-10 15:56:47 +02:00
committed by GitHub
parent 880a9587ee
commit c2eab3101c
12 changed files with 93 additions and 101 deletions

View File

@ -26,6 +26,14 @@ import java.io.*;
import java.util.zip.CRC32;
public class BrutIO {
public static byte[] readAndClose(InputStream in) throws IOException {
try {
return IOUtils.toByteArray(in);
} finally {
IOUtils.closeQuietly(in);
}
}
public static void copyAndClose(InputStream in, OutputStream out) throws IOException {
try {
IOUtils.copy(in, out);
@ -60,11 +68,11 @@ public class BrutIO {
return modified;
}
public static CRC32 calculateCrc(InputStream input) throws IOException {
public static CRC32 calculateCrc(InputStream in) throws IOException {
CRC32 crc = new CRC32();
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = input.read(buffer)) != -1) {
while ((bytesRead = in.read(buffer)) != -1) {
crc.update(buffer, 0, bytesRead);
}
return crc;

View File

@ -24,6 +24,8 @@ import java.io.IOException;
import java.util.logging.Logger;
public class ExtCountingDataInput extends ExtDataInput {
private static final Logger LOGGER = Logger.getLogger(ExtCountingDataInput.class.getName());
private final CountingInputStream mCountIn;
public ExtCountingDataInput(LittleEndianDataInputStream in) {
@ -67,6 +69,4 @@ public class ExtCountingDataInput extends ExtDataInput {
}
return array;
}
private static final Logger LOGGER = Logger.getLogger(ExtCountingDataInput.class.getName());
}

View File

@ -17,6 +17,7 @@
package brut.util;
import brut.common.BrutException;
import brut.util.BrutIO;
import org.apache.commons.io.IOUtils;
import java.io.*;
@ -42,8 +43,9 @@ public abstract class Jar {
}
public static File extractToTmp(String resourcePath, String tmpPrefix, Class<?> clazz) throws BrutException {
InputStream in = null;
try {
InputStream in = clazz.getResourceAsStream(resourcePath);
in = clazz.getResourceAsStream(resourcePath);
if (in == null) {
throw new FileNotFoundException(resourcePath);
}
@ -52,14 +54,13 @@ public abstract class Jar {
File fileOut = File.createTempFile(tmpPrefix, suffix + ".tmp");
fileOut.deleteOnExit();
OutputStream out = Files.newOutputStream(fileOut.toPath());
IOUtils.copy(in, out);
in.close();
out.close();
BrutIO.copyAndClose(in, Files.newOutputStream(fileOut.toPath()));
return fileOut;
} catch (IOException ex) {
throw new BrutException("Could not extract resource: " + resourcePath, ex);
} finally {
IOUtils.closeQuietly(in);
}
}
}

View File

@ -17,7 +17,7 @@
package brut.util;
import brut.common.BrutException;
import org.apache.commons.io.IOUtils;
import brut.util.BrutIO;
import java.io.*;
import java.nio.file.Files;
@ -28,7 +28,6 @@ import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
public class OS {
private static final Logger LOGGER = Logger.getLogger("");
public static void rmdir(File dir) throws BrutException {
@ -77,11 +76,7 @@ public class OS {
continue;
}
try {
try (InputStream in = Files.newInputStream(file.toPath())) {
try (OutputStream out = Files.newOutputStream(destFile.toPath())) {
IOUtils.copy(in, out);
}
}
BrutIO.copyAndClose(Files.newInputStream(file.toPath()), Files.newOutputStream(destFile.toPath()));
} catch (IOException ex) {
throw new BrutException("Could not copy file: " + file, ex);
}