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

@@ -62,6 +62,7 @@ import net.minecraftforge.fml.client.config.IConfigElement;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.relauncher.FMLInjectionData;
import org.apache.commons.compress.utils.IOUtils;
/**
* This class offers advanced configurations capabilities, allowing to provide
@@ -1055,20 +1056,8 @@ public class Configuration
}
finally
{
if (buffer != null)
{
try
{
buffer.close();
} catch (IOException e){}
}
if (input != null)
{
try
{
input.close();
} catch (IOException e){}
}
IOUtils.closeQuietly(buffer);
IOUtils.closeQuietly(input);
}
resetChangedState();
@@ -21,6 +21,7 @@ package net.minecraftforge.fml.client;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Arrays;
@@ -107,6 +108,7 @@ import net.minecraftforge.fml.common.registry.PersistentRegistryManager;
import net.minecraftforge.fml.common.toposort.ModSortingException;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -297,9 +299,17 @@ public class FMLClientHandler implements IFMLSidedHandler
Class<?> optifineConfig = Class.forName("Config", false, Loader.instance().getModClassLoader());
String optifineVersion = (String) optifineConfig.getField("VERSION").get(null);
Map<String,Object> dummyOptifineMeta = ImmutableMap.<String,Object>builder().put("name", "Optifine").put("version", optifineVersion).build();
ModMetadata optifineMetadata = MetadataCollection.from(getClass().getResourceAsStream("optifinemod.info"),"optifine").getMetadataForId("optifine", dummyOptifineMeta);
optifineContainer = new DummyModContainer(optifineMetadata);
FMLLog.info("Forge Mod Loader has detected optifine %s, enabling compatibility features",optifineContainer.getVersion());
InputStream optifineModInfoInputStream = getClass().getResourceAsStream("optifinemod.info");
try
{
ModMetadata optifineMetadata = MetadataCollection.from(optifineModInfoInputStream, "optifine").getMetadataForId("optifine", dummyOptifineMeta);
optifineContainer = new DummyModContainer(optifineMetadata);
FMLLog.info("Forge Mod Loader has detected optifine %s, enabling compatibility features", optifineContainer.getVersion());
}
finally
{
IOUtils.closeQuietly(optifineModInfoInputStream);
}
}
catch (Exception e)
{
@@ -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);
}
}
}
@@ -24,6 +24,7 @@ import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
@@ -55,6 +56,7 @@ import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.Name;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.SortingIndex;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin.TransformerExclusions;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.logging.log4j.Level;
import com.google.common.base.Strings;
@@ -464,9 +466,19 @@ public class CoreModManager {
try
{
Files.createParentDirs(target);
FileOutputStream targ = new FileOutputStream(target);
ByteStreams.copy(jar.getInputStream(jarEntry), targ);
targ.close();
FileOutputStream targetOutputStream = null;
InputStream jarInputStream = null;
try
{
targetOutputStream = new FileOutputStream(target);
jarInputStream = jar.getInputStream(jarEntry);
ByteStreams.copy(jarInputStream, targetOutputStream);
}
finally
{
IOUtils.closeQuietly(targetOutputStream);
IOUtils.closeQuietly(jarInputStream);
}
FMLRelaunchLog.log(Level.DEBUG, "Extracted ContainedDep %s from %s to %s", dep, jar.getName(), target.getCanonicalPath());
result.put(dep,target);
} catch (IOException e)
@@ -19,6 +19,8 @@
package net.minecraftforge.fml.repackage.com.nothome.delta;
import org.apache.commons.compress.utils.IOUtils;
import static net.minecraftforge.fml.repackage.com.nothome.delta.GDiffWriter.COPY_INT_INT;
import static net.minecraftforge.fml.repackage.com.nothome.delta.GDiffWriter.COPY_INT_UBYTE;
import static net.minecraftforge.fml.repackage.com.nothome.delta.GDiffWriter.COPY_INT_USHORT;
@@ -70,16 +72,16 @@ public class GDiffPatcher {
throws IOException
{
RandomAccessFileSeekableSource source =new RandomAccessFileSeekableSource(new RandomAccessFile(sourceFile, "r"));
InputStream patch = new FileInputStream(patchFile);
OutputStream output = new FileOutputStream(outputFile);
InputStream patch = null;
OutputStream output = null;
try {
patch = new FileInputStream(patchFile);
output = new FileOutputStream(outputFile);
patch(source, patch, output);
} catch (IOException e) {
throw e;
} finally {
source.close();
patch.close();
output.close();
IOUtils.closeQuietly(source);
IOUtils.closeQuietly(patch);
IOUtils.closeQuietly(output);
}
}
@@ -260,15 +260,7 @@ public class FMLServerHandler implements IFMLSidedHandler
finally
{
IOUtils.closeQuietly(stream);
try
{
if (zip != null)
zip.close();
}
catch (IOException e)
{
// shush
}
IOUtils.closeQuietly(zip);
}
}