Search in sources :

Example 31 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes_onClosedStream.

@Test(expected = IOException.class)
public void readBytes_onClosedStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "helloWorld";
    final byte[] testData = testString.getBytes();
    final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    final byte[] result = new byte[2];
    cfis.read(result);
    assertArrayEquals(subArray(testData, 2), result);
    cfis.close();
    // should cause IOException
    cfis.read(result);
}
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) Test(org.junit.Test)

Example 32 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method available_onOffsetCachedStream.

@Test
public void available_onOffsetCachedStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "helloWorld";
    final byte[] testData = testString.getBytes();
    final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    // read first 2 bytes
    cfis.read();
    cfis.read();
    // mark for later reset
    cfis.mark(Integer.MAX_VALUE);
    for (int i = 0; i < testData.length - 2; i++) {
        cfis.read();
    }
    // return to the start of the stream
    cfis.reset();
    assertEquals(testData.length - 2, cfis.available());
}
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) Test(org.junit.Test)

Example 33 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method interleavedSourceReads.

/**
 * When given an underlying InputSource and caching it twice with the same
 * cache we should be able to read the input twice assuming that the input
 * that we are interested in has not been read before a mark()
 */
@Test
public void interleavedSourceReads() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final byte[] testData = generateRandomData(_64KB);
    final InputStream is = new MarkShieldInputStream(new UnsynchronizedByteArrayInputStream(testData));
    final FilterInputStreamCache cache1 = getNewCache(is);
    final CachingFilterInputStream cfis1 = new CachingFilterInputStream(cache1);
    cfis1.mark(Integer.MAX_VALUE);
    final CachingFilterInputStream cfis2 = new CachingFilterInputStream(cache1);
    final byte[] result1 = new byte[_12KB];
    cfis1.read(result1);
    assertArrayEquals(subArray(testData, _12KB), result1);
    final byte[] result2 = new byte[_12KB];
    cfis2.read(result2);
    assertArrayEquals(subArray(testData, _12KB), result2);
}
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 34 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method skip.

@Test
public void skip() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "helloWorld";
    final byte[] testData = testString.getBytes();
    final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    // read the first 3 bytes
    assertEquals(testData[0], cfis.read());
    assertEquals(testData[1], cfis.read());
    assertEquals(testData[2], cfis.read());
    // skip 3 bytes
    cfis.skip(3);
    // read bytes 5 to 7 inclusive
    assertEquals(testData[6], cfis.read());
    assertEquals(testData[7], cfis.read());
    assertEquals(testData[8], cfis.read());
}
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) Test(org.junit.Test)

Example 35 with UnsynchronizedByteArrayInputStream

use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.

the class CachingFilterInputStreamTest method skip_onClosedStream.

@Test(expected = IOException.class)
public void skip_onClosedStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "helloWorld";
    final byte[] testData = testString.getBytes();
    InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    cfis.close();
    // should cause IOException
    cfis.skip(1);
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) 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