Search in sources :

Example 26 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes_withOffsetAndLength_allFromCache.

@Test
public void readBytes_withOffsetAndLength_allFromCache() 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 data
    byte[] result = new byte[4];
    int read = cfis.read(result, 1, 3);
    assertEquals(3, read);
    byte[] expected = new byte[4];
    expected[0] = 0;
    System.arraycopy(testData, 0, expected, 1, 3);
    assertArrayEquals(expected, result);
    // reset position to the mark
    cfis.reset();
    // attempt to reread the data (from the cache)
    result = new byte[4];
    read = cfis.read(result, 2, 2);
    expected = new byte[4];
    expected[0] = 0;
    expected[1] = 0;
    System.arraycopy(testData, 0, expected, 2, 2);
    assertEquals(2, read);
    assertArrayEquals(expected, 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 27 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method skip_negativeBytes.

@Test
public void skip_negativeBytes() 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));
    // should cause IOException
    final long skipped = cfis.skip(-1);
    assertEquals(0, skipped);
}
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 28 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream 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();
    final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
    final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    cfis.close();
    // should cause IOException
    cfis.skip(1);
}
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 29 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method available_onEmptyStream.

@Test
public void available_onEmptyStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    final InputStream is = new UnsynchronizedByteArrayInputStream(new byte[] {});
    final CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
    cfis.close();
    assertEquals(0, 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 30 with UnsynchronizedByteArrayInputStream

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

the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes.

@Test
public void readBytes() 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
    byte[] result = new byte[3];
    int read = cfis.read(result);
    assertEquals(3, read);
    assertArrayEquals(subArray(testData, 3), result);
    // mark position
    cfis.mark(Integer.MAX_VALUE);
    // read the next 3 bytes
    result = new byte[3];
    read = cfis.read(result);
    assertEquals(3, read);
    assertArrayEquals(subArray(testData, 3, 3), result);
    // reset position to the mark
    cfis.reset();
    // attempt to reread the last 3 bytes from the mark (from the cache)
    result = new byte[3];
    read = cfis.read(result);
    assertEquals(3, read);
    assertArrayEquals(subArray(testData, 3, 3), result);
    // read the next 2 bytes past the reset mark (past the cache, e.g. from src)
    result = new byte[2];
    read = cfis.read(result);
    assertEquals(2, read);
    assertArrayEquals(subArray(testData, 6, 2), result);
    // reset position to the mark
    cfis.reset();
    // attempt to read the last 5 bytes (from the cache)
    result = new byte[5];
    read = cfis.read(result);
    assertEquals(5, read);
    assertArrayEquals(subArray(testData, 3, 5), result);
    // mark position
    cfis.mark(-1);
    // read the next 2 bytes past the reset mark (past the cache, e.g. from src)
    result = new byte[2];
    read = cfis.read(result);
    assertEquals(2, read);
    assertArrayEquals(subArray(testData, 8, 2), result);
    // reset position to the mark
    cfis.reset();
    // attempt to reread the last 2 bytes from the mark (from the cache)
    result = new byte[2];
    read = cfis.read(result);
    assertEquals(2, read);
    assertArrayEquals(subArray(testData, 8, 2), 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)

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