Replace try finally with try with resources

This commit is contained in:
SiboVG 2024-08-09 05:16:41 +02:00
parent 595406ab68
commit 3f8b35343c
8 changed files with 25 additions and 63 deletions

View File

@ -50,17 +50,13 @@ public class ResourceDecalImage implements DecalImage {
@Override @Override
public void exportImage(File file) throws IOException { public void exportImage(File file) throws IOException {
InputStream is = getBytes(); InputStream is = getBytes();
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
if (is == null) { if (is == null) {
return; return;
} }
try { try (is; OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
FileUtils.copy(is, os); FileUtils.copy(is, os);
} finally {
is.close();
os.close();
} }
} }

View File

@ -166,12 +166,9 @@ public class DecalRegistry {
if (!exportedFile.exists()) { if (!exportedFile.exists()) {
throw new DecalNotFoundException(exportedFile.getAbsolutePath(), this); throw new DecalNotFoundException(exportedFile.getAbsolutePath(), this);
} }
InputStream rawIs = new FileInputStream(exportedFile); try (InputStream rawIs = new FileInputStream(exportedFile)) {
try {
byte[] bytes = FileUtils.readBytes(rawIs); byte[] bytes = FileUtils.readBytes(rawIs);
return new ByteArrayInputStream(bytes); return new ByteArrayInputStream(bytes);
} finally {
rawIs.close();
} }
} }

View File

@ -24,9 +24,7 @@ public class ZipFileAttachment extends Attachment {
public InputStream getBytes() throws DecalNotFoundException, IOException { public InputStream getBytes() throws DecalNotFoundException, IOException {
String name = getName(); String name = getName();
ZipInputStream zis = new ZipInputStream(zipFileLocation.openStream()); try (ZipInputStream zis = new ZipInputStream(zipFileLocation.openStream())) {
try {
ZipEntry entry = zis.getNextEntry(); ZipEntry entry = zis.getNextEntry();
while (entry != null) { while (entry != null) {
if (entry.getName().equals(name)) { if (entry.getName().equals(name)) {
@ -36,8 +34,6 @@ public class ZipFileAttachment extends Attachment {
entry = zis.getNextEntry(); entry = zis.getNextEntry();
} }
throw new DecalNotFoundException(name, null); throw new DecalNotFoundException(name, null);
} finally {
zis.close();
} }
} }

View File

@ -211,9 +211,9 @@ public class GeneralRocketSaver {
// Open a zip stream to write to. // Open a zip stream to write to.
ZipOutputStream zos = new ZipOutputStream(output); ZipOutputStream zos = new ZipOutputStream(output);
zos.setLevel(9);
// big try block to close the zos. // big try block to close the zos.
try { try (zos) {
zos.setLevel(9);
ZipEntry mainFile = new ZipEntry("rocket.ork"); ZipEntry mainFile = new ZipEntry("rocket.ork");
zos.putNextEntry(mainFile); zos.putNextEntry(mainFile);
@ -241,8 +241,6 @@ public class GeneralRocketSaver {
} }
zos.flush(); zos.flush();
} finally {
zos.close();
} }
} }

View File

@ -128,9 +128,7 @@ public abstract class RockSimComponentFileLoader {
if (is == null) { if (is == null) {
return; return;
} }
InputStreamReader r = null; try (InputStreamReader r = new InputStreamReader(is)) {
try {
r = new InputStreamReader(is);
// Create the CSV reader. Use comma separator. // Create the CSV reader. Use comma separator.
CSVParser parser = new CSVParserBuilder() CSVParser parser = new CSVParserBuilder()
@ -160,13 +158,6 @@ public abstract class RockSimComponentFileLoader {
return; return;
} catch (IOException | CsvValidationException e) { } catch (IOException | CsvValidationException e) {
throw new BugException("Could not read component file", e); throw new BugException("Could not read component file", e);
} finally {
if (r != null) {
try {
r.close();
} catch (IOException ignored) {
}
}
} }
} }

View File

@ -65,13 +65,10 @@ public class SlideSetLoader {
*/ */
public SlideSet load(String filename) throws IOException { public SlideSet load(String filename) throws IOException {
String file = baseDir + filename; String file = baseDir + filename;
InputStream in = getLocalizedFile(file);
try (InputStream in = getLocalizedFile(file)) {
try {
InputStreamReader reader = new InputStreamReader(in, "UTF-8"); InputStreamReader reader = new InputStreamReader(in, "UTF-8");
return load(reader); return load(reader);
} finally {
in.close();
} }
} }

View File

@ -120,42 +120,38 @@ public class SlideSetManager {
private List<String> loadTourList() throws IOException { private List<String> loadTourList() throws IOException {
InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE); InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE);
if (in == null) {
throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found."); try (in) {
} if (in == null) {
throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found.");
try { }
List<String> tours = new ArrayList<>(); List<String> tours = new ArrayList<>();
TextLineReader reader = new TextLineReader(in); TextLineReader reader = new TextLineReader(in);
while (reader.hasNext()) { while (reader.hasNext()) {
tours.add(reader.next()); tours.add(reader.next());
} }
return tours; return tours;
} finally {
in.close();
} }
} }
private StyleSheet loadStyleSheet() throws IOException { private StyleSheet loadStyleSheet() throws IOException {
InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE); InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE);
if (in == null) {
throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found."); try (in) {
} if (in == null) {
throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found.");
try { }
StyleSheet ss = new StyleSheet(); StyleSheet ss = new StyleSheet();
ss.addRule(String.format("p { color: rgb(%d, %d, %d, %d)", ss.addRule(String.format("p { color: rgb(%d, %d, %d, %d)",
textColor.getRed(), textColor.getGreen(), textColor.getBlue(), textColor.getAlpha())); textColor.getRed(), textColor.getGreen(), textColor.getBlue(), textColor.getAlpha()));
InputStreamReader reader = new InputStreamReader(in, "UTF-8"); InputStreamReader reader = new InputStreamReader(in, "UTF-8");
ss.loadRules(reader, null); ss.loadRules(reader, null);
return ss; return ss;
} finally {
in.close();
} }
} }

View File

@ -153,10 +153,8 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
} }
// Iterate over JAR entries searching for designs // Iterate over JAR entries searching for designs
JarFile jarFile = null; try (JarFile jarFile = new JarFile(file)) {
try {
jarFile = new JarFile(file);
// Loop through JAR entries searching for files to load // Loop through JAR entries searching for files to load
Enumeration<JarEntry> entries = jarFile.entries(); Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) { while (entries.hasMoreElements()) {
@ -169,18 +167,11 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
name.substring(dirLength, name.length() - 4))); name.substring(dirLength, name.length() - 4)));
} }
} }
} catch (IOException e) { } catch (IOException e) {
logger.error("IOException when processing jarFile", e); logger.error("IOException when processing jarFile", e);
// Could be normal condition if not package in JAR // Could be normal condition if not package in JAR
return null; return null;
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
}
}
} }
return list.toArray(new ExampleDesignFile[0]); return list.toArray(new ExampleDesignFile[0]);