Search in sources :

Example 76 with UnsynchronizedByteArrayInputStream

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));
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) MarkShieldInputStream(org.apache.commons.io.input.MarkShieldInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) MarkShieldInputStream(org.apache.commons.io.input.MarkShieldInputStream) Test(org.junit.Test)

Example 77 with UnsynchronizedByteArrayInputStream

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));
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) MarkShieldInputStream(org.apache.commons.io.input.MarkShieldInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) MarkShieldInputStream(org.apache.commons.io.input.MarkShieldInputStream) Test(org.junit.Test)

Example 78 with UnsynchronizedByteArrayInputStream

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();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 79 with UnsynchronizedByteArrayInputStream

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();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Example 80 with UnsynchronizedByteArrayInputStream

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();
    }
}
Also used : FilterInputStream(java.io.FilterInputStream) CachingFilterInputStream(org.exist.util.io.CachingFilterInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) Test(org.junit.Test)

Aggregations

UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)114 InputStream (java.io.InputStream)102 Test (org.junit.Test)93 MarkShieldInputStream (org.apache.commons.io.input.MarkShieldInputStream)31 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)10 IOException (java.io.IOException)8 FilterInputStream (java.io.FilterInputStream)7 CachingFilterInputStream (org.exist.util.io.CachingFilterInputStream)7 XMLResource (org.xmldb.api.modules.XMLResource)6 DBBroker (org.exist.storage.DBBroker)5 Txn (org.exist.storage.txn.Txn)5 Element (org.w3c.dom.Element)4 Collection (org.xmldb.api.base.Collection)4 NodeProxy (org.exist.dom.persistent.NodeProxy)3 PermissionDeniedException (org.exist.security.PermissionDeniedException)3 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)3 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)3 BooleanValue (org.exist.xquery.value.BooleanValue)3 DoubleValue (org.exist.xquery.value.DoubleValue)3 StringValue (org.exist.xquery.value.StringValue)3