4848 * An XML Stream Reader Handle represents XML content as an XML stream reader
4949 * for reading as a StAX pull stream.
5050 *
51- * When finished with the stream reader, close the stream reader to release
51+ * When finished with the stream reader, call {@link #close} to release
5252 * the response.
5353 */
5454public class XMLStreamReaderHandle
@@ -61,6 +61,7 @@ public class XMLStreamReaderHandle
6161
6262 private XMLResolver resolver ;
6363 private XMLStreamReader content ;
64+ private InputStream contentSource ;
6465 private XMLInputFactory factory ;
6566
6667 /**
@@ -125,7 +126,7 @@ public void setResolver(XMLResolver resolver) {
125126 * Returns an XML Stream Reader for for reading a resource from the database
126127 * as a StAX pull stream.
127128 *
128- * When finished with the stream reader, close the stream reader to release
129+ * When finished with the stream reader, call {@link #close} to release
129130 * the response.
130131 *
131132 * @return the XML stream reader
@@ -195,6 +196,16 @@ public byte[] toBuffer() {
195196 throw new MarkLogicIOException (e );
196197 }
197198 }
199+ /**
200+ * Closes the XMLStreamReader and the InputStream, if any, used to populate
201+ * the XMLStreamReader. This method should always be called when finished
202+ * with the stream reader.
203+ */
204+ public void close () throws XMLStreamException , IOException {
205+ if ( content != null ) content .close ();
206+ if ( contentSource != null ) contentSource .close ();
207+ }
208+
198209 /**
199210 * Buffers the StAX stream and returns the buffer as an XML string.
200211 */
@@ -255,6 +266,7 @@ protected void receiveContent(InputStream content) {
255266 factory .setXMLResolver (resolver );
256267
257268 this .content = factory .createXMLStreamReader (content , "UTF-8" );
269+ this .contentSource = content ;
258270 } catch (XMLStreamException e ) {
259271 logger .error ("Failed to parse StAX stream from input stream" ,e );
260272 throw new MarkLogicInternalException (e );
@@ -296,4 +308,4 @@ public void write(OutputStream out) throws IOException {
296308 throw new MarkLogicInternalException (e );
297309 }
298310 }
299- }
311+ }
0 commit comments