diff --git a/jadx-core/src/main/java/jadx/api/ResourcesLoader.java b/jadx-core/src/main/java/jadx/api/ResourcesLoader.java index fa3d358f6b5..8b1afb291fc 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 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 static ResContainer decodeStream(ResourceFile rf, ResourceDecoder decoder 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 @@ private void addEntry(List list, File zipFile, ZipEntry entry) { 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 e1732ba3f4e..bb9ab61f85e 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 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 @@ void save(File output) throws IOException { 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 void save(OutputStream output) throws IOException { } } } finally { - out.close(); + close(out); } } @@ -156,7 +158,7 @@ public void load() throws IOException, DecodeException { try { load(input); } finally { - input.close(); + close(input); } } @@ -177,13 +179,13 @@ public void load(File input) throws IOException, DecodeException { 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 void load(InputStream input) throws IOException, DecodeException { 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 6fe3b02b6b4..59da069e66b 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.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 void save(File file) { } 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 15c2b7cfe86..7164c63363e 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 org.objectweb.asm.ClassReader; +import static jadx.core.utils.files.FileUtils.close; + public class AsmUtils { private AsmUtils() { @@ -19,9 +21,7 @@ public static String getNameFromClassFile(File file) throws IOException { 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 d7e3826de55..8bc4c5ebcff 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 @@ 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 static void addFileToJar(JarOutputStream jar, File source, String entryNa 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 static File createTempFile(String suffix) { } 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 c6f1834eb9a..b0b29df8ed5 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 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 @@ private boolean loadFromZip(String ext) throws IOException, DecodeException { 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 @@ private static Dex loadFromClassFile(File file) throws IOException, DecodeExcept } 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 bb8070d8b98..95263d6b549 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.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 byte[] convert(String javaFile) throws JadxException { 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 35f51fbce2e..7709442c068 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.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 void parseAll() throws Exception { } 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 @@ private Document loadXML(String xml) throws JadxException, ParserConfigurationEx 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 0389d4f18b9..c46c10dc197 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.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.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 File getJarForClass(Class cls) throws IOException { 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; }