Example usage for io.netty.buffer ByteBuf forEachByte

List of usage examples for io.netty.buffer ByteBuf forEachByte

Introduction

In this page you can find the example usage for io.netty.buffer ByteBuf forEachByte.

Prototype

public abstract int forEachByte(ByteProcessor processor);

Source Link

Document

Iterates over the readable bytes of this buffer with the specified processor in ascending order.

Usage

From source file:org.codice.alliance.video.stream.mpegts.netty.StartCodeTest.java

License:Open Source License

@Test
public void testCodeDoesntExist2() {
    ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 0x04, 0x05, 0x00, 0x00, 0x02, 0x42 });

    int position = byteBuf.forEachByte(startCode);

    assertThat(position, is(-1));/*w  ww.ja  va2  s . c o  m*/

}

From source file:org.codice.alliance.video.stream.mpegts.netty.StartCodeTest.java

License:Open Source License

@Test
public void testEOF() {
    ByteBuf byteBuf = Unpooled.wrappedBuffer(new byte[] { 0x04, 0x05, 0x00, 0x00, 0x01 });

    int position = byteBuf.forEachByte(startCode);

    assertThat(position, is(-1));/*from  w  w w . j av  a 2 s.  co m*/

}

From source file:org.graylog.collector.file.splitters.NewlineChunkSplitter.java

License:Open Source License

@Override
public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) {
    return new Iterable<String>() {
        @Override/*from ww  w. j a  v  a2 s . c  o m*/
        public Iterator<String> iterator() {
            return new AbstractIterator<String>() {

                @Override
                protected String computeNext() {
                    try {
                        if (!buffer.isReadable()) {
                            return endOfData();
                        }
                        final int i = buffer.forEachByte(ByteBufProcessor.FIND_CRLF);
                        if (i == -1) {
                            if (includeRemainingData) {
                                final ByteBuf remaining = buffer.readBytes(buffer.readableBytes());
                                return remaining.toString(charset);
                            } else {
                                return endOfData();
                            }
                        }
                        final ByteBuf fullLine = buffer.readBytes(i);
                        // Strip the \r/\n bytes from the buffer.
                        final byte readByte = buffer.readByte(); // the \r or \n byte
                        if (readByte == '\r') {
                            buffer.readByte(); // the \n byte if previous was \r
                        }
                        return fullLine.toString(charset);
                    } finally {
                        buffer.discardReadBytes();
                    }
                }
            };
        }
    };
}

From source file:org.graylog2.inputs.syslog.tcp.SyslogOctetCountFrameDecoder.java

License:Open Source License

/**
 * Find the byte length of the frame length value.
 *
 * @param buffer The channel buffer//from  w ww .  j  a v  a 2  s  .  c  om
 * @return The length of the frame length value
 */
private int findFrameSizeValueLength(final ByteBuf buffer) {
    final int readerIndex = buffer.readerIndex();
    int index = buffer.forEachByte(BYTE_PROCESSOR);

    if (index >= 0) {
        return index - readerIndex;
    } else {
        return -1;
    }
}

From source file:org.opendaylight.controller.netconf.ssh.threads.Handshaker.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws IOException {
    ByteBuf bb = (ByteBuf) msg;
    // we can block the server here so that slow client does not cause memory pressure
    try {//w  w  w . j  a  va2s .  c om
        bb.forEachByte(new ByteBufProcessor() {
            @Override
            public boolean process(byte value) throws Exception {
                remoteOutputStream.write(value);
                return true;
            }
        });
    } finally {
        bb.release();
    }
}

From source file:ratpack.sse.internal.ServerSentEventDecoder.java

License:Apache License

private static int scanAndFindColon(ByteBuf byteBuf) {
    return byteBuf.forEachByte(SCAN_COLON_PROCESSOR);
}

From source file:ratpack.sse.internal.ServerSentEventDecoder.java

License:Apache License

private static int scanAndFindEndOfLine(ByteBuf byteBuf) {
    return byteBuf.forEachByte(SCAN_EOL_PROCESSOR);
}

From source file:ratpack.sse.internal.ServerSentEventDecoder.java

License:Apache License

private static boolean skipTillMatching(ByteBuf byteBuf, ByteBufProcessor processor) {
    final int lastIndexProcessed = byteBuf.forEachByte(processor);
    if (-1 == lastIndexProcessed) {
        byteBuf.readerIndex(byteBuf.readerIndex() + byteBuf.readableBytes()); // If all the remaining bytes are to be ignored, discard the buffer.
    } else {/* w  w  w. j  a  va  2 s  .c  om*/
        byteBuf.readerIndex(lastIndexProcessed);
    }
    return -1 != lastIndexProcessed;
}

From source file:reactor.net.tcp.TcpServerTests.java

License:Open Source License

@Test
public void exposesNettyByteBuf() throws InterruptedException {
    final int port = SocketUtils.findAvailableTcpPort();
    final CountDownLatch latch = new CountDownLatch(msgs);

    TcpServer<ByteBuf, ByteBuf> server = new TcpServerSpec<ByteBuf, ByteBuf>(NettyTcpServer.class).env(env)
            .listen(port).dispatcher(SynchronousDispatcher.INSTANCE)
            .consume(new Consumer<NetChannel<ByteBuf, ByteBuf>>() {
                @Override/* w  w w . j a  v  a 2  s. co  m*/
                public void accept(NetChannel<ByteBuf, ByteBuf> ch) {
                    ch.consume(new Consumer<ByteBuf>() {
                        @Override
                        public void accept(ByteBuf byteBuf) {
                            byteBuf.forEachByte(new ByteBufProcessor() {
                                @Override
                                public boolean process(byte value) throws Exception {
                                    if (value == '\n') {
                                        latch.countDown();
                                    }
                                    return true;
                                }
                            });
                            byteBuf.release();
                        }
                    });
                }
            }).get();

    log.info("Starting raw server on tcp://localhost:{}", port);
    server.start().await();

    for (int i = 0; i < threads; i++) {
        threadPool.submit(new DataWriter(port));
    }

    try {
        assertTrue("Latch was counted down", latch.await(10, TimeUnit.SECONDS));
    } finally {
        server.shutdown().await();
    }

}

From source file:tonivade.redis.protocol.RedisDecoder.java

License:Open Source License

private static int findEndOfLine(final ByteBuf buffer) {
    int i = buffer.forEachByte(ByteBufProcessor.FIND_LF);
    if (i > 0 && buffer.getByte(i - 1) == '\r') {
        i--;// ww  w . j  a va2  s  .co m
    }
    return i;
}