Example usage for io.netty.buffer ByteBufProcessor FIND_CRLF

List of usage examples for io.netty.buffer ByteBufProcessor FIND_CRLF

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufProcessor FIND_CRLF.

Prototype

ByteBufProcessor FIND_CRLF

To view the source code for io.netty.buffer ByteBufProcessor FIND_CRLF.

Click Source Link

Usage

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. ja  v  a2  s  .c om
        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();
                    }
                }
            };
        }
    };
}