UnsafeByteArrayInputStream extends InputStream { @Override public int read() { return (mPosition < mLimit) ? (mData[mPosition++] & 0xff) : -1; } UnsafeByteArrayInputStream(byte buf[]);  UnsafeByteArrayInputStream(byte buf[], int offset);  UnsafeByteArrayInputStream(byte buf[], int offset, int length); @Override int read(); @Override int read(byte b[], int off, int len); @Override long skip(long len); @Override int available(); @Override boolean markSupported(); @Override void mark(int readAheadLimit); @Override void reset(); @Override void close(); int position(); void position(int newPosition); int size();  }
@Test public void testRead() throws IOException { UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); assertThat(stream.read(), is((int) 'a')); assertThat(stream.available(), is(2)); stream.skip(1); assertThat(stream.available(), is(1)); byte[] bytes = new byte[1]; int read = stream.read(bytes); assertThat(read, is(1)); assertThat(bytes, is("c".getBytes())); stream.reset(); assertThat(stream.position(), is(0)); assertThat(stream.size(), is(3)); stream.position(1); assertThat(stream.read(), is((int) 'b')); }
@Test(expected = IndexOutOfBoundsException.class) public void testWrongLength() { UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); stream.read(new byte[1], 0, 100); }
@Test(expected = IndexOutOfBoundsException.class) public void testWrongOffset() { UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); stream.read(new byte[1], -1, 1); }
@Test(expected = NullPointerException.class) public void testReadEmptyByteArray() { UnsafeByteArrayInputStream stream = new UnsafeByteArrayInputStream("abc".getBytes()); stream.read(null, 0, 1); }