From 941c3eff024078b576716048d9113c1b2fff8d13 Mon Sep 17 00:00:00 2001 From: James Mitchell Date: Wed, 11 Jan 2017 15:18:33 -0800 Subject: [PATCH] Fix #3596 Close jar sources safely in Java 6 (#3597) --- .../common/util/Java6Utils.java | 49 +++++++++++++++++++ .../fml/common/asm/FMLSanityChecker.java | 4 +- .../fml/common/discovery/JarDiscoverer.java | 3 +- .../fml/server/FMLServerHandler.java | 3 +- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 src/main/java/net/minecraftforge/common/util/Java6Utils.java diff --git a/src/main/java/net/minecraftforge/common/util/Java6Utils.java b/src/main/java/net/minecraftforge/common/util/Java6Utils.java new file mode 100644 index 000000000..4038052d4 --- /dev/null +++ b/src/main/java/net/minecraftforge/common/util/Java6Utils.java @@ -0,0 +1,49 @@ +/* + * Minecraft Forge + * Copyright (c) 2016. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation version 2.1 + * of the License. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +package net.minecraftforge.common.util; + +import org.apache.commons.io.IOUtils; + +import javax.annotation.Nullable; +import java.io.Closeable; +import java.io.IOException; +import java.util.zip.ZipFile; + +public class Java6Utils +{ + /** + * {@link ZipFile} does not implement {@link Closeable} on Java 6. + * This method is the same as {@link IOUtils#closeQuietly(Closeable)} but works on {@link ZipFile} on Java 6. + */ + public static void closeZipQuietly(@Nullable ZipFile file) + { + try + { + if (file != null) + { + file.close(); + } + } + catch (IOException ioe) + { + // ignore + } + } +} diff --git a/src/main/java/net/minecraftforge/fml/common/asm/FMLSanityChecker.java b/src/main/java/net/minecraftforge/fml/common/asm/FMLSanityChecker.java index 4464f7d7b..0283e30d3 100644 --- a/src/main/java/net/minecraftforge/fml/common/asm/FMLSanityChecker.java +++ b/src/main/java/net/minecraftforge/fml/common/asm/FMLSanityChecker.java @@ -20,7 +20,6 @@ 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; @@ -29,6 +28,7 @@ import java.util.Map; import java.util.jar.JarEntry; import java.util.jar.JarFile; +import net.minecraftforge.common.util.Java6Utils; import org.apache.commons.io.IOUtils; import org.apache.logging.log4j.Level; @@ -146,7 +146,7 @@ public class FMLSanityChecker implements IFMLCallHook } finally { - IOUtils.closeQuietly(mcJarFile); + Java6Utils.closeZipQuietly(mcJarFile); } } else diff --git a/src/main/java/net/minecraftforge/fml/common/discovery/JarDiscoverer.java b/src/main/java/net/minecraftforge/fml/common/discovery/JarDiscoverer.java index 396198e6d..c6bed3693 100644 --- a/src/main/java/net/minecraftforge/fml/common/discovery/JarDiscoverer.java +++ b/src/main/java/net/minecraftforge/fml/common/discovery/JarDiscoverer.java @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.List; import java.util.jar.JarFile; +import net.minecraftforge.common.util.Java6Utils; import net.minecraftforge.fml.common.FMLLog; import net.minecraftforge.fml.common.LoaderException; import net.minecraftforge.fml.common.MetadataCollection; @@ -119,7 +120,7 @@ public class JarDiscoverer implements ITypeDiscoverer } finally { - IOUtils.closeQuietly(jar); + Java6Utils.closeZipQuietly(jar); } return foundMods; } diff --git a/src/main/java/net/minecraftforge/fml/server/FMLServerHandler.java b/src/main/java/net/minecraftforge/fml/server/FMLServerHandler.java index 05c00ea30..c2fb09188 100644 --- a/src/main/java/net/minecraftforge/fml/server/FMLServerHandler.java +++ b/src/main/java/net/minecraftforge/fml/server/FMLServerHandler.java @@ -36,6 +36,7 @@ import net.minecraft.util.IThreadListener; import net.minecraft.util.text.translation.LanguageMap; import net.minecraft.world.storage.SaveFormatOld; import net.minecraftforge.common.util.CompoundDataFixer; +import net.minecraftforge.common.util.Java6Utils; import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.FMLLog; import net.minecraftforge.fml.common.IFMLSidedHandler; @@ -260,7 +261,7 @@ public class FMLServerHandler implements IFMLSidedHandler finally { IOUtils.closeQuietly(stream); - IOUtils.closeQuietly(zip); + Java6Utils.closeZipQuietly(zip); } }