Skip to content

Commit 9653c13

Browse files
committed
Merge branch 'issue_243' into b1_0
2 parents 73f8859 + fb3c4eb commit 9653c13

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
/**
@@ -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+
}

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)