use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method constructed_from_CachingFilterInputStream_consumed.
@Test
public void constructed_from_CachingFilterInputStream_consumed() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
final byte[] testData = generateRandomData(_12KB);
final InputStream is = new MarkShieldInputStream(new UnsynchronizedByteArrayInputStream(testData));
// first CachingFilterInputStream
final CachingFilterInputStream cfis1 = new CachingFilterInputStream(getNewCache(is));
assertArrayEquals(testData, consumeInputStream(cfis1));
// second CachingFilterInputStream wraps first CachingFilterInputStream
final CachingFilterInputStream cfis2 = new CachingFilterInputStream(cfis1);
assertArrayEquals(testData, consumeInputStream(cfis2));
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method constructed_from_CachingFilterInputStream_partiallyConsumed.
@Test
public void constructed_from_CachingFilterInputStream_partiallyConsumed() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
final byte[] testData = generateRandomData(_12KB);
final InputStream is = new MarkShieldInputStream(new UnsynchronizedByteArrayInputStream(testData));
// first CachingFilterInputStream
final CachingFilterInputStream cfis1 = new CachingFilterInputStream(getNewCache(is));
// read first 6KB
final byte[] firstPart = new byte[_6KB];
cfis1.read(firstPart);
// ensure first 6KB was read!
assertArrayEquals(subArray(testData, _6KB), firstPart);
// second CachingFilterInputStream wraps first CachingFilterInputStream
final CachingFilterInputStream cfis2 = new CachingFilterInputStream(cfis1);
assertArrayEquals(testData, consumeInputStream(cfis2));
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method multiFilter_withoutIncrementReferenceCountFails.
@Test(expected = IOException.class)
public void multiFilter_withoutIncrementReferenceCountFails() throws IOException, XPathException {
final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
final byte[] testData = "test data".getBytes();
try (final InputStream bais = new UnsynchronizedByteArrayInputStream(testData)) {
final BinaryValue binaryValue1 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
final InputStream bvis1 = binaryValue1.getInputStream();
final BinaryValue binaryValue2 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais);
final InputStream bvis2 = binaryValue2.getInputStream();
// create a filter over both BinaryValues, with no reference count increment
final InputStream fis = new MultiBinaryValueFilteringInputStream(new InputStream[] { bvis1, bvis2 }, 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 values, as we have not incremented the reference counts!
filteredBinaryValue.close();
assertTrue(filteredBinaryValue.isClosed());
fis.close();
bvis2.close();
assertTrue(binaryValue2.isClosed());
bvis1.close();
assertTrue(binaryValue1.isClosed());
// we should not be able to read from the origin binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
// this should throw an IOException
binaryValue1.streamBinaryTo(baos);
}
} finally {
binaryValueManager.runCleanupTasks();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method multiFilter_withIncrementReferenceCount.
@Test
public void multiFilter_withIncrementReferenceCount() throws IOException, XPathException {
final BinaryValueManager binaryValueManager = new MockBinaryValueManager();
final byte[] testData1 = "test data".getBytes();
final byte[] testData2 = "second test data".getBytes();
try (final InputStream bais1 = new UnsynchronizedByteArrayInputStream(testData1);
final InputStream bais2 = new UnsynchronizedByteArrayInputStream(testData2)) {
final BinaryValue binaryValue1 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais1);
final InputStream bvis1 = binaryValue1.getInputStream();
final BinaryValue binaryValue2 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), bais2);
final InputStream bvis2 = binaryValue2.getInputStream();
// create a filter over both BinaryValues, and reference count increment
final InputStream fis = new MultiBinaryValueFilteringInputStream(new InputStream[] { bvis1, bvis2 }, true);
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 not close the original binary values, as MultiBinaryValueFilteringInputStream increased the reference count.
filteredBinaryValue.close();
assertTrue(filteredBinaryValue.isClosed());
assertFalse(binaryValue2.isClosed());
assertFalse(binaryValue1.isClosed());
// we should still be able to read from the origin binary value2!
try (final UnsynchronizedByteArrayOutputStream baos2 = new UnsynchronizedByteArrayOutputStream()) {
binaryValue2.streamBinaryTo(baos2);
assertArrayEquals(testData2, baos2.toByteArray());
}
// we should still be able to read from the original binary value1!
try (final UnsynchronizedByteArrayOutputStream baos1 = new UnsynchronizedByteArrayOutputStream()) {
binaryValue1.streamBinaryTo(baos1);
assertArrayEquals(testData1, baos1.toByteArray());
}
// finally close the original binary values
fis.close();
bvis2.close();
binaryValue2.close();
assertTrue(binaryValue2.isClosed());
bvis1.close();
binaryValue1.close();
assertTrue(binaryValue1.isClosed());
} finally {
binaryValueManager.runCleanupTasks();
}
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class BinaryValueFromInputStreamTest method multiFilterFilter_withIncrementReferenceCount.
@Test
public void multiFilterFilter_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);
// create a first filter over the first BinaryValue, and reference count increment
final InputStream fis1 = new BinaryValueFilteringInputStream(binaryValue.getInputStream(), true);
final BinaryValue filteredBinaryValue1 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis1);
// create a second filter over the first BinaryValue, and reference count increment
final InputStream fis2 = new BinaryValueFilteringInputStream(binaryValue.getInputStream(), true);
final BinaryValue filteredBinaryValue2 = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), fis2);
// create a multi filter over both filters
final InputStream mfis = new MultiBinaryValueFilteringInputStream(new InputStream[] { filteredBinaryValue1.getInputStream(), filteredBinaryValue2.getInputStream() }, true);
final BinaryValue multiFilteredBinaryValue = BinaryValueFromInputStream.getInstance(binaryValueManager, new Base64BinaryValueType(), mfis);
// we now destroy the multi filtered binary value, just as it would be if it went out of scope from popLocalVariables#popLocalVariables.
// It should not close the filtered or original binary values, as MultiBinaryValueFilteringInputStream increased the reference count.
multiFilteredBinaryValue.close();
assertTrue(multiFilteredBinaryValue.isClosed());
assertFalse(filteredBinaryValue2.isClosed());
assertFalse(filteredBinaryValue1.isClosed());
assertFalse(binaryValue.isClosed());
// we should still be able to read from the filtered binary value2!
try (final UnsynchronizedByteArrayOutputStream baos2 = new UnsynchronizedByteArrayOutputStream()) {
filteredBinaryValue2.streamBinaryTo(baos2);
assertArrayEquals(testData, baos2.toByteArray());
}
mfis.close();
filteredBinaryValue2.close();
assertTrue(filteredBinaryValue2.isClosed());
// we should still be able to read from the filtered binary value1!
try (final UnsynchronizedByteArrayOutputStream baos1 = new UnsynchronizedByteArrayOutputStream()) {
filteredBinaryValue1.streamBinaryTo(baos1);
assertArrayEquals(testData, baos1.toByteArray());
}
filteredBinaryValue1.close();
assertTrue(filteredBinaryValue1.isClosed());
// we should still be able to read from the original binary value!
try (final UnsynchronizedByteArrayOutputStream baos = new UnsynchronizedByteArrayOutputStream()) {
binaryValue.streamBinaryTo(baos);
assertArrayEquals(testData, baos.toByteArray());
}
// finally close the original binary value
fis2.close();
fis1.close();
binaryValue.close();
assertTrue(binaryValue.isClosed());
} finally {
binaryValueManager.runCleanupTasks();
}
}
Aggregations