/** * {@inheritDoc} */ @Override public void write(byte[] b, int off, int len) throws IOException { queue.put(b, off, len); } }
/** * {@inheritDoc} */ @Override public void write(int b) throws IOException { queue.put((byte) b); }
@Test public void putOneByteAndHasRemaining() { ByteBufferQueue queue = new ByteBufferQueue(100); ByteBuffer src = ByteBuffer.allocate(1); src.put((byte) 0); src.flip(); queue.put(src); assertThat(queue.hasRemaining(), is(true)); }
@Test public void putEmptyBufferAndStillEmpty() { ByteBufferQueue queue = new ByteBufferQueue(100); ByteBuffer src = ByteBuffer.allocate(1); src.flip(); queue.put(src); assertThat(queue.hasRemaining(), is(false)); }
@Test public void putGetDrainsQueue() { ByteBufferQueue queue = new ByteBufferQueue(100); ByteBuffer src = ByteBuffer.allocate(1); src.put((byte) 0); src.flip(); queue.put(src); queue.get(ByteBuffer.allocate(2)); assertThat(queue.hasRemaining(), is(false)); }
@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 readLimit() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); FastByteBufferQueueInputStream instance = new FastByteBufferQueueInputStream(queue, 10); assertThat(read(instance, 10), is("AbCdEfGhIj")); }
@Test public void readSome() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); FastByteBufferQueueInputStream instance = new FastByteBufferQueueInputStream(queue,26); 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)); }
@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 readAll() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); FastByteBufferQueueInputStream instance = new FastByteBufferQueueInputStream(queue,26); assertThat(read(instance), is(str)); }
@Test public void markRead() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); FastByteBufferQueueInputStream instance = new FastByteBufferQueueInputStream(queue,26); assertThat(instance.markSupported(), is(false)); }
@Test public void readBytes() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); FastByteBufferQueueInputStream instance = new FastByteBufferQueueInputStream(queue,26); 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 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 readBytesOffset() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); queue.put(ByteBuffer.wrap(str.getBytes(StandardCharsets.UTF_8))); FastByteBufferQueueInputStream instance = new FastByteBufferQueueInputStream(queue,26); 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 putGetByteAndHasRemaining() { ByteBufferQueue queue = new ByteBufferQueue(100); for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { byte b = (byte)i; queue.put(b); assertThat(queue.hasRemaining(), is(true)); assertThat(queue.get(), is(b)); assertThat(queue.hasRemaining(), is(false)); } }
@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 putGetByteSequences() { ByteBufferQueue queue = new ByteBufferQueue(100); for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { byte b = (byte)i; queue.put(b); assertThat(queue.hasRemaining(), is(true)); } for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { byte b = (byte) i; assertThat(queue.hasRemaining(), is(true)); assertThat(queue.get(), is(b)); } assertThat(queue.hasRemaining(), is(false)); }
@Test public void putGetByteArraySequences() { ByteBufferQueue queue = new ByteBufferQueue(100); byte[] dst = new byte[1]; for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { byte b = (byte)i; queue.put(b); assertThat(queue.hasRemaining(), is(true)); } for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) { byte b = (byte) i; assertThat(queue.hasRemaining(), is(true)); assertThat(queue.get(dst, 0, 1), is(1)); assertThat(dst[0], is(b)); } assertThat(queue.hasRemaining(), is(false)); }