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 /**
@@ -98,7 +99,7 @@ public void setResolver(XMLResolver resolver) {
9899 * Returns an XML Stream Reader for for reading a resource from the database
99100 * as a StAX pull stream.
100101 *
101- * When finished with the stream reader, close the stream reader to release
102+ * When finished with the stream reader, call {@link #close} to release
102103 * the response.
103104 *
104105 * @return the XML stream reader
@@ -166,6 +167,16 @@ public byte[] toBuffer() {
166167 throw new MarkLogicIOException (e );
167168 }
168169 }
170+ /**
171+ * Closes the XMLStreamReader and the InputStream, if any, used to populate
172+ * the XMLStreamReader. This method should always be called when finished
173+ * with the stream reader.
174+ */
175+ public void close () throws XMLStreamException , IOException {
176+ if ( content != null ) content .close ();
177+ if ( contentSource != null ) contentSource .close ();
178+ }
179+
169180 /**
170181 * Buffers the StAX stream and returns the buffer as an XML string.
171182 */
@@ -226,6 +237,7 @@ protected void receiveContent(InputStream content) {
226237 factory .setXMLResolver (resolver );
227238
228239 this .content = factory .createXMLStreamReader (content , "UTF-8" );
240+ this .contentSource = content ;
229241 } catch (XMLStreamException e ) {
230242 logger .error ("Failed to parse StAX stream from input stream" ,e );
231243 throw new MarkLogicInternalException (e );
@@ -267,4 +279,4 @@ public void write(OutputStream out) throws IOException {
267279 throw new MarkLogicInternalException (e );
268280 }
269281 }
270- }
282+ }
0 commit comments