From 2b1f815c583b751202c35b45c4192034f05187ac Mon Sep 17 00:00:00 2001 From: Skylot Date: Sat, 19 Mar 2016 19:14:24 +0300 Subject: [PATCH] core: refactor streams closing --- .../main/java/jadx/api/ResourcesLoader.java | 25 +++------- .../src/main/java/jadx/core/clsp/ClsSet.java | 16 ++++--- .../java/jadx/core/codegen/CodeWriter.java | 6 +-- .../main/java/jadx/core/utils/AsmUtils.java | 6 +-- .../java/jadx/core/utils/files/FileUtils.java | 46 ++++++++++++++----- .../java/jadx/core/utils/files/InputFile.java | 14 +++--- .../java/jadx/core/utils/files/JavaToDex.java | 10 ++-- .../jadx/core/xmlgen/ManifestAttributes.java | 9 ++-- .../java/jadx/tests/api/IntegrationTest.java | 7 +-- 9 files changed, 78 insertions(+), 61 deletions(-) diff --git a/jadx-core/src/main/java/jadx/api/ResourcesLoader.java b/jadx-core/src/main/java/jadx/api/ResourcesLoader.java index fa3d358f6..8b1afb291 100644 --- a/jadx-core/src/main/java/jadx/api/ResourcesLoader.java +++ b/jadx-core/src/main/java/jadx/api/ResourcesLoader.java @@ -22,11 +22,14 @@ import java.util.zip.ZipFile; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static jadx.core.utils.files.FileUtils.READ_BUFFER_SIZE; +import static jadx.core.utils.files.FileUtils.close; +import static jadx.core.utils.files.FileUtils.copyStream; + // TODO: move to core package public final class ResourcesLoader { private static final Logger LOG = LoggerFactory.getLogger(ResourcesLoader.class); - private static final int READ_BUFFER_SIZE = 8 * 1024; private static final int LOAD_SIZE_LIMIT = 10 * 1024 * 1024; private final JadxDecompiler jadxRef; @@ -70,12 +73,10 @@ public final class ResourcesLoader { if (zipFile != null) { zipFile.close(); } - if (inputStream != null) { - inputStream.close(); - } } catch (Exception e) { - LOG.debug("Error close zip file: {}", zipRef, e); + LOG.error("Error close zip file: {}", zipRef, e); } + close(inputStream); } return result; } @@ -149,24 +150,12 @@ public final class ResourcesLoader { ResourceFile rf = new ResourceFile(jadxRef, name, type); rf.setZipRef(new ZipRef(zipFile, name)); list.add(rf); - // LOG.debug("Add resource entry: {}, size: {}", name, entry.getSize()); } public static CodeWriter loadToCodeWriter(InputStream is) throws IOException { CodeWriter cw = new CodeWriter(); ByteArrayOutputStream baos = new ByteArrayOutputStream(READ_BUFFER_SIZE); - byte[] buffer = new byte[READ_BUFFER_SIZE]; - int count; - try { - while ((count = is.read(buffer)) != -1) { - baos.write(buffer, 0, count); - } - } finally { - try { - is.close(); - } catch (Exception ignore) { - } - } + copyStream(is, baos); cw.add(baos.toString("UTF-8")); return cw; } diff --git a/jadx-core/src/main/java/jadx/core/clsp/ClsSet.java b/jadx-core/src/main/java/jadx/core/clsp/ClsSet.java index e1732ba3f..bb9ab61f8 100644 --- a/jadx-core/src/main/java/jadx/core/clsp/ClsSet.java +++ b/jadx-core/src/main/java/jadx/core/clsp/ClsSet.java @@ -27,6 +27,8 @@ import java.util.zip.ZipOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static jadx.core.utils.files.FileUtils.close; + /** * Classes list for import into classpath graph */ @@ -115,13 +117,13 @@ public class ClsSet { out.putNextEntry(new ZipEntry(CLST_PKG_PATH + "/" + CLST_FILENAME)); save(out); } finally { - out.close(); + close(out); } } else { throw new JadxRuntimeException("Unknown file format: " + outputName); } } finally { - outputStream.close(); + close(outputStream); } } @@ -144,7 +146,7 @@ public class ClsSet { } } } finally { - out.close(); + close(out); } } @@ -156,7 +158,7 @@ public class ClsSet { try { load(input); } finally { - input.close(); + close(input); } } @@ -177,13 +179,13 @@ public class ClsSet { entry = in.getNextEntry(); } } finally { - in.close(); + close(in); } } else { throw new JadxRuntimeException("Unknown file format: " + name); } } finally { - inputStream.close(); + close(inputStream); } } @@ -213,7 +215,7 @@ public class ClsSet { classes[i].setParents(parents); } } finally { - in.close(); + close(in); } } diff --git a/jadx-core/src/main/java/jadx/core/codegen/CodeWriter.java b/jadx-core/src/main/java/jadx/core/codegen/CodeWriter.java index 6fe3b02b6..59da069e6 100644 --- a/jadx-core/src/main/java/jadx/core/codegen/CodeWriter.java +++ b/jadx-core/src/main/java/jadx/core/codegen/CodeWriter.java @@ -16,6 +16,8 @@ import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static jadx.core.utils.files.FileUtils.close; + public class CodeWriter { private static final Logger LOG = LoggerFactory.getLogger(CodeWriter.class); private static final int MAX_FILENAME_LENGTH = 128; @@ -304,9 +306,7 @@ public class CodeWriter { } catch (Exception e) { LOG.error("Save file error", e); } finally { - if (out != null) { - out.close(); - } + close(out); } } } diff --git a/jadx-core/src/main/java/jadx/core/utils/AsmUtils.java b/jadx-core/src/main/java/jadx/core/utils/AsmUtils.java index 15c2b7cfe..7164c6336 100644 --- a/jadx-core/src/main/java/jadx/core/utils/AsmUtils.java +++ b/jadx-core/src/main/java/jadx/core/utils/AsmUtils.java @@ -6,6 +6,8 @@ import java.io.IOException; import org.objectweb.asm.ClassReader; +import static jadx.core.utils.files.FileUtils.close; + public class AsmUtils { private AsmUtils() { @@ -19,9 +21,7 @@ public class AsmUtils { ClassReader classReader = new ClassReader(in); className = classReader.getClassName(); } finally { - if (in != null) { - in.close(); - } + close(in); } return className; } diff --git a/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java b/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java index d7e3826de..8bc4c5ebc 100644 --- a/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java +++ b/jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java @@ -3,13 +3,22 @@ package jadx.core.utils.files; import jadx.core.utils.exceptions.JadxRuntimeException; import java.io.BufferedInputStream; +import java.io.Closeable; import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + public class FileUtils { + private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class); + + public static final int READ_BUFFER_SIZE = 8 * 1024; private FileUtils() { } @@ -20,21 +29,12 @@ public class FileUtils { JarEntry entry = new JarEntry(entryName); entry.setTime(source.lastModified()); jar.putNextEntry(entry); - in = new BufferedInputStream(new FileInputStream(source)); - byte[] buffer = new byte[8192]; - while (true) { - int count = in.read(buffer); - if (count == -1) { - break; - } - jar.write(buffer, 0, count); - } + in = new BufferedInputStream(new FileInputStream(source)); + copyStream(in, jar); jar.closeEntry(); } finally { - if (in != null) { - in.close(); - } + close(in); } } @@ -59,4 +59,26 @@ public class FileUtils { } return temp; } + + public static void copyStream(InputStream input, OutputStream output) throws IOException { + byte[] buffer = new byte[READ_BUFFER_SIZE]; + while (true) { + int count = input.read(buffer); + if (count == -1) { + break; + } + output.write(buffer, 0, count); + } + } + + public static void close(Closeable c) { + if (c == null) { + return; + } + try { + c.close(); + } catch (IOException e) { + LOG.error("Close exception for {}", c, e); + } + } } diff --git a/jadx-core/src/main/java/jadx/core/utils/files/InputFile.java b/jadx-core/src/main/java/jadx/core/utils/files/InputFile.java index c6f1834eb..b0b29df8e 100644 --- a/jadx-core/src/main/java/jadx/core/utils/files/InputFile.java +++ b/jadx-core/src/main/java/jadx/core/utils/files/InputFile.java @@ -21,6 +21,8 @@ import org.slf4j.LoggerFactory; import com.android.dex.Dex; +import static jadx.core.utils.files.FileUtils.close; + public class InputFile { private static final Logger LOG = LoggerFactory.getLogger(InputFile.class); @@ -96,14 +98,14 @@ public class InputFile { try { IOUtils.copy(inputStream, fos); } finally { - fos.close(); + close(fos); } addDexFile(entryName, loadFromJar(jarFile)); } else { throw new JadxRuntimeException("Unexpected extension in zip: " + ext); } } finally { - inputStream.close(); + close(inputStream); } index++; if (index == 1) { @@ -144,12 +146,8 @@ public class InputFile { } FileUtils.addFileToJar(jo, file, clsName + ".class"); } finally { - if (jo != null) { - jo.close(); - } - if (out != null) { - out.close(); - } + close(jo); + close(out); } return loadFromJar(outFile); } diff --git a/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java b/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java index bb8070d8b..95263d6b5 100644 --- a/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java +++ b/jadx-core/src/main/java/jadx/core/utils/files/JavaToDex.java @@ -11,6 +11,10 @@ import com.android.dx.command.DxConsole; import com.android.dx.command.dexer.Main; import com.android.dx.command.dexer.Main.Arguments; +import static com.android.dx.command.dexer.Main.run; +import static jadx.core.utils.files.FileUtils.close; +import static java.lang.System.setOut; + public class JavaToDex { private static final String CHARSET_NAME = "UTF-8"; @@ -41,14 +45,14 @@ public class JavaToDex { PrintStream oldOut = System.out; ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { - System.setOut(new PrintStream(baos, true, CHARSET_NAME)); + setOut(new PrintStream(baos, true, CHARSET_NAME)); DxArgs args = new DxArgs("-", new String[]{javaFile}); resetOutDexVar(); - Main.run(args); - baos.close(); + run(args); } catch (Throwable e) { throw new JadxException("dx exception: " + e.getMessage(), e); } finally { + close(baos); System.setOut(oldOut); } try { diff --git a/jadx-core/src/main/java/jadx/core/xmlgen/ManifestAttributes.java b/jadx-core/src/main/java/jadx/core/xmlgen/ManifestAttributes.java index 35f51fbce..7709442c0 100644 --- a/jadx-core/src/main/java/jadx/core/xmlgen/ManifestAttributes.java +++ b/jadx-core/src/main/java/jadx/core/xmlgen/ManifestAttributes.java @@ -19,6 +19,8 @@ import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import static jadx.core.utils.files.FileUtils.close; + public class ManifestAttributes { private static final Logger LOG = LoggerFactory.getLogger(ManifestAttributes.class); @@ -63,7 +65,8 @@ public class ManifestAttributes { } private Document loadXML(String xml) throws JadxException, ParserConfigurationException, SAXException, IOException { - Document doc;InputStream xmlStream = null; + Document doc; + InputStream xmlStream = null; try { xmlStream = ManifestAttributes.class.getResourceAsStream(xml); if (xmlStream == null) { @@ -72,9 +75,7 @@ public class ManifestAttributes { DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); doc = dBuilder.parse(xmlStream); } finally { - if (xmlStream != null) { - xmlStream.close(); - } + close(xmlStream); } return doc; } diff --git a/jadx-core/src/test/java/jadx/tests/api/IntegrationTest.java b/jadx-core/src/test/java/jadx/tests/api/IntegrationTest.java index 0389d4f18..c46c10dc1 100644 --- a/jadx-core/src/test/java/jadx/tests/api/IntegrationTest.java +++ b/jadx-core/src/test/java/jadx/tests/api/IntegrationTest.java @@ -15,7 +15,6 @@ import jadx.core.dex.visitors.DepthTraversal; import jadx.core.dex.visitors.IDexTreeVisitor; import jadx.core.utils.exceptions.CodegenException; import jadx.core.utils.exceptions.JadxException; -import jadx.core.utils.files.FileUtils; import jadx.tests.api.compiler.DynamicCompiler; import jadx.tests.api.compiler.StaticCompiler; import jadx.tests.api.utils.TestUtils; @@ -35,6 +34,8 @@ import java.util.List; import java.util.Map; import java.util.jar.JarOutputStream; +import static jadx.core.utils.files.FileUtils.addFileToJar; +import static jadx.core.utils.files.FileUtils.close; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.is; @@ -261,9 +262,9 @@ public abstract class IntegrationTest extends TestUtils { File temp = createTempFile(".jar"); JarOutputStream jo = new JarOutputStream(new FileOutputStream(temp)); for (File file : list) { - FileUtils.addFileToJar(jo, file, path + "/" + file.getName()); + addFileToJar(jo, file, path + "/" + file.getName()); } - jo.close(); + close(jo); return temp; }