From cd5d4c0385f0c6874f0c243d9a7cb4edf086a33b Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Mon, 14 Sep 2009 02:37:26 +0000 Subject: [PATCH] Use proper hygiene. Close those files/streams! git-svn-id: https://smali.googlecode.com/svn/trunk@477 55b6fa8a-2a1e-11de-a435-ffa8d773f76a --- .../src/main/java/org/jf/dexlib/DexFile.java | 135 ++++++++++-------- 1 file changed, 72 insertions(+), 63 deletions(-) diff --git a/dexlib/src/main/java/org/jf/dexlib/DexFile.java b/dexlib/src/main/java/org/jf/dexlib/DexFile.java index 4ac21ff6..77a3a547 100644 --- a/dexlib/src/main/java/org/jf/dexlib/DexFile.java +++ b/dexlib/src/main/java/org/jf/dexlib/DexFile.java @@ -285,77 +285,86 @@ public class DexFile this(preserveSignedRegisters); long fileLength; - InputStream inputStream; byte[] magic = FileUtils.readFile(file, 0, 8); - //do we have a zip file? - if (magic[0] == 0x50 && magic[1] == 0x4B) { - ZipFile zipFile = new ZipFile(file); - ZipEntry zipEntry = zipFile.getEntry("classes.dex"); - if (zipEntry == null) { - throw new RuntimeException("zip file " + file.getName() + " does not contain a classes.dex file"); - } - fileLength = zipEntry.getSize(); - if (fileLength < 40) { - throw new RuntimeException("The classes.dex file in " + file.getName() + " is too small to be a" + - " valid dex file"); - } else if (fileLength > Integer.MAX_VALUE) { - throw new RuntimeException("The classes.dex file in " + file.getName() + " is too large to read in"); - } - inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); + InputStream inputStream = null; + Input in = null; + ZipFile zipFile = null; + + try { + //do we have a zip file? + if (magic[0] == 0x50 && magic[1] == 0x4B) { + zipFile = new ZipFile(file); + ZipEntry zipEntry = zipFile.getEntry("classes.dex"); + if (zipEntry == null) { + throw new RuntimeException("zip file " + file.getName() + " does not contain a classes.dex file"); + } + fileLength = zipEntry.getSize(); + if (fileLength < 40) { + throw new RuntimeException("The classes.dex file in " + file.getName() + " is too small to be a" + + " valid dex file"); + } else if (fileLength > Integer.MAX_VALUE) { + throw new RuntimeException("The classes.dex file in " + file.getName() + " is too large to read in"); + } + inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); - inputStream.mark(8); + inputStream.mark(8); + for (int i=0; i<8; i++) { + magic[i] = (byte)inputStream.read(); + } + inputStream.reset(); + } else { + fileLength = file.length(); + if (fileLength < 40) { + throw new RuntimeException(file.getName() + " is too small to be a valid dex file"); + } + if (fileLength < 40) { + throw new RuntimeException(file.getName() + " is too small to be a valid dex file"); + } else if (fileLength > Integer.MAX_VALUE) { + throw new RuntimeException(file.getName() + " is too large to read in"); + } + inputStream = new FileInputStream(file); + } + + byte[] dexMagic, odexMagic; + + dexMagic = org.jf.dexlib.HeaderItem.MAGIC; + odexMagic = OdexHeaderItem.MAGIC; + + boolean isDex = true; + this.isOdex = true; for (int i=0; i<8; i++) { - magic[i] = (byte)inputStream.read(); + if (magic[i] != dexMagic[i]) { + isDex = false; + } + if (magic[i] != odexMagic[i]) { + isOdex = false; + } } - inputStream.reset(); - - } else { - fileLength = file.length(); - if (fileLength < 40) { - throw new RuntimeException(file.getName() + " is too small to be a valid dex file"); + + if (isOdex) { + byte[] odexHeaderBytes = FileUtils.readStream(inputStream, 40); + Input odexHeaderIn = new ByteArrayInput(odexHeaderBytes); + OdexHeaderItem odexHeader = new OdexHeaderItem(odexHeaderIn); + + in = new ByteArrayInput(FileUtils.readStream(inputStream, odexHeader.dexLength)); + } else if (isDex) { + in = new ByteArrayInput(FileUtils.readStream(inputStream, (int)fileLength)); + } else { + StringBuffer sb = new StringBuffer("bad magic value:"); + for (int i=0; i<8; i++) { + sb.append(" "); + sb.append(Hex.u1(magic[i])); + } + throw new RuntimeException(sb.toString()); } - if (fileLength < 40) { - throw new RuntimeException(file.getName() + " is too small to be a valid dex file"); - } else if (fileLength > Integer.MAX_VALUE) { - throw new RuntimeException(file.getName() + " is too large to read in"); + } finally { + if (inputStream != null) { + inputStream.close(); } - inputStream = new FileInputStream(file); - } - - byte[] dexMagic, odexMagic; - - dexMagic = org.jf.dexlib.HeaderItem.MAGIC; - odexMagic = OdexHeaderItem.MAGIC; - - boolean isDex = true; - this.isOdex = true; - for (int i=0; i<8; i++) { - if (magic[i] != dexMagic[i]) { - isDex = false; + if (zipFile != null) { + zipFile.close(); } - if (magic[i] != odexMagic[i]) { - isOdex = false; - } - } - - Input in; - - if (isOdex) { - byte[] odexHeaderBytes = FileUtils.readStream(inputStream, 40); - Input odexHeaderIn = new ByteArrayInput(odexHeaderBytes); - OdexHeaderItem odexHeader = new OdexHeaderItem(odexHeaderIn); - - in = new ByteArrayInput(FileUtils.readStream(inputStream, odexHeader.dexLength)); - } else if (isDex) { - in = new ByteArrayInput(FileUtils.readStream(inputStream, (int)fileLength)); - } else { - StringBuffer sb = new StringBuffer("bad magic value:"); - for (int i=0; i<8; i++) { - sb.append(" "); - sb.append(Hex.u1(magic[i])); - } - throw new RuntimeException(sb.toString()); } ReadContext readContext = new ReadContext(this);