use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method filter_withIncrementReferenceCount.
@Test
public void filter_withIncrementReferenceCount() throws IOException, XPathException {
final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
final byte[] testData = "test data".getBytes();
try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
final InputStream bvis = binaryValue.getInputStream();
// create a filter over the first BinaryValue, and reference count increment
final InputStream fis = new BinaryValueFilteringInputStream(bvis, true);
final BinaryValue filteredBinaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis);
// we now destroy the filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
// It should not close the original binary value, as BinaryValueFilteringInputStream increased the reference count.
filteredBinaryValue.close();
assertTrue(filteredBinaryValue.isClosed());
assertFalse(binaryValue.isClosed());
// we should still be able to read from the origin binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
binaryValue.streamBinaryTo(baos);
assertArrayEquals(testData, baos.toByteArray());
}
// finally close the original binary value
fis.close();
bvis.close();
binaryValue.close();
assertTrue(binaryValue.isClosed());
} finally {
binaryValueManager.runCleanupTasks();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method filterFilter_withoutIncrementReferenceCountFails.
@Test(expected = IOException.class)
public void filterFilter_withoutIncrementReferenceCountFails() throws IOException, XPathException {
final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
final byte[] testData = "test data".getBytes();
try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
final InputStream bvis = binaryValue.getInputStream();
// create a filter over the first BinaryValue, with no reference count increment
final InputStream fis1 = new BinaryValueFilteringInputStream(bvis, false);
final BinaryValue filteredBinaryValue1 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis1);
// create a second filter over the first filter, with no reference count increment
final InputStream fbvis = filteredBinaryValue1.getInputStream();
final InputStream fis2 = new BinaryValueFilteringInputStream(fbvis, false);
final BinaryValue filteredBinaryValue2 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis2);
// we now destroy the second filtered binary value, just as it would if it went out of scope from popLocalVariables#popLocalVariables.
// It should close the first filtered binary value and original binary value, as we have not incremented the reference counts!
filteredBinaryValue2.close();
fis2.close();
fbvis.close();
assertTrue(filteredBinaryValue2.isClosed());
assertTrue(filteredBinaryValue1.isClosed());
fis1.close();
bvis.close();
assertTrue(binaryValue.isClosed());
// we should not be able to read from the first filtered binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
// this should throw an IOException
filteredBinaryValue1.streamBinaryTo(baos);
}
} finally {
binaryValueManager.runCleanupTasks();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method filter_withoutIncrementReferenceCountFails.
@Test(expected = IOException.class)
public void filter_withoutIncrementReferenceCountFails() throws IOException, XPathException {
final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
final byte[] testData = "test data".getBytes();
try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
final BinaryValue binaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
final InputStream bvis = binaryValue.getInputStream();
// create a filter over the first BinaryValue, with no reference count increment
final InputStream fis = new BinaryValueFilteringInputStream(bvis, false);
final BinaryValue filteredBinaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis);
// we now destroy the filtered binary value, just as it would be if it went out of scope from popLocalVariables#popLocalVariables.
// It should close the original binary value, as we have not incremented the reference count!
filteredBinaryValue.close();
assertTrue(filteredBinaryValue.isClosed());
fis.close();
bvis.close();
assertTrue(binaryValue.isClosed());
// we should not be able to read from the origin binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
// this should throw an IOException
binaryValue.streamBinaryTo(baos);
}
} finally {
binaryValueManager.runCleanupTasks();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class ActiveDirectoryRealmTest method setUpBeforeClass.
/**
* @throws java.lang.Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
InputStream is = new UnsynchronizedByteArrayInputStream(config.getBytes(StandardCharsets.UTF_8));
Configuration config = Configurator.parse(is);
realm = new ActiveDirectoryRealm(null, config);
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class ExistCollection method createFile.
public XmldbURI createFile(String newName, InputStream is, Long length, String contentType) throws IOException, PermissionDeniedException, CollectionDoesNotExistException {
if (LOG.isDebugEnabled())
LOG.debug("Create '{}' in '{}'", newName, xmldbUri);
XmldbURI newNameUri = XmldbURI.create(newName);
// Get mime, or NULL when not available
MimeType mime = MimeTable.getInstance().getContentTypeFor(newName);
if (mime == null) {
mime = MimeType.BINARY_TYPE;
}
// XML documents are not supported a small file will be created.
if (mime.isXMLType() && length == 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("Creating dummy XML file for null resource lock '{}'", newNameUri);
}
is = new UnsynchronizedByteArrayInputStream("<null_resource/>".getBytes(StandardCharsets.UTF_8));
}
final TransactionManager txnManager = brokerPool.getTransactionManager();
try (final DBBroker broker = brokerPool.get(Optional.ofNullable(subject));
final Txn txn = txnManager.beginTransaction();
final Collection collection = broker.openCollection(xmldbUri, LockMode.WRITE_LOCK)) {
// by ResourceFactory
if (collection == null) {
LOG.debug("Collection {} does not exist", xmldbUri);
txnManager.abort(txn);
throw new CollectionDoesNotExistException(xmldbUri + "");
}
if (LOG.isDebugEnabled()) {
if (mime.isXMLType()) {
LOG.debug("Inserting XML document '{}'", mime.getName());
} else {
LOG.debug("Inserting BINARY document '{}'", mime.getName());
}
}
// Stream into database
try (final FilterInputStreamCache cache = FilterInputStreamCacheFactory.getCacheInstance(() -> (String) broker.getConfiguration().getProperty(Configuration.BINARY_CACHE_CLASS_PROPERTY), is);
final CachingFilterInputStream cfis = new CachingFilterInputStream(cache);
final EXistInputSource eis = new CachingFilterInputStreamInputSource(cfis)) {
broker.storeDocument(txn, newNameUri, eis, mime, collection);
}
// Commit change
txnManager.commit(txn);
if (LOG.isDebugEnabled()) {
LOG.debug("Document created successfully");
}
} catch (EXistException | SAXException e) {
LOG.error(e);
throw new IOException(e);
} catch (LockException e) {
LOG.error(e);
throw new PermissionDeniedException(xmldbUri + "");
} catch (IOException | PermissionDeniedException e) {
LOG.error(e);
throw e;
} finally {
if (LOG.isDebugEnabled()) {
LOG.debug("Finished creation");
}
}
// Send the result back to the client
XmldbURI newResource = xmldbUri.append(newName);
return newResource;
}
Aggregations