4646 * An XML Stream Reader Handle represents XML content as an XML stream reader
4747 * for reading as a StAX pull stream.
4848 *
49- * When finished with the stream reader, close the stream reader to release
49+ * When finished with the stream reader, call {@link #close} to release
5050 * the response.
5151 */
5252public class XMLStreamReaderHandle
@@ -59,6 +59,7 @@ public class XMLStreamReaderHandle
5959
6060 private XMLResolver resolver ;
6161 private XMLStreamReader content ;
62+ private InputStream contentSource ;
6263 private XMLInputFactory factory ;
6364
6465 /**
@@ -99,7 +100,7 @@ public void setResolver(XMLResolver resolver) {
99100 * Returns an XML Stream Reader for for reading a resource from the database
100101 * as a StAX pull stream.
101102 *
102- * When finished with the stream reader, close the stream reader to release
103+ * When finished with the stream reader, call {@link #close} to release
103104 * the response.
104105 *
105106 * @return the XML stream reader
@@ -167,6 +168,16 @@ public byte[] toBuffer() {
167168 throw new MarkLogicIOException (e );
168169 }
169170 }
171+ /**
172+ * Closes the XMLStreamReader and the InputStream, if any, used to populate
173+ * the XMLStreamReader. This method should always be called when finished
174+ * with the stream reader.
175+ */
176+ public void close () throws XMLStreamException , IOException {
177+ if ( content != null ) content .close ();
178+ if ( contentSource != null ) contentSource .close ();
179+ }
180+
170181 /**
171182 * Buffers the StAX stream and returns the buffer as an XML string.
172183 */
@@ -227,6 +238,7 @@ protected void receiveContent(InputStream content) {
227238 factory .setXMLResolver (resolver );
228239
229240 this .content = factory .createXMLStreamReader (content , "UTF-8" );
241+ this .contentSource = content ;
230242 } catch (XMLStreamException e ) {
231243 logger .error ("Failed to parse StAX stream from input stream" ,e );
232244 throw new MarkLogicInternalException (e );
@@ -268,4 +280,4 @@ public void write(OutputStream out) throws IOException {
268280 throw new MarkLogicInternalException (e );
269281 }
270282 }
271- }
283+ }
0 commit comments