Freak not out when loading images from input streams in a sandbox.

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@333 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Michael Bayne
2007-11-08 02:36:17 +00:00
parent ffd07764ee
commit cba2fc4f67
@@ -45,7 +45,10 @@ import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.imageio.stream.MemoryCacheImageInputStream;
import com.samskivert.io.StreamUtil;
import com.samskivert.net.PathUtil; import com.samskivert.net.PathUtil;
import com.samskivert.util.ResultListener; import com.samskivert.util.ResultListener;
import com.samskivert.util.StringUtil; import com.samskivert.util.StringUtil;
@@ -678,21 +681,34 @@ public class ResourceManager
protected static BufferedImage loadImage (InputStream iis) protected static BufferedImage loadImage (InputStream iis)
throws IOException throws IOException
{ {
BufferedImage image = ImageIO.read(iis); BufferedImage image;
try {
iis.close(); if (iis instanceof ImageInputStream) {
} catch (IOException ioe) { image = ImageIO.read(iis);
// jesus fucking hoppalong cassidy christ on a polyester pogo stick!
// ImageInputStreamImpl.close() throws a fucking IOException if it's already closed; } else {
// there's no way to find out if it's already closed or not, so we have to check for // if we don't already have an image input stream, create a memory cache image input
// their bullshit exception; as if we should just "trust" ImageIO.read() to close the // stream to avoid causing freakout if we're used in a sandbox because ImageIO
// fucking input stream when it's done, especially after the goddamned fiasco with // otherwise use FileCacheImageInputStream which tries to create a temp file
// PNGImageReader not closing its fucking inflaters; for the love of humanity MemoryCacheImageInputStream mciis = new MemoryCacheImageInputStream(iis);
if (!"closed".equals(ioe.getMessage())) { image = ImageIO.read(mciis);
Log.warning("Failure closing image input '" + iis + "'."); try {
Log.logStackTrace(ioe); // this doesn't close the underlying stream
mciis.close();
} catch (IOException ioe) {
// ImageInputStreamImpl.close() throws an IOException if it's already closed;
// there's no way to find out if it's already closed or not, so we have to check
// the exception message to determine if this is actually warning worthy
if (!"closed".equals(ioe.getMessage())) {
Log.warning("Failure closing image input '" + iis + "'.");
Log.logStackTrace(ioe);
}
} }
} }
// finally close our input stream
StreamUtil.close(iis);
return image; return image;
} }