Fix some resources that are never closed, close in a finally block (#3544)

This commit is contained in:
James Mitchell authored and LexManos committed 2016-12-21 11:13:27 -08:00
1 parent e32cc2a607
commit e9143859c0
13 files changed
+148 -113

No files matched your search

@@ -20,6 +20,7 @@ package net.minecraftforge.fml.common;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@@ -53,6 +54,7 @@ import net.minecraftforge.fml.common.versioning.VersionParser;
import net.minecraftforge.fml.common.versioning.VersionRange;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Level;
@@ -289,7 +291,15 @@ public class FMLModContainer implements ModContainer
if (versionFile != null)
{
version = new Properties();
version.load(source.getInputStream(versionFile));
InputStream sourceInputStream = source.getInputStream(versionFile);
try
{
version.load(sourceInputStream);
}
finally
{
IOUtils.closeQuietly(sourceInputStream);
}
}
source.close();
}
@@ -300,8 +310,14 @@ public class FMLModContainer implements ModContainer
{
version = new Properties();
FileInputStream fis = new FileInputStream(propsFile);
version.load(fis);
fis.close();
try
{
version.load(fis);
}
finally
{
IOUtils.closeQuietly(fis);
}
}
}
return version;
@@ -23,6 +23,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
@@ -59,6 +60,7 @@ import net.minecraftforge.fml.common.versioning.VersionParser;
import net.minecraftforge.fml.relauncher.ModListHelper;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.Level;
import com.google.common.base.CharMatcher;
@@ -213,7 +215,15 @@ public class Loader
}
minecraft = new MinecraftDummyContainer(MC_VERSION);
mcp = new MCPDummyContainer(MetadataCollection.from(getClass().getResourceAsStream("/mcpmod.info"), "MCP").getMetadataForId("mcp", null));
InputStream mcpModInputStream = getClass().getResourceAsStream("/mcpmod.info");
try
{
mcp = new MCPDummyContainer(MetadataCollection.from(mcpModInputStream, "MCP").getMetadataForId("mcp", null));
}
finally
{
IOUtils.closeQuietly(mcpModInputStream);
}
}
/**
@@ -21,6 +21,7 @@ package net.minecraftforge.fml.common.asm;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.security.CodeSource;
import java.security.cert.Certificate;
@@ -28,6 +29,7 @@ import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.Level;
import net.minecraft.launchwrapper.LaunchClassLoader;
@@ -113,7 +115,15 @@ public class FMLSanityChecker implements IFMLCallHook
mcJarFile = new JarFile(mcPath,true);
mcJarFile.getManifest();
JarEntry cbrEntry = mcJarFile.getJarEntry("net/minecraft/client/ClientBrandRetriever.class");
ByteStreams.toByteArray(mcJarFile.getInputStream(cbrEntry));
InputStream mcJarFileInputStream = mcJarFile.getInputStream(cbrEntry);
try
{
ByteStreams.toByteArray(mcJarFileInputStream);
}
finally
{
IOUtils.closeQuietly(mcJarFileInputStream);
}
Certificate[] certificates = cbrEntry.getCertificates();
certCount = certificates != null ? certificates.length : 0;
if (certificates!=null)
@@ -136,17 +146,7 @@ public class FMLSanityChecker implements IFMLCallHook
}
finally
{
if (mcJarFile != null)
{
try
{
mcJarFile.close();
}
catch (IOException ioe)
{
// Noise
}
}
IOUtils.closeQuietly(mcJarFile);
}
}
else
@@ -45,6 +45,7 @@ import java.util.zip.ZipOutputStream;
import net.minecraft.launchwrapper.IClassTransformer;
import net.minecraftforge.fml.relauncher.FMLRelaunchLog;
import org.apache.commons.compress.utils.IOUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.AbstractInsnNode;
@@ -469,27 +470,8 @@ public class AccessTransformer implements IClassTransformer
}
finally
{
if (outJar != null)
{
try
{
outJar.close();
}
catch (IOException e)
{
}
}
if (inJar != null)
{
try
{
inJar.close();
}
catch (IOException e)
{
}
}
IOUtils.closeQuietly(outJar);
IOUtils.closeQuietly(inJar);
}
}
Multimap<String, Modifier> getModifiers()
@@ -35,6 +35,7 @@ import java.util.zip.ZipOutputStream;
import net.minecraft.launchwrapper.IClassTransformer;
import org.apache.commons.compress.utils.IOUtils;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.tree.ClassNode;
@@ -269,27 +270,8 @@ public class MarkerTransformer implements IClassTransformer
}
finally
{
if (outJar != null)
{
try
{
outJar.close();
}
catch (IOException e)
{
}
}
if (inJar != null)
{
try
{
inJar.close();
}
catch (IOException e)
{
}
}
IOUtils.closeQuietly(outJar);
IOUtils.closeQuietly(inJar);
}
}
}
@@ -33,6 +33,7 @@ import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.ModContainerFactory;
import net.minecraftforge.fml.common.discovery.asm.ASMModParser;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.Level;
import com.google.common.base.Throwables;
@@ -73,8 +74,14 @@ public class DirectoryDiscoverer implements ITypeDiscoverer
try
{
FileInputStream fis = new FileInputStream(metadata);
mc = MetadataCollection.from(fis,modDir.getName());
fis.close();
try
{
mc = MetadataCollection.from(fis, modDir.getName());
}
finally
{
IOUtils.closeQuietly(fis);
}
FMLLog.fine("Found an mcmod.info file in directory %s", modDir.getName());
}
catch (Exception e)
@@ -101,11 +108,11 @@ public class DirectoryDiscoverer implements ITypeDiscoverer
if (match.matches())
{
ASMModParser modParser = null;
FileInputStream fis = null;
try
{
FileInputStream fis = new FileInputStream(file);
fis = new FileInputStream(file);
modParser = new ASMModParser(fis);
fis.close();
candidate.addClassEntry(path+file.getName());
}
catch (LoaderException e)
@@ -115,7 +122,11 @@ public class DirectoryDiscoverer implements ITypeDiscoverer
}
catch (Exception e)
{
Throwables.propagate(e);
throw Throwables.propagate(e);
}
finally
{
IOUtils.closeQuietly(fis);
}
modParser.validate();
@@ -19,6 +19,7 @@
package net.minecraftforge.fml.common.discovery;
import java.io.InputStream;
import java.util.Collections;
import java.util.List;
import java.util.jar.JarFile;
@@ -30,6 +31,7 @@ import net.minecraftforge.fml.common.ModContainer;
import net.minecraftforge.fml.common.ModContainerFactory;
import net.minecraftforge.fml.common.discovery.asm.ASMModParser;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.Level;
import java.util.regex.Matcher;
@@ -54,7 +56,15 @@ public class JarDiscoverer implements ITypeDiscoverer
if (modInfo != null)
{
FMLLog.finer("Located mcmod.info file in file %s", candidate.getModContainer().getName());
mc = MetadataCollection.from(jar.getInputStream(modInfo), candidate.getModContainer().getName());
InputStream inputStream = jar.getInputStream(modInfo);
try
{
mc = MetadataCollection.from(inputStream, candidate.getModContainer().getName());
}
finally
{
IOUtils.closeQuietly(inputStream);
}
}
else
{
@@ -73,7 +83,15 @@ public class JarDiscoverer implements ITypeDiscoverer
ASMModParser modParser;
try
{
modParser = new ASMModParser(jar.getInputStream(ze));
InputStream inputStream = jar.getInputStream(ze);
try
{
modParser = new ASMModParser(inputStream);
}
finally
{
IOUtils.closeQuietly(inputStream);
}
candidate.addClassEntry(ze.getName());
}
catch (LoaderException e)
@@ -101,16 +119,7 @@ public class JarDiscoverer implements ITypeDiscoverer
}
finally
{
if (jar != null)
{
try
{
jar.close();
}
catch (Exception e)
{
}
}
IOUtils.closeQuietly(jar);
}
return foundMods;
}
@@ -21,6 +21,7 @@ package net.minecraftforge.fml.common.patcher;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
@@ -30,6 +31,7 @@ import java.util.jar.JarFile;
import net.minecraftforge.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper;
import net.minecraftforge.fml.repackage.com.nothome.delta.Delta;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
@@ -40,6 +42,8 @@ import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
import javax.annotation.Nullable;
public class GenDiffSet {
private static final List<String> RESERVED_NAMES = Arrays.asList("CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9");
@@ -78,8 +82,7 @@ public class GenDiffSet {
String sourceClassName = name.replace('/', '.');
String targetClassName = remapper.map(name).replace('/', '.');
JarEntry entry = sourceZip.getJarEntry(jarName);
byte[] vanillaBytes = entry != null ? ByteStreams.toByteArray(sourceZip.getInputStream(entry)) : new byte[0];
byte[] vanillaBytes = toByteArray(sourceZip, entry);
byte[] patchedBytes = Files.toByteArray(targetFile);
byte[] diff = delta.compute(vanillaBytes, patchedBytes);
@@ -117,4 +120,21 @@ public class GenDiffSet {
sourceZip.close();
}
private static byte[] toByteArray(JarFile sourceZip, @Nullable JarEntry entry) throws IOException
{
if (entry == null)
{
return new byte[0];
}
InputStream sourceZipInputStream = sourceZip.getInputStream(entry);
try
{
return ByteStreams.toByteArray(sourceZipInputStream);
}
finally
{
IOUtils.closeQuietly(sourceZipInputStream);
}
}
}