Skip to content

Commit 4e1667f

Browse files
committed
fix #243, offer a way to close the input source for XMLStreamReaderHandle
1 parent d0be4fe commit 4e1667f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/com/marklogic/client/io/XMLStreamReaderHandle.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
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
*/
5454
public 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);

src/test/java/com/marklogic/client/test/XMLDocumentTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public void startElement(String uri, String localName, String qName, Attributes
137137
assertTrue("Failed to process XML document with SAX",
138138
counter.get("elementCount") == 2 && counter.get("attributeCount") == 2);
139139

140-
XMLStreamReader streamReader = docMgr.read(docId, new XMLStreamReaderHandle()).get();
140+
XMLStreamReaderHandle streamReaderHandle = docMgr.read(docId, new XMLStreamReaderHandle());
141+
XMLStreamReader streamReader = streamReaderHandle.get();
141142
int elementCount = 0;
142143
int attributeCount = 0;
143144
while (streamReader.hasNext()) {
@@ -148,7 +149,7 @@ public void startElement(String uri, String localName, String qName, Attributes
148149
if (elementAttributeCount > 0)
149150
attributeCount += elementAttributeCount;
150151
}
151-
streamReader.close();
152+
streamReaderHandle.close();
152153
assertTrue("Failed to process XML document with StAX stream reader",
153154
elementCount == 2 && attributeCount == 2);
154155

0 commit comments

Comments
 (0)