Fix various unclosed I/O resources (#5766)

This commit is contained in:
Ben Staddon authored and LexManos committed 2019-08-02 13:32:08 -07:00
1 parent 9a636e3c76
commit 3a62514e60
7 files changed
+138 -68

No files matched your search

@@ -35,6 +35,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -439,9 +440,16 @@ public class ForgeModContainer extends DummyModContainer implements WorldAccessC
public void modConstruction(FMLConstructionEvent evt)
{
InputStream is = ForgeModContainer.class.getResourceAsStream("/META-INF/vanilla_annotations.json");
if (is != null)
JsonAnnotationLoader.loadJson(is, null, evt.getASMHarvestedData());
log.debug("Loading Vanilla annotations: " + is);
try
{
if (is != null)
JsonAnnotationLoader.loadJson(is, null, evt.getASMHarvestedData());
log.debug("Loading Vanilla annotations: " + is);
}
finally
{
IOUtils.closeQuietly(is);
}
List<String> all = Lists.newArrayList();
for (ASMData asm : evt.getASMHarvestedData().getAll(ICrashReportDetail.class.getName().replace('.', '/')))
@@ -30,7 +30,6 @@ import java.util.List;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import javax.imageio.ImageIO;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
@@ -43,6 +42,7 @@ import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.texture.DynamicTexture;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.renderer.texture.TextureUtil;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.resources.I18n;
import net.minecraft.client.resources.IResourcePack;
@@ -387,7 +387,7 @@ public class GuiModList extends GuiScreen
{
InputStream logoResource = getClass().getResourceAsStream(logoFile);
if (logoResource != null)
logo = ImageIO.read(logoResource);
logo = TextureUtil.readBufferedImage(logoResource);
}
if (logo != null)
{
@@ -26,6 +26,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
@@ -650,7 +652,10 @@ public class Loader
FMLLog.log.trace("Found a mod state file {}", forcedModFile.getName());
try
{
forcedModListProperties.load(new InputStreamReader(new FileInputStream(forcedModFile), StandardCharsets.UTF_8));
try (Reader reader = new InputStreamReader(new FileInputStream(forcedModFile), StandardCharsets.UTF_8))
{
forcedModListProperties.load(reader);
}
FMLLog.log.trace("Loaded states for {} mods from file", forcedModListProperties.size());
}
catch (Exception e)
@@ -863,7 +868,10 @@ public class Loader
Properties loaded = new Properties();
try
{
loaded.load(getClass().getClassLoader().getResourceAsStream("fmlbranding.properties"));
try (InputStream stream = getClass().getClassLoader().getResourceAsStream("fmlbranding.properties"))
{
loaded.load(stream);
}
}
catch (Exception e)
{
@@ -935,9 +943,15 @@ public class Loader
try
{
Properties props = new Properties();
props.load(new InputStreamReader(new FileInputStream(forcedModFile), StandardCharsets.UTF_8));
try (Reader reader = new InputStreamReader(new FileInputStream(forcedModFile), StandardCharsets.UTF_8))
{
props.load(reader);
}
props.put(modId, "false");
props.store(new OutputStreamWriter(new FileOutputStream(forcedModFile), StandardCharsets.UTF_8), null);
try (Writer writer = new OutputStreamWriter(new FileOutputStream(forcedModFile), StandardCharsets.UTF_8))
{
props.store(writer, null);
}
}
catch (Exception e)
{
@@ -966,7 +980,10 @@ public class Loader
JsonElement injectedDeps;
try
{
injectedDeps = parser.parse(new InputStreamReader(new FileInputStream(injectedDepFile), StandardCharsets.UTF_8));
try (Reader reader = new InputStreamReader(new FileInputStream(injectedDepFile), StandardCharsets.UTF_8))
{
injectedDeps = parser.parse(reader);
}
for (JsonElement el : injectedDeps.getAsJsonArray())
{
JsonObject jo = el.getAsJsonObject();
@@ -48,6 +48,7 @@ import com.google.common.hash.Hashing;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import org.apache.commons.io.IOUtils;
public class ClassPatchManager {
//Must be ABOVE INSTANCE so they get set in time for the constructor.
@@ -166,57 +167,68 @@ public class ClassPatchManager {
public void setup(Side side)
{
Pattern binpatchMatcher = Pattern.compile(String.format("binpatch/%s/.*.binpatch", side.toString().toLowerCase(Locale.ENGLISH)));
JarInputStream jis;
JarInputStream jis = null;
try
{
InputStream binpatchesCompressed = getClass().getResourceAsStream("/binpatches.pack.lzma");
if (binpatchesCompressed==null)
{
if (!FMLLaunchHandler.isDeobfuscatedEnvironment())
{
FMLLog.log.fatal("The binary patch set is missing, things are not going to work!");
}
return;
}
LzmaInputStream binpatchesDecompressed = new LzmaInputStream(binpatchesCompressed);
ByteArrayOutputStream jarBytes = new ByteArrayOutputStream();
JarOutputStream jos = new JarOutputStream(jarBytes);
Pack200.newUnpacker().unpack(binpatchesDecompressed, jos);
jis = new JarInputStream(new ByteArrayInputStream(jarBytes.toByteArray()));
}
catch (Exception e)
{
throw new RuntimeException("Error occurred reading binary patches. Expect severe problems!", e);
}
patches = ArrayListMultimap.create();
do
{
try
{
JarEntry entry = jis.getNextJarEntry();
if (entry == null)
InputStream binpatchesCompressed = getClass().getResourceAsStream("/binpatches.pack.lzma");
if (binpatchesCompressed==null)
{
break;
}
if (binpatchMatcher.matcher(entry.getName()).matches())
{
ClassPatch cp = readPatch(entry, jis);
if (cp != null)
if (!FMLLaunchHandler.isDeobfuscatedEnvironment())
{
patches.put(cp.sourceClassName, cp);
FMLLog.log.fatal("The binary patch set is missing, things are not going to work!");
}
return;
}
try (LzmaInputStream binpatchesDecompressed = new LzmaInputStream(binpatchesCompressed))
{
ByteArrayOutputStream jarBytes = new ByteArrayOutputStream();
try (JarOutputStream jos = new JarOutputStream(jarBytes))
{
Pack200.newUnpacker().unpack(binpatchesDecompressed, jos);
jis = new JarInputStream(new ByteArrayInputStream(jarBytes.toByteArray()));
}
}
else
{
jis.closeEntry();
}
}
catch (IOException e)
catch (Exception e)
{
throw new RuntimeException("Error occurred reading binary patches. Expect severe problems!", e);
}
} while (true);
patches = ArrayListMultimap.create();
do
{
try
{
JarEntry entry = jis.getNextJarEntry();
if (entry == null)
{
break;
}
if (binpatchMatcher.matcher(entry.getName()).matches())
{
ClassPatch cp = readPatch(entry, jis);
if (cp != null)
{
patches.put(cp.sourceClassName, cp);
}
}
else
{
jis.closeEntry();
}
}
catch (IOException e)
{
}
} while (true);
}
finally
{
IOUtils.closeQuietly(jis);
}
FMLLog.log.debug("Read {} binary patches", patches.size());
if (DEBUG)
FMLLog.log.debug("Patch list :\n\t{}", Joiner.on("\t\n").join(patches.asMap().entrySet()));