@@ -39,6 +39,8 @@ public class WWXML
3939{
4040 public static final String XLINK_URI = "http://www.w3.org/1999/xlink" ;
4141
42+ private static Map <XMLEventReader , InputStream > inputSources = new HashMap <XMLEventReader , InputStream >();
43+
4244 /**
4345 * Create a DOM builder.
4446 *
@@ -170,9 +172,12 @@ public static Document openDocumentFile(String filePath, Class c)
170172 throw new IllegalArgumentException (message );
171173 }
172174
173- InputStream inputStream = WWIO .openFileOrResourceStream (filePath , c );
174-
175- return inputStream != null ? openDocumentStream (inputStream ) : null ;
175+ try (InputStream inputStream = WWIO .openFileOrResourceStream (filePath , c )) {
176+ return inputStream != null ? openDocumentStream (inputStream ) : null ;
177+ } catch (IOException e ) {
178+ e .printStackTrace ();
179+ return null ;
180+ }
176181 }
177182
178183 /**
@@ -273,10 +278,7 @@ public static void saveDocumentToFile(Document doc, String filePath)
273278 throw new IllegalArgumentException (message );
274279 }
275280
276- try
277- {
278- java .io .FileOutputStream outputStream = new java .io .FileOutputStream (filePath );
279-
281+ try (java .io .FileOutputStream outputStream = new java .io .FileOutputStream (filePath )) {
280282 saveDocumentToStream (doc , outputStream );
281283 }
282284 catch (IOException e )
@@ -356,7 +358,9 @@ public static XMLEventReader openEventReaderStream(InputStream inputStream, bool
356358
357359 try
358360 {
359- return inputFactory .createXMLEventReader (inputStream );
361+ XMLEventReader reader = inputFactory .createXMLEventReader (inputStream );
362+ inputSources .put (reader , inputStream );
363+ return reader ;
360364 }
361365 catch (XMLStreamException e )
362366 {
@@ -440,7 +444,9 @@ public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAwar
440444 try
441445 {
442446 InputStream inputStream = url .openStream ();
443- return openEventReaderStream (inputStream , isNamespaceAware );
447+ XMLEventReader reader = openEventReaderStream (inputStream , isNamespaceAware );
448+ inputSources .put (reader , inputStream );
449+ return reader ;
444450 }
445451 catch (IOException e )
446452 {
@@ -531,6 +537,15 @@ public static void closeEventReader(XMLEventReader eventReader, String name)
531537 try
532538 {
533539 eventReader .close ();
540+ InputStream is = inputSources .get (eventReader );
541+ if (is != null ) {
542+ try {
543+ is .close ();
544+ } catch (IOException e ) {
545+ e .printStackTrace ();
546+ }
547+ inputSources .remove (eventReader );
548+ }
534549 }
535550 catch (XMLStreamException e )
536551 {
0 commit comments