@@ -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 *
@@ -161,9 +163,16 @@ public static Document openDocumentFile(String filePath, Class c)
161163 throw new IllegalArgumentException (message );
162164 }
163165
164- InputStream inputStream = WWIO .openFileOrResourceStream (filePath , c );
165-
166- return inputStream != null ? openDocumentStream (inputStream ) : null ;
166+ try (InputStream inputStream = WWIO .openFileOrResourceStream (filePath , c ))
167+ {
168+ return inputStream != null ? openDocumentStream (inputStream ) : null ;
169+ }
170+ catch (IOException e )
171+ {
172+ String message = Logging .getMessage ("generic.ExceptionClosingStream" , filePath );
173+ Logging .logger ().severe (message );
174+ return null ;
175+ }
167176 }
168177
169178 /**
@@ -264,10 +273,8 @@ public static void saveDocumentToFile(Document doc, String filePath)
264273 throw new IllegalArgumentException (message );
265274 }
266275
267- try
276+ try ( java . io . FileOutputStream outputStream = new java . io . FileOutputStream ( filePath ))
268277 {
269- java .io .FileOutputStream outputStream = new java .io .FileOutputStream (filePath );
270-
271278 saveDocumentToStream (doc , outputStream );
272279 }
273280 catch (IOException e )
@@ -347,7 +354,9 @@ public static XMLEventReader openEventReaderStream(InputStream inputStream, bool
347354
348355 try
349356 {
350- return inputFactory .createXMLEventReader (inputStream );
357+ XMLEventReader reader = inputFactory .createXMLEventReader (inputStream );
358+ inputSources .put (reader , inputStream );
359+ return reader ;
351360 }
352361 catch (XMLStreamException e )
353362 {
@@ -431,7 +440,9 @@ public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAwar
431440 try
432441 {
433442 InputStream inputStream = url .openStream ();
434- return openEventReaderStream (inputStream , isNamespaceAware );
443+ XMLEventReader reader = openEventReaderStream (inputStream , isNamespaceAware );
444+ inputSources .put (reader , inputStream );
445+ return reader ;
435446 }
436447 catch (IOException e )
437448 {
@@ -517,11 +528,28 @@ else if (!(docSource instanceof String))
517528 public static void closeEventReader (XMLEventReader eventReader , String name )
518529 {
519530 if (eventReader == null )
531+ {
520532 return ;
533+ }
521534
522535 try
523536 {
524537 eventReader .close ();
538+ InputStream is = inputSources .get (eventReader );
539+ if (is != null )
540+ {
541+ try
542+ {
543+ is .close ();
544+ }
545+ catch (IOException e )
546+ {
547+ String message = Logging .getMessage ("generic.ExceptionClosingStream" ,
548+ name != null ? name : "Unknown" );
549+ Logging .logger ().severe (message );
550+ }
551+ inputSources .remove (eventReader );
552+ }
525553 }
526554 catch (XMLStreamException e )
527555 {
0 commit comments