Replaced ClassLoader.getResource() + URL.openStream() with ClassLoader.getResourceAsStream()

This optimization allows to reuse UrlClassLoader's capability to load resources faster: e.g. using existing ZipFile handles or preloaded contents

GitOrigin-RevId: 315b251b7fcb5b600b0626d694f0d7e761ee973f
This commit is contained in:
Maxim.Mossienko
2019-05-29 11:09:00 +03:00
committed by intellij-monorepo-bot
parent 4c9fb78cb0
commit 1a0362acf4
8 changed files with 61 additions and 43 deletions
@@ -24,14 +24,12 @@ import com.intellij.util.xmlb.annotations.Property;
import gnu.trove.THashSet;
import gnu.trove.TObjectHashingStrategy;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.*;
import javax.swing.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
@@ -399,11 +397,11 @@ public abstract class InspectionProfileEntry implements BatchSuppressableTool {
return null;
}
@Deprecated
@ApiStatus.ScheduledForRemoval(inVersion = "2020.1")
@Nullable
protected URL getDescriptionUrl() {
final String fileName = getDescriptionFileName();
if (fileName == null) return null;
return ResourceUtil.getResource(getDescriptionContextClass(), "/inspectionDescriptions", fileName);
return null;
}
@NotNull
@@ -429,9 +427,12 @@ public abstract class InspectionProfileEntry implements BatchSuppressableTool {
if (description != null) return description;
try {
URL descriptionUrl = getDescriptionUrl();
if (descriptionUrl == null) return null;
return ResourceUtil.loadText(descriptionUrl);
InputStream descriptionStream = null;
final String fileName = getDescriptionFileName();
if (fileName != null) {
descriptionStream = ResourceUtil.getResourceAsStream(getDescriptionContextClass(), "/inspectionDescriptions", fileName);
}
return descriptionStream != null ? ResourceUtil.loadText(descriptionStream) : null;
}
catch (IOException ignored) {
}
@@ -17,7 +17,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URL;
import java.io.InputStream;
/**
* @author Dmitry Avdeev
@@ -161,28 +161,23 @@ public abstract class InspectionToolWrapper<T extends InspectionProfileEntry, E
final String description = getStaticDescription();
if (description != null) return description;
try {
URL descriptionUrl = getDescriptionUrl();
if (descriptionUrl == null) return null;
return ResourceUtil.loadText(descriptionUrl);
InputStream descriptionStream = getDescriptionStream();
return descriptionStream != null ? ResourceUtil.loadText(descriptionStream) : null;
}
catch (IOException ignored) { }
return getTool().loadDescription();
}
private URL getDescriptionUrl() {
private InputStream getDescriptionStream() {
Application app = ApplicationManager.getApplication();
if (myEP == null || app.isUnitTestMode() || app.isHeadlessEnvironment()) {
return superGetDescriptionUrl();
}
String fileName = getDescriptionFileName();
return myEP.getLoaderForClass().getResource("inspectionDescriptions/" + fileName);
}
@Nullable
private URL superGetDescriptionUrl() {
final String fileName = getDescriptionFileName();
return ResourceUtil.getResource(getDescriptionContextClass(), "inspectionDescriptions", fileName);
if (myEP == null || app.isUnitTestMode() || app.isHeadlessEnvironment()) {
return ResourceUtil.getResourceAsStream(getDescriptionContextClass().getClassLoader(), "inspectionDescriptions", fileName);
}
return myEP.getLoaderForClass().getResourceAsStream("inspectionDescriptions/" + fileName);
}
@NotNull
@@ -21,7 +21,7 @@ import org.jetbrains.annotations.NotNull;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;
@@ -59,11 +59,11 @@ public class DumpInspectionDescriptionsAction extends AnAction implements DumbAw
if (names == null) groups.put(group, (names = ContainerUtil.newTreeSet()));
names.add(toolWrapper.getShortName());
final URL url = getDescriptionUrl(toolWrapper);
if (url != null) {
final InputStream stream = getDescriptionStream(toolWrapper);
if (stream != null) {
doDump(new File(descDirectory, toolWrapper.getShortName() + ".html"), new Processor() {
@Override public void process(BufferedWriter writer) throws Exception {
writer.write(ResourceUtil.loadText(url));
writer.write(ResourceUtil.loadText(stream));
}
});
}
@@ -112,9 +112,9 @@ public class DumpInspectionDescriptionsAction extends AnAction implements DumbAw
return StringUtil.isEmptyOrSpaces(name) ? "General" : name;
}
private static URL getDescriptionUrl(final InspectionToolWrapper toolWrapper) {
private static InputStream getDescriptionStream(final InspectionToolWrapper toolWrapper) {
final Class aClass = getInspectionClass(toolWrapper);
return ResourceUtil.getResource(aClass, "/inspectionDescriptions", toolWrapper.getShortName() + ".html");
return ResourceUtil.getResourceAsStream(aClass, "/inspectionDescriptions", toolWrapper.getShortName() + ".html");
}
private interface Processor {
@@ -34,7 +34,7 @@ internal class SearchableOptionIndexLoader(val registrar: SearchableOptionsRegis
}
private fun loadSynonyms() {
val root = JDOMUtil.load(ResourceUtil.getResource(SearchableOptionsRegistrar::class.java, "/search/", "synonyms.xml"))
val root = JDOMUtil.load(ResourceUtil.getResourceAsStream(SearchableOptionsRegistrar::class.java, "/search/", "synonyms.xml"))
for (configurable in root.getChildren("configurable")) {
val id = configurable.getAttributeValue("id") ?: continue
val groupName = configurable.getAttributeValue("configurable_name")
@@ -34,6 +34,7 @@ import org.jetbrains.annotations.Nullable;
import javax.swing.event.DocumentEvent;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.*;
@@ -76,10 +77,10 @@ public class SearchableOptionsRegistrarImpl extends SearchableOptionsRegistrar {
ApplicationManager.getApplication().isUnitTestMode()) return;
try {
//stop words
URL url = ResourceUtil.getResource(SearchableOptionsRegistrarImpl.class, "/search/", "ignore.txt");
if (url == null) throw new IOException("Broken installation: IDE does not provide /search/ignore.txt");
InputStream stream = ResourceUtil.getResourceAsStream(SearchableOptionsRegistrarImpl.class, "/search/", "ignore.txt");
if (stream == null) throw new IOException("Broken installation: IDE does not provide /search/ignore.txt");
String text = ResourceUtil.loadText(url);
String text = ResourceUtil.loadText(stream);
final String[] stopWords = text.split("[\\W]");
ContainerUtil.addAll(myStopWords, stopWords);
}
@@ -30,7 +30,6 @@ import com.intellij.util.SVGLoader;
import com.intellij.util.io.IOUtil;
import com.intellij.util.ui.ImageUtil;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.JBUIScale;
import com.intellij.util.ui.JBUIScale.ScaleContext;
import com.intellij.util.ui.UIUtil;
import com.twelvemonkeys.imageio.stream.ByteArrayImageInputStream;
@@ -62,7 +61,6 @@ import java.awt.image.BufferedImage;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static com.intellij.util.ui.UIUtil.drawImage;
@@ -118,14 +116,14 @@ public class TipUIUtil {
ClassLoader tipLoader = pluginDescriptor == null ? TipUIUtil.class.getClassLoader() :
ObjectUtils.notNull(pluginDescriptor.getPluginClassLoader(), TipUIUtil.class.getClassLoader());
URL url = ResourceUtil.getResource(tipLoader, "/tips/", tip.fileName);
if (url == null) {
InputStream tipStream = ResourceUtil.getResourceAsStream(tipLoader, "/tips/", tip.fileName);
if (tipStream == null) {
return getCantReadText(tip);
}
text.append(ResourceUtil.loadText(url));
text.append(ResourceUtil.loadText(tipStream));
updateImages(text, tipLoader, "", component);
URL cssResource = ResourceUtil.getResource(tipLoader, "/tips/", isUnderDarcula() ? "css/tips_darcula.css" : "css/tips.css");
cssText = cssResource != null ? new String(readBytes(cssResource), StandardCharsets.UTF_8) : "";
InputStream cssResourceStream = ResourceUtil.getResourceAsStream(tipLoader, "/tips/", isUnderDarcula() ? "css/tips_darcula.css" : "css/tips.css");
cssText = cssResourceStream != null ? ResourceUtil.loadText(cssResourceStream) : "";
}
updateShortcuts(text);
@@ -28,6 +28,24 @@ public class ResourceUtil {
return getResource(loaderClass.getClassLoader(), basePath, fileName);
}
public static InputStream getResourceAsStream(@NotNull Class loaderClass, @NonNls @NotNull String basePath, @NonNls @NotNull String fileName) {
return getResourceAsStream(loaderClass.getClassLoader(), basePath, fileName);
}
public static InputStream getResourceAsStream(@NotNull ClassLoader loader, @NonNls @NotNull String basePath, @NonNls @NotNull String fileName) {
String fixedPath = StringUtil.trimStart(StringUtil.trimEnd(basePath, "/"), "/");
List<String> bundles = calculateBundleNames(fixedPath, Locale.getDefault());
for (String bundle : bundles) {
InputStream stream = loader.getResourceAsStream(bundle + "/" + fileName);
if (stream == null) continue;
return stream;
}
return loader.getResourceAsStream(fixedPath + "/" + fileName);
}
public static URL getResource(@NotNull ClassLoader loader, @NonNls @NotNull String basePath, @NonNls @NotNull String fileName) {
String fixedPath = StringUtil.trimStart(StringUtil.trimEnd(basePath, "/"), "/");
@@ -98,7 +116,12 @@ public class ResourceUtil {
@NotNull
public static String loadText(@NotNull URL url) throws IOException {
InputStream inputStream = new BufferedInputStream(URLUtil.openStream(url));
return loadText(URLUtil.openStream(url));
}
@NotNull
public static String loadText(@NotNull InputStream in) throws IOException {
InputStream inputStream = in instanceof BufferedInputStream ? in : new BufferedInputStream(in);
try (InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
StringBuilder text = new StringBuilder();
@@ -46,7 +46,7 @@ public abstract class MavenBuildToolLogTestUtils extends UsefulTestCase {
@NotNull
protected static String[] fromFile(String resource) throws IOException {
try (InputStream stream = ResourceUtil.getResource(MavenBuildToolLogTestUtils.class, "", resource).openStream();
try (InputStream stream = ResourceUtil.getResourceAsStream(MavenBuildToolLogTestUtils.class, "", resource);
Scanner scanner = new Scanner(stream)) {
List<String> result = new ArrayList<>();
while (scanner.hasNextLine()) {