/** * SPI: Creates a new {@link ByteBuffer} optimally sized for network read/write operations. * * @return a new {@link ByteBuffer} optimally sized for network read/write operations. */ protected ByteBufferQueue newByteBufferQueue() { return new ByteBufferQueue(CAPACITY); }
/** * Constructor. * * @param ioHub the {@link IOHub} that we use. */ public NetworkLayer(@Nonnull IOHub ioHub) { this.ioHub = ioHub; this.recvQueue = new ByteBufferQueue(CAPACITY); this.sendQueue = new ByteBufferQueue(CAPACITY); }
@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 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 putEmptyBufferAndStillEmpty() { ByteBufferQueue queue = new ByteBufferQueue(100); ByteBuffer src = ByteBuffer.allocate(1); src.flip(); queue.put(src); 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 newInstanceIsEmpty() { assertThat(new ByteBufferQueue(100).hasRemaining(), is(false)); }
@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 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 writeBytes() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); ByteBufferQueueOutputStream instance = new ByteBufferQueueOutputStream(queue); for (int i = 1; i < str.length(); i += 2) { instance.write(str.charAt(i)); } assertThat(read(queue), is("bdfhjlnprtvxz")); }
@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 writeSome() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); ByteBufferQueueOutputStream instance = new ByteBufferQueueOutputStream(queue); instance.write(str.getBytes(StandardCharsets.UTF_8), 0, 10); assertThat(read(queue), is("AbCdEfGhIj")); }
@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 writeAll() throws Exception { String str = "AbCdEfGhIjKlMnOpQrStUvWxYz"; ByteBufferQueue queue = new ByteBufferQueue(10); ByteBufferQueueOutputStream instance = new ByteBufferQueueOutputStream(queue); instance.write(str.getBytes(StandardCharsets.UTF_8)); assertThat(read(queue), is(str)); }
@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)); }