UnsafeStringReader extends Reader { @Override public int read() throws IOException { ensureOpen(); if (mPosition >= mLimit) { return -1; } return mString.charAt(mPosition++); } UnsafeStringReader(String str); @Override int read(); @Override int read(char[] cs, int off, int len); @Override long skip(long ns); @Override boolean ready(); @Override boolean markSupported(); @Override void mark(int readAheadLimit); @Override void reset(); @Override void close();  }
@Test public void testRead() throws IOException { UnsafeStringReader reader = new UnsafeStringReader("abc"); assertThat(reader.markSupported(), is(true)); assertThat(reader.read(), is((int) 'a')); assertThat(reader.read(), is((int) 'b')); assertThat(reader.read(), is((int) 'c')); assertThat(reader.read(), is(-1)); reader.reset(); reader.mark(0); assertThat(reader.read(), is((int) 'a')); char[] chars = new char[2]; reader.read(chars); reader.close(); assertThat(chars[0], is('b')); assertThat(chars[1], is('c')); }
@Test public void testWrongLength() throws IOException { Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { UnsafeStringReader reader = new UnsafeStringReader("abc"); char[] chars = new char[1]; reader.read(chars, 0, 2); }); }