Search in sources :

Example 16 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest method readBytes_withZeroOffset_partFromCache.

@Test
public void readBytes_withZeroOffset_partFromCache() 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));
    // mark the position
    cfis.mark(Integer.MAX_VALUE);
    // read the first 5 byts data
    byte[] result = new byte[5];
    int read = cfis.read(result, 0, result.length);
    assertEquals(5, read);
    assertArrayEquals(subArray(testData, 5), result);
    // reset position to the mark
    cfis.reset();
    // attempt to read all the data (first 5 bytes will be from the cache)
    result = new byte[testData.length];
    read = cfis.read(result, 0, result.length);
    assertEquals(testData.length, read);
    assertArrayEquals(testData, result);
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 17 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest method skip_partFromCache.

@Test
public void skip_partFromCache() 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));
    // read the first 2 bytes
    assertEquals(testData[0], cfis.read());
    assertEquals(testData[1], cfis.read());
    // skip 2 bytes
    cfis.skip(2);
    cfis.mark(Integer.MAX_VALUE);
    // read byte 5
    assertEquals(testData[4], cfis.read());
    // skip 2 bytes
    cfis.skip(2);
    // read bytes 6 to 7 inclusive
    assertEquals(testData[7], cfis.read());
    assertEquals(testData[8], cfis.read());
    cfis.reset();
    // reread bytes 5 to 7 inclusive
    assertEquals(testData[4], cfis.read());
    assertEquals(testData[5], cfis.read());
    assertEquals(testData[6], cfis.read());
    assertEquals(testData[7], cfis.read());
    assertEquals(testData[8], cfis.read());
    // read final byte (outside cache)
    assertEquals(testData[9], cfis.read());
}
Also used : UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) Test(org.junit.Test)

Example 18 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readByte_allFromCache.

@Test
public void readByte_allFromCache() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final String testString = "hello";
    final byte[] testData = testString.getBytes();
    final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    // mark the position
    cfis.mark(Integer.MAX_VALUE);
    // read the data
    assertEquals(testData[0], cfis.read());
    assertEquals(testData[1], cfis.read());
    assertEquals(testData[2], cfis.read());
    assertEquals(testData[3], cfis.read());
    assertEquals(testData[4], cfis.read());
    // reset position to the mark
    cfis.reset();
    // attempt to reread the data (from the cache)
    assertEquals(testData[0], cfis.read());
    assertEquals(testData[1], cfis.read());
    assertEquals(testData[2], cfis.read());
    assertEquals(testData[3], cfis.read());
    assertEquals(testData[4], 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 19 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes_partFromCache.

@Test
public void readBytes_partFromCache() 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));
    // mark the position
    cfis.mark(Integer.MAX_VALUE);
    // read the first 5 byts data
    byte[] result = new byte[5];
    int read = cfis.read(result);
    assertEquals(5, read);
    assertArrayEquals(subArray(testData, 5), result);
    // reset position to the mark
    cfis.reset();
    // attempt to read all the data (first 5 bytes will be from the cache)
    result = new byte[testData.length];
    read = cfis.read(result);
    assertEquals(testData.length, read);
    assertArrayEquals(testData, 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 20 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes_pastEndOfStream_fromCache.

@Test
public void readBytes_pastEndOfStream_fromCache() 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));
    cfis.mark(Integer.MAX_VALUE);
    // read first two bytes from stream
    final byte[] result = new byte[2];
    int read = cfis.read(result);
    assertEquals(2, read);
    assertArrayEquals(subArray(testData, 2), result);
    cfis.reset();
    // read all bytes from cache and src, +1 past end of stream
    final byte[] endOfStreamResult = new byte[testData.length + 1];
    read = cfis.read(endOfStreamResult);
    final byte[] expectedResult = new byte[testData.length + 1];
    System.arraycopy(testData, 0, expectedResult, 0, testData.length);
    assertEquals(testData.length, read);
    assertArrayEquals(expectedResult, endOfStreamResult);
    // 2nd attempt to read past end of stream
    read = cfis.read(result);
    assertEquals(-1, read);
    read = cfis.read(result);
    assertEquals(-1, 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)

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