Close various resource inputstreams

This commit is contained in:
Adam
2021-07-04 15:14:05 -04:00
parent 6b11afd907
commit 5d420ae57b
12 changed files with 76 additions and 44 deletions

View File

@@ -38,9 +38,8 @@ public class OverlayIndex
static static
{ {
InputStream indexStream = OverlayIndex.class.getResourceAsStream("/runelite/index"); try (InputStream indexStream = OverlayIndex.class.getResourceAsStream("/runelite/index");
DataInputStream in = new DataInputStream(indexStream))
try (DataInputStream in = new DataInputStream(indexStream))
{ {
int id; int id;
while ((id = in.readInt()) != -1) while ((id = in.readInt()) != -1)

View File

@@ -479,14 +479,11 @@ public class Notifier
{ {
if (NOTIFICATION_FILE.exists()) if (NOTIFICATION_FILE.exists())
{ {
try try (InputStream fileStream = new BufferedInputStream(new FileInputStream(NOTIFICATION_FILE));
AudioInputStream sound = AudioSystem.getAudioInputStream(fileStream))
{ {
InputStream fileStream = new BufferedInputStream(new FileInputStream(NOTIFICATION_FILE)); clip.open(sound);
try (AudioInputStream sound = AudioSystem.getAudioInputStream(fileStream)) return true;
{
clip.open(sound);
return true;
}
} }
catch (UnsupportedAudioFileException | IOException | LineUnavailableException e) catch (UnsupportedAudioFileException | IOException | LineUnavailableException e)
{ {
@@ -495,8 +492,8 @@ public class Notifier
} }
// Otherwise load from the classpath // Otherwise load from the classpath
InputStream fileStream = new BufferedInputStream(Notifier.class.getResourceAsStream("notification.wav")); try (InputStream fileStream = new BufferedInputStream(Notifier.class.getResourceAsStream("notification.wav"));
try (AudioInputStream sound = AudioSystem.getAudioInputStream(fileStream)) AudioInputStream sound = AudioSystem.getAudioInputStream(fileStream))
{ {
clip.open(sound); clip.open(sound);
return true; return true;

View File

@@ -30,6 +30,7 @@ import com.google.gson.JsonSyntaxException;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException; import java.security.InvalidKeyException;
@@ -134,13 +135,12 @@ public class ExternalPluginClient
private static Certificate loadCertificate() private static Certificate loadCertificate()
{ {
try try (InputStream in = ExternalPluginClient.class.getResourceAsStream("externalplugins.crt"))
{ {
CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate certificate = certFactory.generateCertificate(ExternalPluginClient.class.getResourceAsStream("externalplugins.crt")); return certFactory.generateCertificate(in);
return certificate;
} }
catch (CertificateException e) catch (CertificateException | IOException e)
{ {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap; import com.google.common.collect.Multimap;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@@ -49,12 +50,19 @@ public class ItemVariationMapping
static static
{ {
final Gson gson = new Gson(); final Gson gson = new Gson();
final TypeToken<Map<String, Collection<Integer>>> typeToken = new TypeToken<Map<String, Collection<Integer>>>() // CHECKSTYLE:OFF
{ final TypeToken<Map<String, Collection<Integer>>> typeToken = new TypeToken<Map<String, Collection<Integer>>>(){};
}; // CHECKSTYLE:ON
final InputStream geLimitData = ItemVariationMapping.class.getResourceAsStream("/item_variations.json"); final Map<String, Collection<Integer>> itemVariations;
final Map<String, Collection<Integer>> itemVariations = gson.fromJson(new InputStreamReader(geLimitData, StandardCharsets.UTF_8), typeToken.getType()); try (InputStream geLimitData = ItemVariationMapping.class.getResourceAsStream("/item_variations.json"))
{
itemVariations = gson.fromJson(new InputStreamReader(geLimitData, StandardCharsets.UTF_8), typeToken.getType());
}
catch (IOException e)
{
throw new RuntimeException(e);
}
ImmutableMap.Builder<Integer, Integer> builder = new ImmutableMap.Builder<>(); ImmutableMap.Builder<Integer, Integer> builder = new ImmutableMap.Builder<>();
ImmutableMultimap.Builder<Integer, Integer> invertedBuilder = new ImmutableMultimap.Builder<>(); ImmutableMultimap.Builder<Integer, Integer> invertedBuilder = new ImmutableMultimap.Builder<>();

View File

@@ -32,7 +32,9 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Function; import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Template public class Template
{ {
private final List<Function<String, String>> resourceLoaders = new ArrayList<>(); private final List<Function<String, String>> resourceLoaders = new ArrayList<>();
@@ -80,10 +82,16 @@ public class Template
{ {
return add(f -> return add(f ->
{ {
InputStream is = clazz.getResourceAsStream(f); try (InputStream is = clazz.getResourceAsStream(f))
if (is != null)
{ {
return inputStreamToString(is); if (is != null)
{
return inputStreamToString(is);
}
}
catch (IOException ex)
{
log.warn(null, ex);
} }
return null; return null;
}); });

View File

@@ -28,6 +28,7 @@ import com.google.inject.Inject;
import com.google.inject.Provides; import com.google.inject.Provides;
import java.awt.Color; import java.awt.Color;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import net.runelite.api.Client; import net.runelite.api.Client;
import net.runelite.api.Constants; import net.runelite.api.Constants;
import net.runelite.api.GameState; import net.runelite.api.GameState;
@@ -59,7 +60,10 @@ public class SkyboxPlugin extends Plugin
@Override @Override
public void startUp() throws IOException public void startUp() throws IOException
{ {
skybox = new Skybox(SkyboxPlugin.class.getResourceAsStream("skybox.txt"), "skybox.txt"); try (InputStream in = SkyboxPlugin.class.getResourceAsStream("skybox.txt"))
{
skybox = new Skybox(in, "skybox.txt");
}
} }
@Override @Override

View File

@@ -559,13 +559,13 @@ public class ClientLoader implements Supplier<Applet>
private static Certificate[] getJagexCertificateChain() private static Certificate[] getJagexCertificateChain()
{ {
try try (InputStream in = ClientLoader.class.getResourceAsStream("jagex.crt"))
{ {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(ClientLoader.class.getResourceAsStream("jagex.crt")); Collection<? extends Certificate> certificates = certificateFactory.generateCertificates(in);
return certificates.toArray(new Certificate[0]); return certificates.toArray(new Certificate[0]);
} }
catch (CertificateException e) catch (CertificateException | IOException e)
{ {
throw new RuntimeException("Unable to parse pinned certificates", e); throw new RuntimeException("Unable to parse pinned certificates", e);
} }

View File

@@ -28,6 +28,7 @@ import java.awt.Font;
import java.awt.FontFormatException; import java.awt.FontFormatException;
import java.awt.GraphicsEnvironment; import java.awt.GraphicsEnvironment;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import javax.swing.text.StyleContext; import javax.swing.text.StyleContext;
import lombok.Getter; import lombok.Getter;
@@ -48,10 +49,12 @@ public class FontManager
{ {
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
try try (InputStream inRunescape = FontManager.class.getResourceAsStream("runescape.ttf");
InputStream inRunescapeSmall = FontManager.class.getResourceAsStream("runescape_small.ttf");
InputStream inRunescapeBold = FontManager.class.getResourceAsStream("runescape_bold.ttf"))
{ {
Font font = Font.createFont(Font.TRUETYPE_FONT, // runescape
FontManager.class.getResourceAsStream("runescape.ttf")) Font font = Font.createFont(Font.TRUETYPE_FONT, inRunescape)
.deriveFont(Font.PLAIN, 16); .deriveFont(Font.PLAIN, 16);
ge.registerFont(font); ge.registerFont(font);
@@ -59,8 +62,8 @@ public class FontManager
.getFont(font.getName(), Font.PLAIN, 16); .getFont(font.getName(), Font.PLAIN, 16);
ge.registerFont(runescapeFont); ge.registerFont(runescapeFont);
Font smallFont = Font.createFont(Font.TRUETYPE_FONT, // small
FontManager.class.getResourceAsStream("runescape_small.ttf")) Font smallFont = Font.createFont(Font.TRUETYPE_FONT, inRunescapeSmall)
.deriveFont(Font.PLAIN, 16); .deriveFont(Font.PLAIN, 16);
ge.registerFont(smallFont); ge.registerFont(smallFont);
@@ -68,8 +71,8 @@ public class FontManager
.getFont(smallFont.getName(), Font.PLAIN, 16); .getFont(smallFont.getName(), Font.PLAIN, 16);
ge.registerFont(runescapeSmallFont); ge.registerFont(runescapeSmallFont);
Font boldFont = Font.createFont(Font.TRUETYPE_FONT, // bold
FontManager.class.getResourceAsStream("runescape_bold.ttf")) Font boldFont = Font.createFont(Font.TRUETYPE_FONT, inRunescapeBold)
.deriveFont(Font.BOLD, 16); .deriveFont(Font.BOLD, 16);
ge.registerFont(boldFont); ge.registerFont(boldFont);

View File

@@ -35,6 +35,7 @@ import java.awt.image.DirectColorModel;
import java.awt.image.PixelGrabber; import java.awt.image.PixelGrabber;
import java.awt.image.RescaleOp; import java.awt.image.RescaleOp;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -382,11 +383,11 @@ public class ImageUtil
*/ */
public static BufferedImage loadImageResource(final Class<?> c, final String path) public static BufferedImage loadImageResource(final Class<?> c, final String path)
{ {
try try (InputStream in = c.getResourceAsStream(path))
{ {
synchronized (ImageIO.class) synchronized (ImageIO.class)
{ {
return ImageIO.read(c.getResourceAsStream(path)); return ImageIO.read(in);
} }
} }
catch (IllegalArgumentException e) catch (IllegalArgumentException e)

View File

@@ -27,6 +27,7 @@ package net.runelite.client.util;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodHandles;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
@@ -104,10 +105,10 @@ public class ReflectUtil
*/ */
public static void installLookupHelper(PrivateLookupableClassLoader cl) public static void installLookupHelper(PrivateLookupableClassLoader cl)
{ {
try String name = PrivateLookupHelper.class.getName();
try (InputStream in = ReflectUtil.class.getResourceAsStream("/" + name.replace('.', '/') + ".class"))
{ {
String name = PrivateLookupHelper.class.getName(); byte[] classData = ByteStreams.toByteArray(in);
byte[] classData = ByteStreams.toByteArray(ReflectUtil.class.getResourceAsStream("/" + name.replace('.', '/') + ".class"));
Class<?> clazz = cl.defineClass0(name, classData, 0, classData.length); Class<?> clazz = cl.defineClass0(name, classData, 0, classData.length);
// force <clinit> to run // force <clinit> to run

View File

@@ -29,6 +29,7 @@ import com.google.common.io.CharSource;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.Assert; import org.junit.Assert;
@@ -50,8 +51,13 @@ public class SkyboxTest
@Test @Test
public void testLoadActual() throws IOException public void testLoadActual() throws IOException
{ {
long start = System.nanoTime(); long start;
Skybox skybox = new Skybox(SkyboxPlugin.class.getResourceAsStream("skybox.txt"), "skybox.txt"); Skybox skybox;
try (InputStream in = SkyboxPlugin.class.getResourceAsStream("skybox.txt"))
{
start = System.nanoTime();
skybox = new Skybox(in, "skybox.txt");
}
log.info("Parse took {}ms", (System.nanoTime() - start) / 1_000_000); log.info("Parse took {}ms", (System.nanoTime() - start) / 1_000_000);
String skyboxFile = System.getProperty("skyboxExport"); String skyboxFile = System.getProperty("skyboxExport");

View File

@@ -25,19 +25,24 @@
*/ */
package net.runelite.client.plugins.worldmap; package net.runelite.client.plugins.worldmap;
import java.io.IOException;
import java.io.InputStream;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import org.junit.Test; import org.junit.Test;
public class TeleportLocationDataTest public class TeleportLocationDataTest
{ {
@Test @Test
public void testResources() public void testResources() throws IOException
{ {
for (TeleportLocationData data : TeleportLocationData.values()) for (TeleportLocationData data : TeleportLocationData.values())
{ {
String path = data.getIconPath(); String path = data.getIconPath();
assertNotNull(path); assertNotNull(path);
assertNotNull(path, getClass().getResourceAsStream(path)); try (InputStream in = getClass().getResourceAsStream(path))
{
assertNotNull(in);
}
} }
} }
} }