@Override public int read(byte[] b) throws IOException { return read(b, 0, b.length); }
@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 read_atStartOfStream() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Word"), 2); int readByte = fixedLengthInputStream.read(); assertEquals('W', (char) readByte); }
@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 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 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_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_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 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_withOverSizedByteArray_shouldReturnDataUpToLimit() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 6); byte[] data = new byte[100]; int numberOfBytesRead = fixedLengthInputStream.read(data); assertEquals(6, numberOfBytesRead); assertEquals("Hello ", ByteString.of(data, 0, numberOfBytesRead).utf8()); }
@Test public void read_withByteArraySmallerThanLimit_shouldConsumeSizeOfByteArray() throws Exception { FixedLengthInputStream fixedLengthInputStream = new FixedLengthInputStream(inputStream("Hello World"), 6); byte[] data = new byte[5]; int numberOfBytesRead = fixedLengthInputStream.read(data); assertEquals(5, numberOfBytesRead); assertEquals("Hello", ByteString.of(data).utf8()); }
@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()); }