BlockConsumingInputStream extends InputStream { @Override public int read() throws IOException { if (positionInBuffer < buffer.length) { return buffer[positionInBuffer++] & 0xff; } if (nextBlockToRead < numBlocks) { boolean reloaded = refillBuffer(); if (!reloaded) { return -1; } return buffer[positionInBuffer++] & 0xff; } return -1; } private  BlockConsumingInputStream(BlockGetter blockGetter, long numBlocks, int blocksInMemory); static BlockConsumingInputStream create(
            BlockGetter blockGetter,
            long numBlocks,
            int blocksInMemory); @Override int read(); @Override int read(byte[] bytes, int off, int len);  }
@Test(expected = NullPointerException.class) public void cantReadToNullArray() throws IOException { dataStream.read(null, 1, 1); }
@Test(expected = IndexOutOfBoundsException.class) public void cantReadToNegativePlace() throws IOException { dataStream.read(data, -1, 1); }
@Test(expected = IndexOutOfBoundsException.class) public void cantReadNegativeAmount() throws IOException { dataStream.read(data, 0, -1); }
@Test(expected = IndexOutOfBoundsException.class) public void cantReadMoreThanArrayLength() throws IOException { dataStream.read(data, 0, 10); }
@Test public void canReadSingleByte() throws IOException { int byteAsInt = dataStream.read(); byte[] readByte = { (byte) byteAsInt }; assertEquals("d", new String(readByte, StandardCharsets.UTF_8)); }
@Test public void readEmptyArrayReturnsZero() throws IOException { int read = dataStream.read(new byte[0]); assertEquals(0, read); }
@Test public void canReadBlock() throws IOException { byte[] result = new byte[DATA_SIZE]; int read = dataStream.read(result); assertEquals(DATA_SIZE, read); assertArrayEquals(data, result); }
@Test public void readSingleByteWhenStreamExhaustedReturnsMinusOne() throws IOException { dataStream.read(new byte[DATA_SIZE]); int read = dataStream.read(); assertEquals(-1, read); }
@Test public void readWhenStreamExhaustedReturnsMinusOne() throws IOException { byte[] result = new byte[DATA_SIZE]; dataStream.read(result); int read = dataStream.read(result); assertEquals(-1, read); }
@Test public void largerArraysThanDataGetPartiallyFilled() throws IOException { byte[] result = new byte[DATA_SIZE_PLUS_ONE]; int read = dataStream.read(result); assertEquals(DATA_SIZE, read); assertArrayEquals(data, Arrays.copyOf(result, DATA_SIZE)); }