Commit 2b1f815c authored by Skylot's avatar Skylot

core: refactor streams closing

parent 0fff1a67
...@@ -22,11 +22,14 @@ import java.util.zip.ZipFile; ...@@ -22,11 +22,14 @@ import java.util.zip.ZipFile;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; 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 // TODO: move to core package
public final class ResourcesLoader { public final class ResourcesLoader {
private static final Logger LOG = LoggerFactory.getLogger(ResourcesLoader.class); 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 static final int LOAD_SIZE_LIMIT = 10 * 1024 * 1024;
private final JadxDecompiler jadxRef; private final JadxDecompiler jadxRef;
...@@ -70,12 +73,10 @@ public final class ResourcesLoader { ...@@ -70,12 +73,10 @@ public final class ResourcesLoader {
if (zipFile != null) { if (zipFile != null) {
zipFile.close(); zipFile.close();
} }
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) { } catch (Exception e) {
LOG.debug("Error close zip file: {}", zipRef, e); LOG.error("Error close zip file: {}", zipRef, e);
} }
close(inputStream);
} }
return result; return result;
} }
...@@ -149,24 +150,12 @@ public final class ResourcesLoader { ...@@ -149,24 +150,12 @@ public final class ResourcesLoader {
ResourceFile rf = new ResourceFile(jadxRef, name, type); ResourceFile rf = new ResourceFile(jadxRef, name, type);
rf.setZipRef(new ZipRef(zipFile, name)); rf.setZipRef(new ZipRef(zipFile, name));
list.add(rf); list.add(rf);
// LOG.debug("Add resource entry: {}, size: {}", name, entry.getSize());
} }
public static CodeWriter loadToCodeWriter(InputStream is) throws IOException { public static CodeWriter loadToCodeWriter(InputStream is) throws IOException {
CodeWriter cw = new CodeWriter(); CodeWriter cw = new CodeWriter();
ByteArrayOutputStream baos = new ByteArrayOutputStream(READ_BUFFER_SIZE); ByteArrayOutputStream baos = new ByteArrayOutputStream(READ_BUFFER_SIZE);
byte[] buffer = new byte[READ_BUFFER_SIZE]; copyStream(is, baos);
int count;
try {
while ((count = is.read(buffer)) != -1) {
baos.write(buffer, 0, count);
}
} finally {
try {
is.close();
} catch (Exception ignore) {
}
}
cw.add(baos.toString("UTF-8")); cw.add(baos.toString("UTF-8"));
return cw; return cw;
} }
......
...@@ -27,6 +27,8 @@ import java.util.zip.ZipOutputStream; ...@@ -27,6 +27,8 @@ import java.util.zip.ZipOutputStream;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static jadx.core.utils.files.FileUtils.close;
/** /**
* Classes list for import into classpath graph * Classes list for import into classpath graph
*/ */
...@@ -115,13 +117,13 @@ public class ClsSet { ...@@ -115,13 +117,13 @@ public class ClsSet {
out.putNextEntry(new ZipEntry(CLST_PKG_PATH + "/" + CLST_FILENAME)); out.putNextEntry(new ZipEntry(CLST_PKG_PATH + "/" + CLST_FILENAME));
save(out); save(out);
} finally { } finally {
out.close(); close(out);
} }
} else { } else {
throw new JadxRuntimeException("Unknown file format: " + outputName); throw new JadxRuntimeException("Unknown file format: " + outputName);
} }
} finally { } finally {
outputStream.close(); close(outputStream);
} }
} }
...@@ -144,7 +146,7 @@ public class ClsSet { ...@@ -144,7 +146,7 @@ public class ClsSet {
} }
} }
} finally { } finally {
out.close(); close(out);
} }
} }
...@@ -156,7 +158,7 @@ public class ClsSet { ...@@ -156,7 +158,7 @@ public class ClsSet {
try { try {
load(input); load(input);
} finally { } finally {
input.close(); close(input);
} }
} }
...@@ -177,13 +179,13 @@ public class ClsSet { ...@@ -177,13 +179,13 @@ public class ClsSet {
entry = in.getNextEntry(); entry = in.getNextEntry();
} }
} finally { } finally {
in.close(); close(in);
} }
} else { } else {
throw new JadxRuntimeException("Unknown file format: " + name); throw new JadxRuntimeException("Unknown file format: " + name);
} }
} finally { } finally {
inputStream.close(); close(inputStream);
} }
} }
...@@ -213,7 +215,7 @@ public class ClsSet { ...@@ -213,7 +215,7 @@ public class ClsSet {
classes[i].setParents(parents); classes[i].setParents(parents);
} }
} finally { } finally {
in.close(); close(in);
} }
} }
......
...@@ -16,6 +16,8 @@ import org.jetbrains.annotations.Nullable; ...@@ -16,6 +16,8 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import static jadx.core.utils.files.FileUtils.close;
public class CodeWriter { public class CodeWriter {
private static final Logger LOG = LoggerFactory.getLogger(CodeWriter.class); private static final Logger LOG = LoggerFactory.getLogger(CodeWriter.class);
private static final int MAX_FILENAME_LENGTH = 128; private static final int MAX_FILENAME_LENGTH = 128;
...@@ -304,9 +306,7 @@ public class CodeWriter { ...@@ -304,9 +306,7 @@ public class CodeWriter {
} catch (Exception e) { } catch (Exception e) {
LOG.error("Save file error", e); LOG.error("Save file error", e);
} finally { } finally {
if (out != null) { close(out);
out.close();
}
} }
} }
} }
...@@ -6,6 +6,8 @@ import java.io.IOException; ...@@ -6,6 +6,8 @@ import java.io.IOException;
import org.objectweb.asm.ClassReader; import org.objectweb.asm.ClassReader;
import static jadx.core.utils.files.FileUtils.close;
public class AsmUtils { public class AsmUtils {
private AsmUtils() { private AsmUtils() {
...@@ -19,9 +21,7 @@ public class AsmUtils { ...@@ -19,9 +21,7 @@ public class AsmUtils {
ClassReader classReader = new ClassReader(in); ClassReader classReader = new ClassReader(in);
className = classReader.getClassName(); className = classReader.getClassName();
} finally { } finally {
if (in != null) { close(in);
in.close();
}
} }
return className; return className;
} }
......
...@@ -3,13 +3,22 @@ package jadx.core.utils.files; ...@@ -3,13 +3,22 @@ package jadx.core.utils.files;
import jadx.core.utils.exceptions.JadxRuntimeException; import jadx.core.utils.exceptions.JadxRuntimeException;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream; import java.util.jar.JarOutputStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FileUtils { public class FileUtils {
private static final Logger LOG = LoggerFactory.getLogger(FileUtils.class);
public static final int READ_BUFFER_SIZE = 8 * 1024;
private FileUtils() { private FileUtils() {
} }
...@@ -20,21 +29,12 @@ public class FileUtils { ...@@ -20,21 +29,12 @@ public class FileUtils {
JarEntry entry = new JarEntry(entryName); JarEntry entry = new JarEntry(entryName);
entry.setTime(source.lastModified()); entry.setTime(source.lastModified());
jar.putNextEntry(entry); jar.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[8192]; in = new BufferedInputStream(new FileInputStream(source));
while (true) { copyStream(in, jar);
int count = in.read(buffer);
if (count == -1) {
break;
}
jar.write(buffer, 0, count);
}
jar.closeEntry(); jar.closeEntry();
} finally { } finally {
if (in != null) { close(in);
in.close();
}
} }
} }
...@@ -59,4 +59,26 @@ public class FileUtils { ...@@ -59,4 +59,26 @@ public class FileUtils {
} }
return temp; 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);
}
}
} }
...@@ -21,6 +21,8 @@ import org.slf4j.LoggerFactory; ...@@ -21,6 +21,8 @@ import org.slf4j.LoggerFactory;
import com.android.dex.Dex; import com.android.dex.Dex;
import static jadx.core.utils.files.FileUtils.close;
public class InputFile { public class InputFile {
private static final Logger LOG = LoggerFactory.getLogger(InputFile.class); private static final Logger LOG = LoggerFactory.getLogger(InputFile.class);
...@@ -96,14 +98,14 @@ public class InputFile { ...@@ -96,14 +98,14 @@ public class InputFile {
try { try {
IOUtils.copy(inputStream, fos); IOUtils.copy(inputStream, fos);
} finally { } finally {
fos.close(); close(fos);
} }
addDexFile(entryName, loadFromJar(jarFile)); addDexFile(entryName, loadFromJar(jarFile));
} else { } else {
throw new JadxRuntimeException("Unexpected extension in zip: " + ext); throw new JadxRuntimeException("Unexpected extension in zip: " + ext);
} }
} finally { } finally {
inputStream.close(); close(inputStream);
} }
index++; index++;
if (index == 1) { if (index == 1) {
...@@ -144,12 +146,8 @@ public class InputFile { ...@@ -144,12 +146,8 @@ public class InputFile {
} }
FileUtils.addFileToJar(jo, file, clsName + ".class"); FileUtils.addFileToJar(jo, file, clsName + ".class");
} finally { } finally {
if (jo != null) { close(jo);
jo.close(); close(out);
}
if (out != null) {
out.close();
}
} }
return loadFromJar(outFile); return loadFromJar(outFile);
} }
......
...@@ -11,6 +11,10 @@ import com.android.dx.command.DxConsole; ...@@ -11,6 +11,10 @@ import com.android.dx.command.DxConsole;
import com.android.dx.command.dexer.Main; import com.android.dx.command.dexer.Main;
import com.android.dx.command.dexer.Main.Arguments; 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 { public class JavaToDex {
private static final String CHARSET_NAME = "UTF-8"; private static final String CHARSET_NAME = "UTF-8";
...@@ -41,14 +45,14 @@ public class JavaToDex { ...@@ -41,14 +45,14 @@ public class JavaToDex {
PrintStream oldOut = System.out; PrintStream oldOut = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
try { try {
System.setOut(new PrintStream(baos, true, CHARSET_NAME)); setOut(new PrintStream(baos, true, CHARSET_NAME));
DxArgs args = new DxArgs("-", new String[]{javaFile}); DxArgs args = new DxArgs("-", new String[]{javaFile});
resetOutDexVar(); resetOutDexVar();
Main.run(args); run(args);
baos.close();
} catch (Throwable e) { } catch (Throwable e) {
throw new JadxException("dx exception: " + e.getMessage(), e); throw new JadxException("dx exception: " + e.getMessage(), e);
} finally { } finally {
close(baos);
System.setOut(oldOut); System.setOut(oldOut);
} }
try { try {
......
...@@ -19,6 +19,8 @@ import org.w3c.dom.Node; ...@@ -19,6 +19,8 @@ import org.w3c.dom.Node;
import org.w3c.dom.NodeList; import org.w3c.dom.NodeList;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import static jadx.core.utils.files.FileUtils.close;
public class ManifestAttributes { public class ManifestAttributes {
private static final Logger LOG = LoggerFactory.getLogger(ManifestAttributes.class); private static final Logger LOG = LoggerFactory.getLogger(ManifestAttributes.class);
...@@ -63,7 +65,8 @@ public class ManifestAttributes { ...@@ -63,7 +65,8 @@ public class ManifestAttributes {
} }
private Document loadXML(String xml) throws JadxException, ParserConfigurationException, SAXException, IOException { private Document loadXML(String xml) throws JadxException, ParserConfigurationException, SAXException, IOException {
Document doc;InputStream xmlStream = null; Document doc;
InputStream xmlStream = null;
try { try {
xmlStream = ManifestAttributes.class.getResourceAsStream(xml); xmlStream = ManifestAttributes.class.getResourceAsStream(xml);
if (xmlStream == null) { if (xmlStream == null) {
...@@ -72,9 +75,7 @@ public class ManifestAttributes { ...@@ -72,9 +75,7 @@ public class ManifestAttributes {
DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
doc = dBuilder.parse(xmlStream); doc = dBuilder.parse(xmlStream);
} finally { } finally {
if (xmlStream != null) { close(xmlStream);
xmlStream.close();
}
} }
return doc; return doc;
} }
......
...@@ -15,7 +15,6 @@ import jadx.core.dex.visitors.DepthTraversal; ...@@ -15,7 +15,6 @@ import jadx.core.dex.visitors.DepthTraversal;
import jadx.core.dex.visitors.IDexTreeVisitor; import jadx.core.dex.visitors.IDexTreeVisitor;
import jadx.core.utils.exceptions.CodegenException; import jadx.core.utils.exceptions.CodegenException;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.files.FileUtils;
import jadx.tests.api.compiler.DynamicCompiler; import jadx.tests.api.compiler.DynamicCompiler;
import jadx.tests.api.compiler.StaticCompiler; import jadx.tests.api.compiler.StaticCompiler;
import jadx.tests.api.utils.TestUtils; import jadx.tests.api.utils.TestUtils;
...@@ -35,6 +34,8 @@ import java.util.List; ...@@ -35,6 +34,8 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.jar.JarOutputStream; 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.containsString;
import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
...@@ -261,9 +262,9 @@ public abstract class IntegrationTest extends TestUtils { ...@@ -261,9 +262,9 @@ public abstract class IntegrationTest extends TestUtils {
File temp = createTempFile(".jar"); File temp = createTempFile(".jar");
JarOutputStream jo = new JarOutputStream(new FileOutputStream(temp)); JarOutputStream jo = new JarOutputStream(new FileOutputStream(temp));
for (File file : list) { for (File file : list) {
FileUtils.addFileToJar(jo, file, path + "/" + file.getName()); addFileToJar(jo, file, path + "/" + file.getName());
} }
jo.close(); close(jo);
return temp; return temp;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment