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

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

View File

@@ -30,6 +30,7 @@ import com.google.gson.JsonSyntaxException;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
@@ -134,13 +135,12 @@ public class ExternalPluginClient
private static Certificate loadCertificate()
{
try
try (InputStream in = ExternalPluginClient.class.getResourceAsStream("externalplugins.crt"))
{
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
Certificate certificate = certFactory.generateCertificate(ExternalPluginClient.class.getResourceAsStream("externalplugins.crt"));
return certificate;
return certFactory.generateCertificate(in);
}
catch (CertificateException e)
catch (CertificateException | IOException 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.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
@@ -49,12 +50,19 @@ public class ItemVariationMapping
static
{
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 = gson.fromJson(new InputStreamReader(geLimitData, StandardCharsets.UTF_8), typeToken.getType());
final Map<String, Collection<Integer>> itemVariations;
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<>();
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.List;
import java.util.function.Function;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class Template
{
private final List<Function<String, String>> resourceLoaders = new ArrayList<>();
@@ -80,10 +82,16 @@ public class Template
{
return add(f ->
{
InputStream is = clazz.getResourceAsStream(f);
if (is != null)
try (InputStream is = clazz.getResourceAsStream(f))
{
return inputStreamToString(is);
if (is != null)
{
return inputStreamToString(is);
}
}
catch (IOException ex)
{
log.warn(null, ex);
}
return null;
});

View File

@@ -28,6 +28,7 @@ import com.google.inject.Inject;
import com.google.inject.Provides;
import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import net.runelite.api.Client;
import net.runelite.api.Constants;
import net.runelite.api.GameState;
@@ -59,7 +60,10 @@ public class SkyboxPlugin extends Plugin
@Override
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

View File

@@ -559,13 +559,13 @@ public class ClientLoader implements Supplier<Applet>
private static Certificate[] getJagexCertificateChain()
{
try
try (InputStream in = ClientLoader.class.getResourceAsStream("jagex.crt"))
{
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]);
}
catch (CertificateException e)
catch (CertificateException | IOException 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.GraphicsEnvironment;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.text.StyleContext;
import lombok.Getter;
@@ -48,10 +49,12 @@ public class FontManager
{
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,
FontManager.class.getResourceAsStream("runescape.ttf"))
// runescape
Font font = Font.createFont(Font.TRUETYPE_FONT, inRunescape)
.deriveFont(Font.PLAIN, 16);
ge.registerFont(font);
@@ -59,8 +62,8 @@ public class FontManager
.getFont(font.getName(), Font.PLAIN, 16);
ge.registerFont(runescapeFont);
Font smallFont = Font.createFont(Font.TRUETYPE_FONT,
FontManager.class.getResourceAsStream("runescape_small.ttf"))
// small
Font smallFont = Font.createFont(Font.TRUETYPE_FONT, inRunescapeSmall)
.deriveFont(Font.PLAIN, 16);
ge.registerFont(smallFont);
@@ -68,8 +71,8 @@ public class FontManager
.getFont(smallFont.getName(), Font.PLAIN, 16);
ge.registerFont(runescapeSmallFont);
Font boldFont = Font.createFont(Font.TRUETYPE_FONT,
FontManager.class.getResourceAsStream("runescape_bold.ttf"))
// bold
Font boldFont = Font.createFont(Font.TRUETYPE_FONT, inRunescapeBold)
.deriveFont(Font.BOLD, 16);
ge.registerFont(boldFont);

View File

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

View File

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