mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[performance] Speed-up resource loading via classloader:
Avoid eager loading jar resource bytes.
This commit is contained in:
@@ -19,12 +19,11 @@ import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.reference.SoftReference;
|
||||
import com.intellij.util.ArrayUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.util.*;
|
||||
import java.util.jar.Attributes;
|
||||
@@ -131,7 +130,7 @@ class JarLoader extends Loader {
|
||||
try {
|
||||
ZipEntry entry = zipFile.getEntry(name);
|
||||
if (entry != null) {
|
||||
return MemoryResource.load(getBaseURL(), zipFile, entry, myAttributes);
|
||||
return new MyResource(getBaseURL(), entry);
|
||||
}
|
||||
}
|
||||
finally {
|
||||
@@ -144,6 +143,49 @@ class JarLoader extends Loader {
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private class MyResource extends Resource {
|
||||
private final URL myUrl;
|
||||
private final ZipEntry myEntry;
|
||||
|
||||
public MyResource(URL url, ZipEntry entry) throws IOException {
|
||||
myUrl = new URL(url, entry.getName());
|
||||
myEntry = entry;
|
||||
}
|
||||
|
||||
@Override
|
||||
public URL getURL() {
|
||||
return myUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() throws IOException {
|
||||
return new ByteArrayInputStream(getBytes());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes() throws IOException {
|
||||
byte[] result = ArrayUtil.EMPTY_BYTE_ARRAY;
|
||||
try {
|
||||
ZipFile file = getZipFile();
|
||||
try {
|
||||
result = FileUtil.loadBytes(file.getInputStream(myEntry), (int)myEntry.getSize());
|
||||
} finally {
|
||||
releaseZipFile(file);
|
||||
}
|
||||
}
|
||||
catch (Exception e) {
|
||||
error("file: " + myCanonicalFile, e);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getValue(Attribute key) {
|
||||
return myAttributes != null ? myAttributes.get(key) : null;
|
||||
}
|
||||
}
|
||||
|
||||
protected void error(String message, Throwable t) {
|
||||
Logger.getInstance(JarLoader.class).error(message, t);
|
||||
|
||||
Reference in New Issue
Block a user