Use try-with-resources

This commit is contained in:
Lanchon 2017-09-22 21:54:03 -03:00
parent f59046497d
commit 36a2aa8c1a
2 changed files with 3 additions and 12 deletions

View File

@ -38,11 +38,8 @@ public class RawDexIO {
public static DexBackedDexFile readRawDexFile(File file, Opcodes opcodes) throws IOException { public static DexBackedDexFile readRawDexFile(File file, Opcodes opcodes) throws IOException {
/* /*
InputStream inputStream = new FileInputStream(file); try (InputStream inputStream = new FileInputStream(file)) {
try {
return readRawDexFile(inputStream, file.length(), opcodes); return readRawDexFile(inputStream, file.length(), opcodes);
} finally {
inputStream.close();
} }
*/ */
byte[] buf = Files.toByteArray(file); byte[] buf = Files.toByteArray(file);

View File

@ -37,27 +37,21 @@ public class ZipFileDexContainer extends AbstractMultiDexContainer<WrappingMulti
public ZipFileDexContainer(File zip, DexFileNamer namer, Opcodes opcodes) throws IOException { public ZipFileDexContainer(File zip, DexFileNamer namer, Opcodes opcodes) throws IOException {
Map<String, WrappingMultiDexFile<DexBackedDexFile>> entryMap = new TreeMap<>(new DexFileNameComparator(namer)); Map<String, WrappingMultiDexFile<DexBackedDexFile>> entryMap = new TreeMap<>(new DexFileNameComparator(namer));
ZipFile zipFile = new ZipFile(zip); try (ZipFile zipFile = new ZipFile(zip)) {
try {
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries(); Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while (zipEntries.hasMoreElements()) { while (zipEntries.hasMoreElements()) {
ZipEntry zipEntry = zipEntries.nextElement(); ZipEntry zipEntry = zipEntries.nextElement();
String entryName = zipEntry.getName(); String entryName = zipEntry.getName();
if (namer.isValidName(entryName)) { if (namer.isValidName(entryName)) {
DexBackedDexFile dexFile; DexBackedDexFile dexFile;
InputStream inputStream = zipFile.getInputStream(zipEntry); try (InputStream inputStream = zipFile.getInputStream(zipEntry)) {
try {
dexFile = RawDexIO.readRawDexFile(inputStream, zipEntry.getSize(), opcodes); dexFile = RawDexIO.readRawDexFile(inputStream, zipEntry.getSize(), opcodes);
} finally {
inputStream.close();
} }
WrappingMultiDexFile<DexBackedDexFile> multiDexFile = WrappingMultiDexFile<DexBackedDexFile> multiDexFile =
new BasicMultiDexFile<>(this, entryName, dexFile); new BasicMultiDexFile<>(this, entryName, dexFile);
if (entryMap.put(entryName, multiDexFile) != null) throwDuplicateEntryName(entryName); if (entryMap.put(entryName, multiDexFile) != null) throwDuplicateEntryName(entryName);
} }
} }
} finally {
zipFile.close();
} }
initialize(entryMap, opcodes); initialize(entryMap, opcodes);
} }