Title: [2116] trunk/xstream/src/java/com/thoughtworks/xstream/io: StAX based drivers (StaxDriver and JettisonMappedXmlDriver) are not closing internal input stream reading from file or URL (XSTR-685).
Revision
2116
Author
joehni
Date
2013-09-17 14:44:14 -0500 (Tue, 17 Sep 2013)

Log Message

StAX based drivers (StaxDriver and JettisonMappedXmlDriver) are not closing internal input stream reading from file or URL (XSTR-685).

Modified Paths

Diff

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/io/json/JettisonMappedXmlDriver.java (2115 => 2116)


--- trunk/xstream/src/java/com/thoughtworks/xstream/io/json/JettisonMappedXmlDriver.java	2013-09-17 17:52:23 UTC (rev 2115)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/io/json/JettisonMappedXmlDriver.java	2013-09-17 19:44:14 UTC (rev 2116)
@@ -97,25 +97,45 @@
     }
 
     public HierarchicalStreamReader createReader(URL in) {
+        InputStream instream = null;
         try {
+            instream = in.openStream();
             return new StaxReader(new QNameMap(), mif.createXMLStreamReader(
-                in.toExternalForm(), in.openStream()), getNameCoder());
+                in.toExternalForm(), instream), getNameCoder());
         } catch (final XMLStreamException e) {
             throw new StreamException(e);
         } catch (IOException e) {
             throw new StreamException(e);
+        } finally {
+            if (instream != null) {
+                try {
+                    instream.close();
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
         }
     }
 
     public HierarchicalStreamReader createReader(File in) {
+        InputStream instream = null;
         try {
+            instream = new FileInputStream(in);
             return new StaxReader(new QNameMap(), mif.createXMLStreamReader(in
                 .toURI()
-                .toASCIIString(), new FileInputStream(in)), getNameCoder());
+                .toASCIIString(), instream), getNameCoder());
         } catch (final XMLStreamException e) {
             throw new StreamException(e);
         } catch (IOException e) {
             throw new StreamException(e);
+        } finally {
+            if (instream != null) {
+                try {
+                    instream.close();
+                } catch (IOException e) {
+                    // ignore
+                }
+            }
         }
     }
 

Modified: trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/StaxDriver.java (2115 => 2116)


--- trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/StaxDriver.java	2013-09-17 17:52:23 UTC (rev 2115)
+++ trunk/xstream/src/java/com/thoughtworks/xstream/io/xml/StaxDriver.java	2013-09-17 19:44:14 UTC (rev 2116)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2004, 2005, 2006 Joe Walnes.
- * Copyright (C) 2006, 2007, 2009, 2011 XStream Committers.
+ * Copyright (C) 2006, 2007, 2009, 2011, 2013 XStream Committers.
  * All rights reserved.
  *
  * The software in this package is published under the terms of the BSD
@@ -12,6 +12,9 @@
 package com.thoughtworks.xstream.io.xml;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.Reader;
@@ -28,6 +31,7 @@
 
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
+import com.thoughtworks.xstream.io.ReaderWrapper;
 import com.thoughtworks.xstream.io.StreamException;
 import com.thoughtworks.xstream.io.naming.NameCoder;
 
@@ -100,18 +104,50 @@
     }
 
     public HierarchicalStreamReader createReader(URL in) {
+        final InputStream stream;
         try {
-            return createStaxReader(createParser(new StreamSource(in.toExternalForm())));
+            stream = in.openStream();
+            HierarchicalStreamReader reader = createStaxReader(createParser(new StreamSource(
+                stream, in.toExternalForm())));
+            return new ReaderWrapper(reader) {
+
+                public void close() {
+                    super.close();
+                    try {
+                        stream.close();
+                    } catch (IOException e) {
+                        // ignore
+                    }
+                }
+            };
         } catch (XMLStreamException e) {
             throw new StreamException(e);
+        } catch (IOException e) {
+            throw new StreamException(e);
         }
     }
 
     public HierarchicalStreamReader createReader(File in) {
+        final InputStream stream;
         try {
-            return createStaxReader(createParser(new StreamSource(in)));
+            stream = new FileInputStream(in);
+            HierarchicalStreamReader reader = createStaxReader(createParser(new StreamSource(
+                stream, in.toURI().toASCIIString())));
+            return new ReaderWrapper(reader) {
+
+                public void close() {
+                    super.close();
+                    try {
+                        stream.close();
+                    } catch (IOException e) {
+                        // ignore
+                    }
+                }
+            };
         } catch (XMLStreamException e) {
             throw new StreamException(e);
+        } catch (FileNotFoundException e) {
+            throw new StreamException(e);
         }
     }
 

To unsubscribe from this list please visit:

http://xircles.codehaus.org/manage_email

Reply via email to