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
public void exportImage(File file) throws IOException {
InputStream is = getBytes();
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
if (is == null) {
return;
}
try {
try (is; OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) {
FileUtils.copy(is, os);
} finally {
is.close();
os.close();
}
}

View File

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

View File

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

View File

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

View File

@ -128,9 +128,7 @@ public abstract class RockSimComponentFileLoader {
if (is == null) {
return;
}
InputStreamReader r = null;
try {
r = new InputStreamReader(is);
try (InputStreamReader r = new InputStreamReader(is)) {
// Create the CSV reader. Use comma separator.
CSVParser parser = new CSVParserBuilder()
@ -160,13 +158,6 @@ public abstract class RockSimComponentFileLoader {
return;
} catch (IOException | CsvValidationException 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 {
String file = baseDir + filename;
InputStream in = getLocalizedFile(file);
try {
try (InputStream in = getLocalizedFile(file)) {
InputStreamReader reader = new InputStreamReader(in, "UTF-8");
return load(reader);
} finally {
in.close();
}
}

View File

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

View File

@ -153,9 +153,7 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
}
// Iterate over JAR entries searching for designs
JarFile jarFile = null;
try {
jarFile = new JarFile(file);
try (JarFile jarFile = new JarFile(file)) {
// Loop through JAR entries searching for files to load
Enumeration<JarEntry> entries = jarFile.entries();
@ -174,13 +172,6 @@ public class ExampleDesignFile implements Comparable<ExampleDesignFile> {
logger.error("IOException when processing jarFile", e);
// Could be normal condition if not package in JAR
return null;
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException e) {
}
}
}
return list.toArray(new ExampleDesignFile[0]);