@Test public void available_afterSkipRemaining() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); fixedLengthInputStream.skipRemaining(); int available = fixedLengthInputStream.available(); assertEquals(0, available); }
@Test public void available_afterPartialRead() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); //noinspection ResultOfMethodCallIgnored fixedLengthInputStream.read(); int available = fixedLengthInputStream.available(); assertEquals(4, available); }
@Test public void read_atStartOfStream() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Word"), 2); int readByte = fixedLengthInputStream.read(); assertEquals('W', (char) readByte); }
@Test public void available_atStartOfStream() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); int available = fixedLengthInputStream.available(); assertEquals(5, available); }
@Test public void skipRemaining_shouldExhaustStream() throws IOException { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); fixedLengthInputStream.skipRemaining(); assertInputStreamExhausted(fixedLengthInputStream); }
@Test //TODO: Maybe this should throw. The underlying stream delivering less bytes than expected is most likely an error. public void readingStream_withLimitGreaterThanNumberOfBytesInUnderlyingInputStream() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 100); String readString = readStreamAsUtf8String(fixedLengthInputStream); assertEquals("Hello World", readString); }
@Override public long skip(long n) throws IOException { long d = mIn.skip(Math.min(n, available())); if (d > 0) { mCount += d; } return d; }
@Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
@Override public Object foundLiteral(ImapResponse response, FixedLengthInputStream literal) throws Exception { foundLiteralCalled = true; int skipBytes = readNumberOfBytes; while (skipBytes > 0) { long skippedBytes = literal.skip(skipBytes); skipBytes -= skippedBytes; } if (throwException) { throw new ImapResponseParserTestException(exceptionCount++); } return returnValue; } }
@Test public void read_withOverSizedByteArray_shouldNotConsumeMoreThanLimitFromUnderlyingStream() throws Exception { InputStream inputStream = inputStream("Hello World"); FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream, 6); //noinspection ResultOfMethodCallIgnored fixedLengthInputStream.read(new byte[100]); assertRemainingInputStreamEquals("World", inputStream); }
@Test public void available_afterPartialReadArray() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); consumeBytes(fixedLengthInputStream, 2); int available = fixedLengthInputStream.available(); assertEquals(3, available); }
@Test public void skipRemaining_shouldNotConsumeMoreThanLimitFromUnderlyingInputStream() throws IOException { InputStream inputStream = inputStream("Hello World"); FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream, 6); fixedLengthInputStream.skipRemaining(); assertRemainingInputStreamEquals("World", inputStream); }
@Test public void readingStream_shouldReturnDataUpToLimit() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello world"), 5); String readString = readStreamAsUtf8String(fixedLengthInputStream); assertEquals("Hello", readString); }
@Test public void readArrayWithOffset_atEndOfStream_shouldReturnMinusOne() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello world"), 5); exhaustStream(fixedLengthInputStream); int numberOfBytesRead = fixedLengthInputStream.read(new byte[2], 0, 2); assertEquals(-1, numberOfBytesRead); }
@Test public void available_afterSkip() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); guaranteedSkip(fixedLengthInputStream, 2); int available = fixedLengthInputStream.available(); assertEquals(3, available); }
FixedLengthInputStream fixed = new FixedLengthInputStream(inputStream, size); boolean someDataWasRead = fixed.available() != size; if (someDataWasRead) { if (result == null && callbackException == null) { fixed.skipRemaining();
@Test public void readingStream_shouldNotConsumeMoreThanLimitFromUnderlyingInputStream() throws Exception { InputStream inputStream = inputStream("Hello world"); FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream, 5); exhaustStream(fixedLengthInputStream); assertRemainingInputStreamEquals(" world", inputStream); }
@Test public void read_inMiddleOfStream() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Word"), 2); consumeBytes(fixedLengthInputStream, 1); int readByte = fixedLengthInputStream.read(); assertEquals('o', (char) readByte); }
@Test public void available_afterStreamHasBeenExhausted() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); exhaustStream(fixedLengthInputStream); int available = fixedLengthInputStream.available(); assertEquals(0, available); }