Java Code Examples for io.netty.buffer.ByteBuf.forEachByte()

The following are Jave code examples for showing how to use forEachByte() of the io.netty.buffer.ByteBuf class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: Re-Collector   File: NewlineChunkSplitter.java   Source Code and License Vote up 5 votes
@Override
public Iterable<String> split(final ByteBuf buffer, final Charset charset, final boolean includeRemainingData) {
    return () -> new AbstractIterator<String>() {
        @Override
        protected String computeNext() {
            ByteBuf fullLine = null;
            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();
                    }
                }
                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();
                if (fullLine != null) {
                    fullLine.release();
                }

            }
        }
    };
}
 
Example 2
Project: ss-java   File: AbstractBouncycastleCrypto.java   Source Code and License Vote up 5 votes
@Override
protected ByteBuf process(final StreamCipher cipher, ByteBuf data) {
    final ByteBuf slice = data.slice();
    slice.writerIndex(0);
    data.forEachByte(data.readerIndex(), data.readableBytes(), new ByteBufProcessor() {
        @Override
        public boolean process(byte b) throws Exception {
            slice.writeByte(cipher.returnByte(b));
            return true;
        }
    });
    return data;
}
 
Example 3
Project: NioImapClient   File: BaseStringParser.java   Source Code and License Vote up 5 votes
public String parse(ByteBuf buffer) {
  seq = sequenceReference.get();

  seq.reset();
  size = 0;
  int i = buffer.forEachByte(this);
  buffer.readerIndex(i + 1);

  String result = seq.toString();
  seq = null;

  return result;
}
 
Example 4
Project: NioImapClient   File: NumberParser.java   Source Code and License Vote up 5 votes
public long parse(ByteBuf in) {
  seq = sequenceRef.get();

  seq.reset();
  size = 0;
  int i = in.forEachByte(this);
  in.readerIndex(i);

  Long result = Long.parseLong(seq.toString());
  seq = null;

  return result;
}
 
Example 5
Project: NioSmtpClient   File: DotStuffing.java   Source Code and License Vote up 5 votes
private static int findDotAtBeginningOfLine(ByteBuf buffer, int startAt, byte[] previousBytes) {
  int length = buffer.readableBytes();

  if (previousBytes[0] == CR && previousBytes[1] == LF && buffer.getByte(startAt) == DOT) {
    return startAt;
  }

  if (previousBytes[1] == CR && length >= 2 && buffer.getByte(startAt) == LF && buffer.getByte(startAt + 1) == DOT) {
    return startAt + 1;
  }

  int i = startAt;
  while (++i < length) {
    i = buffer.forEachByte(i, length - i, ByteProcessor.FIND_LF);
    if (i == -1) {
      return -1;
    }

    if (buffer.getByte(i - 1) == CR) {
      if (i + 1 < length && buffer.getByte(i + 1) == DOT) {
        return i + 1;
      } else {
        continue;
      }
    }
  }

  return -1;
}
 
Example 6
Project: NettyStudy   File: ByteBufExamples.java   Source Code and License Vote up 4 votes
/**
 * Listing 5.9
 */
public static void byteBufProcessor(ByteBuf buffer) {
    int index = buffer.forEachByte(ByteBufProcessor.FIND_CR);
}
 
Example 7
Project: reactive-pg-client   File: MessageDecoder.java   Source Code and License Vote up 4 votes
int parse(ByteBuf in) {
  afterSpace = false;
  rows = 0;
  in.forEachByte(in.readerIndex(), in.readableBytes() - 1, this);
  return rows;
}