use of org.exist.util.CachingFilterInputStreamInputSource in project exist by eXist-db.
the class InMemoryOutputStream method stream.
public void stream(final XmldbURL xmldbURL, final InputStream is, @Deprecated final int length) throws IOException {
BrokerPool db;
try {
db = BrokerPool.getInstance();
} catch (EXistException e) {
throw new IOException(e);
}
try (final DBBroker broker = db.getBroker()) {
final XmldbURI collectionUri = XmldbURI.create(xmldbURL.getCollection());
final XmldbURI documentUri = XmldbURI.create(xmldbURL.getDocumentName());
final TransactionManager transact = db.getTransactionManager();
try (final Txn txn = transact.beginTransaction();
final Collection collection = broker.getOrCreateCollection(txn, collectionUri)) {
if (collection == null) {
throw new IOException("Resource " + collectionUri.toString() + " is not a collection.");
}
final LockManager lockManager = db.getLockManager();
txn.acquireCollectionLock(() -> lockManager.acquireCollectionWriteLock(collectionUri));
if (collection.hasChildCollection(broker, documentUri)) {
throw new IOException("Resource " + documentUri.toString() + " is a collection.");
}
try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), is);
final CachingFilterInputStream cfis = new CachingFilterInputStream(cache)) {
final MimeType mime = MimeTable.getInstance().getContentTypeFor(documentUri);
try (final ManagedDocumentLock lock = lockManager.acquireDocumentWriteLock(documentUri)) {
broker.storeDocument(txn, documentUri, new CachingFilterInputStreamInputSource(cfis), mime, collection);
}
}
txn.commit();
}
} catch (final IOException ex) {
LOG.debug(ex);
throw ex;
} catch (final Exception ex) {
LOG.debug(ex);
throw new IOException(ex.getMessage(), ex);
}
}
Aggregations