use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method available_onOffsetPartiallyCachedStream.
@Test
public void available_onOffsetPartiallyCachedStream() 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 first 2 bytes
cfis.read();
cfis.read();
// mark for later reset
cfis.mark(Integer.MAX_VALUE);
// read next 2 bytes
cfis.read();
cfis.read();
// return to the start of the stream
cfis.reset();
assertEquals(testData.length - 2, cfis.available());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method available_onUnCachedStream.
@Test
public void available_onUnCachedStream() 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.length, cfis.available());
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method readByte_pastEndOfStream.
@Test
public void readByte_pastEndOfStream() 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 all the bytes upto end of stream
int b = -1;
int testDataOffset = 0;
while ((b = cfis.read()) > -1) {
assertEquals(testData[testDataOffset++], b);
}
// read byte past end of stream
b = cfis.read();
assertEquals(-1, b);
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method readBytes_partFromCache.
@Test
public void readBytes_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);
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);
}
use of org.apache.commons.io.input.UnsynchronizedByteArrayInputStream in project exist by eXist-db.
the class CachingFilterInputStreamTest method readBytes_pastEndOfStream.
@Test
public void readBytes_pastEndOfStream() 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));
byte[] result = new byte[testData.length];
int read = cfis.read(result);
assertEquals(testData.length, read);
assertArrayEquals(testData, result);
byte[] endOfStreamResult = new byte[testData.length];
read = cfis.read(endOfStreamResult);
assertEquals(-1, read);
assertArrayEquals(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, endOfStreamResult);
}
Aggregations