@Test public void markRead() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); ByteBufferQueueInputStream instance = new ByteBufferQueueInputStream(queue); assumeThat(instance.markSupported(), is(true)); instance.mark(4); instance.read(new byte[3]); instance.reset(); assertThat(read(instance), is(str)); }
@Test public void skipRead() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); ByteBufferQueueInputStream instance = new ByteBufferQueueInputStream(queue); StringBuffer buf = new StringBuffer(); int b; do { if (instance.skip(1) != 1) { b = -1; } else { b = instance.read(); if (b != -1) { buf.append((char) b); } } } while (b != -1); assertThat(buf.toString(), is("bdfhjlnprtvxz")); }
@Test public void readBytes() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); ByteBufferQueueInputStream instance = new ByteBufferQueueInputStream(queue); byte[] bytes = new byte[10]; assertThat(instance.read(bytes), is(10)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("AbCdEfGhIj")); assertThat(instance.read(bytes), is(10)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("KlMnOpQrSt")); assertThat(instance.read(bytes), is(6)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("UvWxYzQrSt")); }
@Test public void readSome() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); ByteBufferQueueInputStream instance = new ByteBufferQueueInputStream(queue); assertThat(read(instance, 10), is("AbCdEfGhIj")); }
@Test public void readBytesOffset() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); ByteBufferQueueInputStream instance = new ByteBufferQueueInputStream(queue); byte[] bytes = new byte[10]; assertThat(instance.read(bytes,5,3), is(3)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("\u0000\u0000\u0000\u0000\u0000AbC\u0000\u0000")); assertThat(instance.read(bytes, 0, 2), is(2)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("dE\u0000\u0000\u0000AbC\u0000\u0000")); assertThat(instance.read(bytes, 2, 8), is(8)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("dEfGhIjKlM")); assertThat(instance.read(bytes, 2, 8), is(8)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("dEnOpQrStU")); assertThat(instance.read(bytes, 2, 8), is(5)); assertThat(new String(bytes, StandardCharsets.UTF_8), is("dEvWxYzStU")); }
@Test public void readLimit() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); ByteBufferQueueInputStream instance = new ByteBufferQueueInputStream(queue, 10); assertThat(read(instance, 10), is("AbCdEfGhIj")); }
@Test public void readAll() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); ByteBufferQueueInputStream instance = new ByteBufferQueueInputStream(queue); assertThat(read(instance), is(str)); }