[core] Fix resource leak (#813)

This commit is contained in:
vvb2060 2021-07-15 00:29:23 +08:00 committed by GitHub
parent bfe31837ad
commit 521786f6af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View File

@ -14,16 +14,16 @@ import java.util.zip.ZipEntry;
import sun.net.www.ParseUtil; import sun.net.www.ParseUtil;
public final class ClassPathURLStreamHandler extends Handler { final class ClassPathURLStreamHandler extends Handler {
private final String fileUri; private final String fileUri;
private final JarFile jarFile; private final JarFile jarFile;
public ClassPathURLStreamHandler(String jarFileName) throws IOException { ClassPathURLStreamHandler(String jarFileName) throws IOException {
jarFile = new JarFile(jarFileName); jarFile = new JarFile(jarFileName);
fileUri = new File(jarFileName).toURI().toString(); fileUri = new File(jarFileName).toURI().toString();
} }
public URL getEntryUrlOrNull(String entryName) { URL getEntryUrlOrNull(String entryName) {
if (jarFile.getEntry(entryName) != null) { if (jarFile.getEntry(entryName) != null) {
try { try {
String encodedName = ParseUtil.encodePath(entryName, false); String encodedName = ParseUtil.encodePath(entryName, false);
@ -40,7 +40,12 @@ public final class ClassPathURLStreamHandler extends Handler {
return new ClassPathURLConnection(url); return new ClassPathURLConnection(url);
} }
private class ClassPathURLConnection extends JarURLConnection { @Override
protected void finalize() throws Throwable {
jarFile.close();
}
private final class ClassPathURLConnection extends JarURLConnection {
private JarFile connectionJarFile = null; private JarFile connectionJarFile = null;
private ZipEntry jarEntry = null; private ZipEntry jarEntry = null;
private InputStream jarInput = null; private InputStream jarInput = null;
@ -48,6 +53,12 @@ public final class ClassPathURLStreamHandler extends Handler {
private ClassPathURLConnection(URL url) throws MalformedURLException { private ClassPathURLConnection(URL url) throws MalformedURLException {
super(url); super(url);
setUseCaches(false);
}
@Override
public void setUseCaches(boolean usecaches) {
super.setUseCaches(false);
} }
@Override @Override

View File

@ -8,6 +8,7 @@ import android.system.Os;
import android.system.OsConstants; import android.system.OsConstants;
import android.util.Log; import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import java.io.File; import java.io.File;
@ -16,6 +17,7 @@ import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.Channels; import java.nio.channels.Channels;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.List; import java.util.List;
@ -164,6 +166,15 @@ public final class InMemoryDelegateLastClassLoader extends ByteBufferDexClassLoa
return new CompoundEnumeration<>(resources); return new CompoundEnumeration<>(resources);
} }
@NonNull
@Override
public String toString() {
return "LspModuleClassLoader[" +
"module=" + apk + "," +
"nativeLibraryDirs=" + Arrays.toString(nativeLibraryDirs.toArray()) + "," +
super.toString() + "]";
}
public static InMemoryDelegateLastClassLoader loadApk(File apk, public static InMemoryDelegateLastClassLoader loadApk(File apk,
String librarySearchPath, String librarySearchPath,
ClassLoader parent) { ClassLoader parent) {