From db959d4704dfea3ad22c502997e1e2360dedec9e Mon Sep 17 00:00:00 2001 From: ayman abdelghany Date: Thu, 2 Jun 2016 13:24:00 +0200 Subject: [PATCH] [squid:S2095] Resources should be closed --- .../club/bytecodeviewer/JarUtils.java | 19 ++++++------------- .../decompilers/JDGUIDecompiler.java | 7 +++---- .../bytecode/InstructionPrinter.java | 4 +--- 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/JarUtils.java b/src/main/java/the/bytecode/club/bytecodeviewer/JarUtils.java index 4fbbe88c..860ec028 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/JarUtils.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/JarUtils.java @@ -192,9 +192,9 @@ public class JarUtils { */ public static void saveAsJar(ArrayList nodeList, String path, String manifest) { - try { - JarOutputStream out = new JarOutputStream( - new FileOutputStream(path)); + try (JarOutputStream out = new JarOutputStream( + new FileOutputStream(path))) { + for (ClassNode cn : nodeList) { ClassWriter cw = new ClassWriter(0); cn.accept(cw); @@ -218,7 +218,6 @@ public class JarUtils { } } - out.close(); } catch (IOException e) { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); } @@ -230,8 +229,7 @@ public class JarUtils { * @param path the exact jar output path */ public static void saveAsJarClassesOnly(ArrayList nodeList, String path) { - try { - JarOutputStream out = new JarOutputStream(new FileOutputStream(path)); + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(path))) { ArrayList noDupe = new ArrayList(); for (ClassNode cn : nodeList) { ClassWriter cw = new ClassWriter(0); @@ -248,15 +246,13 @@ public class JarUtils { } noDupe.clear(); - out.close(); } catch (IOException e) { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); } } public static void saveAsJarClassesOnly(Map nodeList, String path) { - try { - JarOutputStream out = new JarOutputStream(new FileOutputStream(path)); + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(path))) { ArrayList noDupe = new ArrayList(); for (Entry cn : nodeList.entrySet()) { String name = cn.getKey(); @@ -269,15 +265,13 @@ public class JarUtils { } noDupe.clear(); - out.close(); } catch (IOException e) { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); } } public static void saveAsJar(Map nodeList, String path) { - try { - JarOutputStream out = new JarOutputStream(new FileOutputStream(path)); + try (JarOutputStream out = new JarOutputStream(new FileOutputStream(path))) { ArrayList noDupe = new ArrayList(); for (Entry entry : nodeList.entrySet()) { String name = entry.getKey(); @@ -303,7 +297,6 @@ public class JarUtils { } noDupe.clear(); - out.close(); } catch (IOException e) { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/JDGUIDecompiler.java b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/JDGUIDecompiler.java index da518fb1..4fa13e51 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/JDGUIDecompiler.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/JDGUIDecompiler.java @@ -60,9 +60,9 @@ public class JDGUIDecompiler extends Decompiler { @Override public void decompileToZip(String zipName) { - try { - File output = new File(zipName); - ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(output)); + File output = new File(zipName); + try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(output))) { + for (Map.Entry entry : BytecodeViewer.getLoadedBytes().entrySet()) { String name = entry.getKey(); if (name.endsWith(".class")) { @@ -82,7 +82,6 @@ public class JDGUIDecompiler extends Decompiler { } zipOutputStream.closeEntry(); } - zipOutputStream.close(); } catch (Exception e) { handleException(e); } diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/InstructionPrinter.java b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/InstructionPrinter.java index c8916692..d82dfe77 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/InstructionPrinter.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/decompilers/bytecode/InstructionPrinter.java @@ -291,13 +291,11 @@ public class InstructionPrinter { } public static void saveTo(File file, InstructionPrinter printer) { - try { - BufferedWriter bw = new BufferedWriter(new FileWriter(file)); + try (BufferedWriter bw = new BufferedWriter(new FileWriter(file))) { for (String s : printer.createPrint()) { bw.write(s); bw.newLine(); } - bw.close(); } catch (IOException e) { new the.bytecode.club.bytecodeviewer.api.ExceptionUI(e); }