use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method readByte_onClosedStream.
@Test(expected = IOException.class)
public void readByte_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));
assertEquals(testData[0], cfis.read());
cfis.close();
// should cause IOException
cfis.read();
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method available_onCachedStream.
@Test
public void available_onCachedStream() 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 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 method readByte.
@Test
public void readByte() 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 3 bytes
assertEquals(testData[0], cfis.read());
assertEquals(testData[1], cfis.read());
assertEquals(testData[2], cfis.read());
// mark position
cfis.mark(Integer.MAX_VALUE);
// read the next 3 bytes
assertEquals(testData[3], cfis.read());
assertEquals(testData[4], cfis.read());
assertEquals(testData[5], cfis.read());
// reset position to the mark
cfis.reset();
// attempt to reread the last 3 bytes from the mark (from the cache)
assertEquals(testData[3], cfis.read());
assertEquals(testData[4], cfis.read());
assertEquals(testData[5], cfis.read());
// read the next 2 bytes past the reset mark (past the cache, e.g. from src)
assertEquals(testData[6], cfis.read());
assertEquals(testData[7], cfis.read());
// reset position to the mark
cfis.reset();
// attempt to read the last 5 bytes (from the cache)
assertEquals(testData[3], cfis.read());
assertEquals(testData[4], cfis.read());
assertEquals(testData[5], cfis.read());
assertEquals(testData[6], cfis.read());
assertEquals(testData[7], cfis.read());
// mark position
cfis.mark(-1);
// read the next 2 bytes past the reset mark (past the cache, e.g. from src)
assertEquals(testData[8], cfis.read());
assertEquals(testData[9], cfis.read());
// reset position to the mark
cfis.reset();
// attempt to reread the last 2 bytes from the mark (from the cache)
assertEquals(testData[8], cfis.read());
assertEquals(testData[9], cfis.read());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method skip.
@Test
public void skip() 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 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());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest 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();
InputStream is = new UnsynchronizedByteArrayInputStream(testData);
CachingFilterInputStream cfis = new CachingFilterInputStream(getNewCache(is));
cfis.mark(Integer.MAX_VALUE);
// read first two bytes from stream
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
byte[] endOfStreamResult = new byte[testData.length + 1];
read = cfis.read(endOfStreamResult);
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);
}
Aggregations