private void processCommand() throws IOException { try { FastByteBufferQueueInputStream is = new FastByteBufferQueueInputStream(receiveQueue, readCommandSizes[0]); try { Command cmd = Command.readFrom(channel, is, readCommandSizes[0]); LOGGER.log(Level.WARNING, "Failed to construct Command in channel " + channel.getName(), e); } finally { int available = is.available(); if (available > 0) { if (is.skip(available) != available) {
@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 skipRead() 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); 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))); 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 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 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 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))); FastByteBufferQueueInputStream instance = new FastByteBufferQueueInputStream(queue,26); assertThat(read(instance), is(str)); }