@@ -39,6 +39,8 @@ public class WWXML
39
39
{
40
40
public static final String XLINK_URI = "http://www.w3.org/1999/xlink" ;
41
41
42
+ private static Map <XMLEventReader , InputStream > inputSources = new HashMap <XMLEventReader , InputStream >();
43
+
42
44
/**
43
45
* Create a DOM builder.
44
46
*
@@ -170,9 +172,12 @@ public static Document openDocumentFile(String filePath, Class c)
170
172
throw new IllegalArgumentException (message );
171
173
}
172
174
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
+ }
176
181
}
177
182
178
183
/**
@@ -273,10 +278,7 @@ public static void saveDocumentToFile(Document doc, String filePath)
273
278
throw new IllegalArgumentException (message );
274
279
}
275
280
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 )) {
280
282
saveDocumentToStream (doc , outputStream );
281
283
}
282
284
catch (IOException e )
@@ -356,7 +358,9 @@ public static XMLEventReader openEventReaderStream(InputStream inputStream, bool
356
358
357
359
try
358
360
{
359
- return inputFactory .createXMLEventReader (inputStream );
361
+ XMLEventReader reader = inputFactory .createXMLEventReader (inputStream );
362
+ inputSources .put (reader , inputStream );
363
+ return reader ;
360
364
}
361
365
catch (XMLStreamException e )
362
366
{
@@ -440,7 +444,9 @@ public static XMLEventReader openEventReaderURL(URL url, boolean isNamespaceAwar
440
444
try
441
445
{
442
446
InputStream inputStream = url .openStream ();
443
- return openEventReaderStream (inputStream , isNamespaceAware );
447
+ XMLEventReader reader = openEventReaderStream (inputStream , isNamespaceAware );
448
+ inputSources .put (reader , inputStream );
449
+ return reader ;
444
450
}
445
451
catch (IOException e )
446
452
{
@@ -531,6 +537,15 @@ public static void closeEventReader(XMLEventReader eventReader, String name)
531
537
try
532
538
{
533
539
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
+ }
534
549
}
535
550
catch (XMLStreamException e )
536
551
{
0 commit comments