@Test public void skipRemaining_shouldExhaustStream() throws IOException { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); fixedLengthInputStream.skipRemaining(); assertInputStreamExhausted(fixedLengthInputStream); }
@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_withOverSizedByteArrayInMiddleOfStream_shouldNotConsumeMoreThanLimitFromUnderlyingStream() throws Exception { InputStream inputStream = inputStream("Hello World"); FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream, 6); consumeBytes(fixedLengthInputStream, 5); //noinspection ResultOfMethodCallIgnored fixedLengthInputStream.read(new byte[10]); assertRemainingInputStreamEquals("World", inputStream); }
@Test public void skip_shouldConsumeBytes() throws IOException { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); guaranteedSkip(fixedLengthInputStream, 2); assertRemainingInputStreamEquals("llo", fixedLengthInputStream); }
@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_afterPartialReadArray() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); consumeBytes(fixedLengthInputStream, 2); int available = fixedLengthInputStream.available(); assertEquals(3, available); }
@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); }
@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_afterSkip() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); guaranteedSkip(fixedLengthInputStream, 2); int available = fixedLengthInputStream.available(); assertEquals(3, available); }
@Test public void read_atStartOfStream() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Word"), 2); int readByte = fixedLengthInputStream.read(); assertEquals('W', (char) readByte); }
private void assertRemainingInputStreamEquals(String expected, InputStream inputStream) throws IOException { assertEquals(expected, readStreamAsUtf8String(inputStream)); }
@Test public void readArray_atEndOfStream_shouldReturnMinusOne() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello world"), 5); exhaustStream(fixedLengthInputStream); int numberOfBytesRead = fixedLengthInputStream.read(new byte[2]); assertEquals(-1, numberOfBytesRead); }
@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 readingStream_shouldReturnDataUpToLimit() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello world"), 5); String readString = readStreamAsUtf8String(fixedLengthInputStream); assertEquals("Hello", readString); }
@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 available_atStartOfStream() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); int available = fixedLengthInputStream.available(); assertEquals(5, available); }
@Test public void read_atEndOfStream_shouldReturnMinusOne() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello world"), 5); exhaustStream(fixedLengthInputStream); int readByte = fixedLengthInputStream.read(); assertEquals(-1, readByte); }
@Test public void read_withOverSizedByteArrayInMiddleOfStream_shouldReturnDataUpToLimit() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 6); consumeBytes(fixedLengthInputStream, 5); byte[] data = new byte[10]; int numberOfBytesRead = fixedLengthInputStream.read(data); assertEquals(1, numberOfBytesRead); assertEquals(" ", ByteString.of(data, 0, numberOfBytesRead).utf8()); }
@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_afterStreamHasBeenExhausted() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 5); exhaustStream(fixedLengthInputStream); int available = fixedLengthInputStream.available(); assertEquals(0, available); }