Code example for ByteArrayInputStream

0
import static org.junit.Assert.fail; 
 
public class MarkableInputStreamTest { 
  @Test 
  public void test() throws Exception { 
    MarkableInputStream in = new MarkableInputStream(new ByteArrayInputStream(
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ".getBytes(Charset.forName("US-ASCII"))));
    assertThat(readBytes(in, 3)).isEqualTo("ABC");
    long posA = in.savePosition(7);// DEFGHIJ
    assertThat(readBytes(in, 4)).isEqualTo("DEFG");
    in.mark(5); // HIJKL
    assertThat(readBytes(in, 4)).isEqualTo("HIJK");
    in.reset(); // Back to 'H'
    assertThat(readBytes(in, 3)).isEqualTo("HIJ");
    in.reset(posA); // Back to 'D'
    assertThat(readBytes(in, 7)).isEqualTo("DEFGHIJ");
    in.reset(); // Back to 'H' again.
    assertThat(readBytes(in, 6)).isEqualTo("HIJKLM");
    try { 
      in.reset();
      fail();