< prev index next >

src/com/sun/org/apache/xerces/internal/impl/XMLEntityManager.java

Print this page
@  rev 1993 : 8213734: SAXParser.parse(File, ..) does not close resources when Exception occurs.
|  Reviewed-by: lancea
~

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
  */
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
  * this work for additional information regarding copyright ownership.

@@ -822,11 +822,11 @@
             //reader = new OneCharReader(reader);
         }
 
         // We've seen a new Reader.
         // Push it on the stack so we can close it later.
-        //fOwnReaders.add(reader);
+        fReaderStack.push(reader);
 
         // push entity on stack
         if (fCurrentEntity != null) {
             fEntityStack.push(fCurrentEntity);
         }

@@ -1345,20 +1345,25 @@
     public Entity.ScannedEntity getTopLevelEntity() {
         return (Entity.ScannedEntity)
             (fEntityStack.empty() ? null : fEntityStack.elementAt(0));
     }
 
+    // A stack containing all the open readers
+    protected Stack<Reader> fReaderStack = new Stack<>();
 
     /**
      * Close all opened InputStreams and Readers opened by this parser.
      */
     public void closeReaders() {
-        /** this call actually does nothing, readers are closed in the endEntity method
-         * through the current entity.
-         * The change seems to have happened during the jdk6 development with the
-         * addition of StAX
-        **/
+        // close all readers
+        while (!fReaderStack.isEmpty()) {
+            try {
+                (fReaderStack.pop()).close();
+            } catch (IOException e) {
+                // ignore
+            }
+        }
     }
 
     public void endEntity() throws IOException, XNIException {
 
         // call handler

@@ -1388,10 +1393,17 @@
             }catch(IOException ex){
                 throw new XNIException(ex);
             }
         }
 
+        // REVISIT: We should never encounter underflow if the calls
+        // to startEntity and endEntity are balanced, but guard
+        // against the EmptyStackException for now. -- mrglavas
+        if (!fReaderStack.isEmpty()) {
+            fReaderStack.pop();
+        }
+
         if (fEntityHandler != null) {
             //so this is the last opened entity, signal it to current fEntityHandler using Augmentation
             if(entity == null){
                 fEntityAugs.removeAllItems();
                 fEntityAugs.putItem(Constants.LAST_ENTITY, Boolean.TRUE);
< prev index next >