}
long startTime = System.currentTimeMillis();
DBBroker broker = null;
DocumentImpl document = null;
try {
broker = brokerPool.get(subject);
// If it is not a collection, check if it is a document
document = broker.getXMLResource(xmldbUri, Lock.READ_LOCK);
if (document.getResourceType() == DocumentImpl.XML_FILE) {
// Stream XML document
Serializer serializer = broker.getSerializer();
serializer.reset();
try {
// Set serialization options
serializer.setProperties(configuration);
// Serialize document
try (Writer w = new OutputStreamWriter(os, "UTF-8")) {
serializer.serialize(document, w);
w.flush();
}
// don;t flush
if (!(os instanceof VirtualTempFile)) {
os.flush();
}
} catch (SAXException e) {
LOG.error(e);
throw new IOException(String.format("Error while serializing XML document: %s", e.getMessage()), e);
}
} else {
// Stream NON-XML document
broker.readBinaryResource((BinaryDocument) document, os);
os.flush();
}
} catch (EXistException e) {
LOG.error(e);
throw new IOException(e.getMessage());
} catch (PermissionDeniedException e) {
LOG.error(e);
throw e;
} finally {
if (document != null) {
document.getUpdateLock().release(Lock.READ_LOCK);
}
brokerPool.release(broker);
if (LOG.isDebugEnabled()) {