createTestStream(5);
final StreamFinishedListener listener = new TestStreamFinishedListener();
byte[] array = new byte[5];
// Try it with a regular read
LimitedLengthInputStream in = new LimitedLengthInputStream(wrappedStream, limit, listener);
try {
assertEquals(limit, in.getLength());
assertFalse(callbackCalled);
assertEquals(5, in.available());
for (int i = 0; i < 5; i++) {
assertEquals(i, in.read());
assertFalse(callbackCalled);
assertEquals(4 - i, in.available());
}
assertEquals(-1, in.read());
assertTrue(callbackCalled);
assertEquals(0, in.available());
callbackCalled = false;
assertEquals(-1, in.read());
assertFalse(callbackCalled);
assertEquals(0, in.available());
assertEquals(limit, in.getLength());
} finally {
in.close();
}
// Try it with the array read
createTestStream(5);
in = new LimitedLengthInputStream(wrappedStream, limit, listener);
try {
assertFalse(callbackCalled);
assertEquals(5, in.available());
assertEquals(5, in.read(array));
for (int i = 0; i < 5; i++) {
assertEquals(i, array[i]);
}
assertFalse(callbackCalled);
assertEquals(0, in.available());
assertEquals(-1, in.read(array));
assertTrue(callbackCalled);
assertEquals(0, in.available());
callbackCalled = false;
assertEquals(-1, in.read(array));
assertFalse(callbackCalled);
assertEquals(0, in.available());
} finally {
in.close();
}
// Try it with the array+pos+length read (aligned with actual underlying
// bytes)
createTestStream(5);
in = new LimitedLengthInputStream(wrappedStream, limit, listener);
try {
assertFalse(callbackCalled);
assertEquals(5, in.available());
assertEquals(5, in.read(array, 0, array.length));
for (int i = 0; i < 5; i++) {
assertEquals(i, array[i]);
}
assertFalse(callbackCalled);
assertEquals(0, in.available());
assertEquals(-1, in.read(array, 0, array.length));
assertTrue(callbackCalled);
assertEquals(0, in.available());
callbackCalled = false;
assertEquals(-1, in.read(array, 0, array.length));
assertFalse(callbackCalled);
assertEquals(0, in.available());
} finally {
in.close();
}
// Try it with the array+pos+length read (not aligned with actual
// underlying bytes)
array = new byte[10];
createTestStream(5);
in = new LimitedLengthInputStream(wrappedStream, limit, listener);
try {
assertFalse(callbackCalled);
assertEquals(5, in.available());
assertEquals(5, in.read(array, 0, array.length));
for (int i = 0; i < 5; i++) {
assertEquals(i, array[i]);
}
assertFalse(callbackCalled);
assertEquals(0, in.available());
assertEquals(-1, in.read(array, 0, array.length));
assertTrue(callbackCalled);
assertEquals(0, in.available());
callbackCalled = false;
assertEquals(-1, in.read(array, 0, array.length));
assertFalse(callbackCalled);
assertEquals(0, in.available());
} finally {
in.close();
}
// Try it with skip
createTestStream(5);
in = new LimitedLengthInputStream(wrappedStream, limit, listener);
try {
assertFalse(callbackCalled);
assertEquals(5, in.available());
assertEquals(5, in.skip(5));
assertFalse(callbackCalled);
assertEquals(0, in.available());
assertEquals(0, in.skip(1));
assertFalse(callbackCalled); // The contract of skip() doesn't let
// us definitively say that we're at
// the EOF, so the callback won't get
// called until we read
assertEquals(-1, in.read());
assertEquals(0, in.available());
assertTrue(callbackCalled);
callbackCalled = false;
assertEquals(0, in.skip(1));
assertFalse(callbackCalled);
assertEquals(0, in.available());
} finally {
in.close();
}
}