use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream 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();
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, 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);
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method available_onCachedStream.
@Test
public void available_onCachedStream() 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 for later reset
cfis.mark(Integer.MAX_VALUE);
for (int i = 0; i < testData.length; i++) {
cfis.read();
}
// return to the start of the stream
cfis.reset();
assertEquals(testData.length, cfis.available());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method readBytes_allFromCache.
@Test
public void readBytes_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
byte[] result = new byte[testData.length];
int read = cfis.read(result);
assertEquals(testData.length, read);
assertArrayEquals(testData, result);
// reset position to the mark
cfis.reset();
// attempt to reread the data (from the cache)
result = new byte[testData.length];
read = cfis.read(result);
assertEquals(testData.length, read);
assertArrayEquals(testData, result);
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method skip_partFromCache.
@Test
public void skip_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));
// 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());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest_NonMarkableByteArrayInputStream method constructed_from_CachingFilterInputStream.
@Test
public void constructed_from_CachingFilterInputStream() throws IOException, InstantiationException, IllegalAccessException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
final byte[] testData = generateRandomData(_12KB);
final InputStream is = new UnsynchronizedByteArrayInputStream(testData);
// first CachingFilterInputStream
final CachingFilterInputStream cfis1 = new CachingFilterInputStream(getNewCache(is));
// second CachingFilterInputStream wraps first CachingFilterInputStream
final CachingFilterInputStream cfis2 = new CachingFilterInputStream(cfis1);
assertArrayEquals(testData, consumeInputStream(cfis2));
}
Aggregations