diff --git a/core/src/main/java/info/openrocket/core/appearance/defaults/ResourceDecalImage.java b/core/src/main/java/info/openrocket/core/appearance/defaults/ResourceDecalImage.java index 90b539f51..052ca90f8 100644 --- a/core/src/main/java/info/openrocket/core/appearance/defaults/ResourceDecalImage.java +++ b/core/src/main/java/info/openrocket/core/appearance/defaults/ResourceDecalImage.java @@ -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(); } } diff --git a/core/src/main/java/info/openrocket/core/document/DecalRegistry.java b/core/src/main/java/info/openrocket/core/document/DecalRegistry.java index f5eed91bc..75875e96b 100644 --- a/core/src/main/java/info/openrocket/core/document/DecalRegistry.java +++ b/core/src/main/java/info/openrocket/core/document/DecalRegistry.java @@ -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(); } } diff --git a/core/src/main/java/info/openrocket/core/document/attachments/ZipFileAttachment.java b/core/src/main/java/info/openrocket/core/document/attachments/ZipFileAttachment.java index e613bcd24..fdd226e73 100644 --- a/core/src/main/java/info/openrocket/core/document/attachments/ZipFileAttachment.java +++ b/core/src/main/java/info/openrocket/core/document/attachments/ZipFileAttachment.java @@ -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(); } } diff --git a/core/src/main/java/info/openrocket/core/file/GeneralRocketSaver.java b/core/src/main/java/info/openrocket/core/file/GeneralRocketSaver.java index 88e8a0eed..28cd06387 100644 --- a/core/src/main/java/info/openrocket/core/file/GeneralRocketSaver.java +++ b/core/src/main/java/info/openrocket/core/file/GeneralRocketSaver.java @@ -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(); } } diff --git a/core/src/main/java/info/openrocket/core/preset/loader/RockSimComponentFileLoader.java b/core/src/main/java/info/openrocket/core/preset/loader/RockSimComponentFileLoader.java index d4a3c571e..240ed13b6 100644 --- a/core/src/main/java/info/openrocket/core/preset/loader/RockSimComponentFileLoader.java +++ b/core/src/main/java/info/openrocket/core/preset/loader/RockSimComponentFileLoader.java @@ -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) { - } - } } } diff --git a/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetLoader.java b/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetLoader.java index a04acfa7f..fa9e64452 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetLoader.java +++ b/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetLoader.java @@ -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(); } } diff --git a/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetManager.java b/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetManager.java index 8ba481250..ffa3345f8 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetManager.java +++ b/swing/src/main/java/info/openrocket/swing/gui/help/tours/SlideSetManager.java @@ -120,42 +120,38 @@ public class SlideSetManager { private List loadTourList() throws IOException { InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + TOURS_FILE); - if (in == null) { - throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found."); - } - - try { - + + try (in) { + if (in == null) { + throw new FileNotFoundException("File '" + baseDir + TOURS_FILE + "' not found."); + } + List tours = new ArrayList<>(); TextLineReader reader = new TextLineReader(in); while (reader.hasNext()) { tours.add(reader.next()); } return tours; - - } finally { - in.close(); + } } private StyleSheet loadStyleSheet() throws IOException { InputStream in = ClassLoader.getSystemResourceAsStream(baseDir + STYLESHEET_FILE); - if (in == null) { - throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found."); - } - - try { - + + try (in) { + if (in == null) { + throw new FileNotFoundException("File '" + baseDir + STYLESHEET_FILE + "' not found."); + } + StyleSheet ss = new StyleSheet(); ss.addRule(String.format("p { color: rgb(%d, %d, %d, %d)", textColor.getRed(), textColor.getGreen(), textColor.getBlue(), textColor.getAlpha())); InputStreamReader reader = new InputStreamReader(in, "UTF-8"); ss.loadRules(reader, null); return ss; - - } finally { - in.close(); + } } diff --git a/swing/src/main/java/info/openrocket/swing/gui/main/ExampleDesignFile.java b/swing/src/main/java/info/openrocket/swing/gui/main/ExampleDesignFile.java index 55f63af5c..221f026b6 100644 --- a/swing/src/main/java/info/openrocket/swing/gui/main/ExampleDesignFile.java +++ b/swing/src/main/java/info/openrocket/swing/gui/main/ExampleDesignFile.java @@ -153,10 +153,8 @@ public class ExampleDesignFile implements Comparable { } // 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 entries = jarFile.entries(); while (entries.hasMoreElements()) { @@ -169,18 +167,11 @@ public class ExampleDesignFile implements Comparable { name.substring(dirLength, name.length() - 4))); } } - + } catch (IOException e) { 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]);