### Question:
MessageReadSetIndexInputStream extends InputStream { @Override public int read() throws IOException { if (currentOffset >= messageReadSet.sizeInBytes(indexToRead)) { return -1; } ByteBuffer buf = ByteBuffer.allocate(1); ByteBufferOutputStream bufferStream = new ByteBufferOutputStream(buf); long bytesRead = messageReadSet.writeTo(indexToRead, Channels.newChannel(bufferStream), currentOffset, 1); if (bytesRead != 1) { throw new IllegalStateException("Number of bytes read for read from messageReadSet should be 1"); } currentOffset++; buf.flip(); return buf.get() & 0xFF; } MessageReadSetIndexInputStream(MessageReadSet messageReadSet, int indexToRead, int startingOffset); @Override int read(); @Override int read(byte b[], int off, int len);  }### Answer:
@Test public void messageReadSetIndexInputStreamTestException() throws Exception { ArrayList<ByteBuffer> listBuf = new ArrayList<ByteBuffer>(); byte[] buf = new byte[1024]; new Random().nextBytes(buf); listBuf.add(ByteBuffer.wrap(buf)); ArrayList<StoreKey> storeKeys = new ArrayList<StoreKey>(); storeKeys.add(new MockId("012345678910123223233456789012")); MessageReadSet readSet = new MockMessageReadSet(listBuf, storeKeys); MessageReadSetIndexInputStream stream = new MessageReadSetIndexInputStream(readSet, 0, 0); byte[] bufOutput = new byte[1024]; Assert.assertEquals("Should return 0 if length requested is 0", 0, stream.read(bufOutput, 0, 0)); Assert.assertEquals("Should return 0 if length requested is 0", 0, stream.read(bufOutput, 1, 0)); try { stream.read(bufOutput, -1, 10); Assert.fail("IndexOutOfBoundsException is expected."); } catch (IndexOutOfBoundsException e) { } try { stream.read(bufOutput, 0, -1); Assert.fail("IndexOutOfBoundsException is expected."); } catch (IndexOutOfBoundsException e) { } try { stream.read(bufOutput, 1, 1024); Assert.fail("IndexOutOfBoundsException is expected."); } catch (IndexOutOfBoundsException e) { } stream.read(bufOutput, 0, 1024); Assert.assertArrayEquals("Output doesn't match", bufOutput, buf); Assert.assertEquals("Should return -1 if no more data", -1, stream.read()); Assert.assertEquals("Should return -1 if no more data", -1, stream.read(bufOutput, 0, 1)); }