xmlgraphics-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbowdi...@apache.org
Subject svn commit: r784103 - /xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
Date Fri, 12 Jun 2009 12:59:18 GMT
Author: cbowditch
Date: Fri Jun 12 12:59:18 2009
New Revision: 784103

URL: http://svn.apache.org/viewvc?rev=784103&view=rev
Log:
bug fix: make sure we close underlying stream when ImageInputStream is closed.

Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java?rev=784103&r1=784102&r2=784103&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
(original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
Fri Jun 12 12:59:18 2009
@@ -23,6 +23,9 @@
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
 import java.net.MalformedURLException;
 import java.net.URL;
 
@@ -173,7 +176,7 @@
                 //Buffer and uncompress if necessary
                 in = ImageUtil.autoDecorateInputStream(in);
                 imageSource = new ImageSource(
-                        ImageIO.createImageInputStream(in), source.getSystemId(), false);
+                        createImageInputStream(in), source.getSystemId(), false);
             } catch (IOException ioe) {
                 log.error("Unable to create ImageInputStream for InputStream"
                         + " from system identifier '"
@@ -183,6 +186,42 @@
         return imageSource;
     }
 
+    protected ImageInputStream createImageInputStream(InputStream in) throws IOException
{
+        ImageInputStream iin = ImageIO.createImageInputStream(in);
+        return (ImageInputStream)Proxy.newProxyInstance(
+                getClass().getClassLoader(),
+                new Class[] {ImageInputStream.class},
+                new ObservingImageInputStreamInvocationHandler(iin, in));
+    }
+
+    private static class ObservingImageInputStreamInvocationHandler
+        implements InvocationHandler {
+
+        private ImageInputStream iin;
+        private InputStream in;
+
+        public ObservingImageInputStreamInvocationHandler(ImageInputStream iin,
+                InputStream underlyingStream) {
+            this.iin = iin;
+            this.in = underlyingStream;
+        }
+
+        /** {@inheritDoc} */
+        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
+            if ("close".equals(method.getName())) {
+                try {
+                    return method.invoke(iin, args);
+                } finally {
+                    IOUtils.closeQuietly(this.in);
+                    this.in = null;
+                }
+            } else {
+                return method.invoke(iin, args);
+            }
+        }
+
+    }
+
     /**
      * Convert from a <code>URL</code> to a <code>File</code>.
      * <p>



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org


Mime
View raw message