Skip to content

Commit fb3c4eb

Browse files
committed
fix #243, offer a way to close the input source for XMLStreamReaderHandle
1 parent 6bfffdf commit fb3c4eb

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
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
*/
5252
public 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+
}

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

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

134-
XMLStreamReader streamReader = docMgr.read(docId, new XMLStreamReaderHandle()).get();
134+
XMLStreamReaderHandle streamReaderHandle = docMgr.read(docId, new XMLStreamReaderHandle());
135+
XMLStreamReader streamReader = streamReaderHandle.get();
135136
int elementCount = 0;
136137
int attributeCount = 0;
137138
while (streamReader.hasNext()) {
@@ -142,7 +143,7 @@ public void startElement(String uri, String localName, String qName, Attributes
142143
if (elementAttributeCount > 0)
143144
attributeCount += elementAttributeCount;
144145
}
145-
streamReader.close();
146+
streamReaderHandle.close();
146147
assertTrue("Failed to process XML document with StAX stream reader",
147148
elementCount == 2 && attributeCount == 2);
148149

0 commit comments

Comments
 (0)