From 037d66857093a60a41201384f74a97c3e92603e4 Mon Sep 17 00:00:00 2001 From: Liu Dong Date: Thu, 6 Jun 2019 11:05:49 +0800 Subject: [PATCH] fix #94: file channel not closed when close apk file --- .../java/net/dongliu/apk/parser/ApkFile.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/java/net/dongliu/apk/parser/ApkFile.java b/src/main/java/net/dongliu/apk/parser/ApkFile.java index 45fc219..4d0ff98 100644 --- a/src/main/java/net/dongliu/apk/parser/ApkFile.java +++ b/src/main/java/net/dongliu/apk/parser/ApkFile.java @@ -3,6 +3,7 @@ import net.dongliu.apk.parser.bean.ApkSignStatus; import net.dongliu.apk.parser.utils.Inputs; +import javax.annotation.Nullable; import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; @@ -25,6 +26,8 @@ public class ApkFile extends AbstractApkFile implements Closeable { private final ZipFile zf; private File apkFile; + @Nullable + private FileChannel fileChannel; public ApkFile(File apkFile) throws IOException { this.apkFile = apkFile; @@ -66,8 +69,8 @@ public byte[] getFileData(String path) throws IOException { @Override protected ByteBuffer fileData() throws IOException { - FileChannel channel = new FileInputStream(apkFile).getChannel(); - return channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size()); + fileChannel = new FileInputStream(apkFile).getChannel(); + return fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size()); } @@ -110,8 +113,16 @@ public ApkSignStatus verifyApk() throws IOException { @Override public void close() throws IOException { - super.close(); - zf.close(); + try (Closeable superClosable = new Closeable() { + @Override + public void close() throws IOException { + ApkFile.super.close(); + } + }; + Closeable zipFileClosable = zf; + Closeable fileChannelClosable = fileChannel) { + + } } }