Java Code Examples for io.vertx.core.buffer.Buffer#slice()

The following examples show how to use io.vertx.core.buffer.Buffer#slice() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BufferWrappingBytes.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
BufferWrappingBytes(Buffer buffer, int offset, int length) {
  checkArgument(length >= 0, "Invalid negative length");
  int bufferLength = buffer.length();
  checkElementIndex(offset, bufferLength + 1);
  checkArgument(
      offset + length <= bufferLength,
      "Provided length %s is too big: the buffer has size %s and has only %s bytes from %s",
      length,
      bufferLength,
      bufferLength - offset,
      offset);

  if (offset == 0 && length == bufferLength) {
    this.buffer = buffer;
  } else {
    this.buffer = buffer.slice(offset, offset + length);
  }
}
 
Example 2
Source File: BufferWrappingBytes.java    From cava with Apache License 2.0 6 votes vote down vote up
BufferWrappingBytes(Buffer buffer, int offset, int length) {
  checkArgument(length >= 0, "Invalid negative length");
  int bufferLength = buffer.length();
  checkElementIndex(offset, bufferLength + 1);
  checkArgument(
      offset + length <= bufferLength,
      "Provided length %s is too big: the buffer has size %s and has only %s bytes from %s",
      length,
      bufferLength,
      bufferLength - offset,
      offset);

  if (offset == 0 && length == bufferLength) {
    this.buffer = buffer;
  } else {
    this.buffer = buffer.slice(offset, offset + length);
  }
}
 
Example 3
Source File: TcpParser.java    From servicecomb-java-chassis with Apache License 2.0 5 votes vote down vote up
protected void onParse(Buffer buffer) {
  switch (status) {
    case TCP_HEADER:
      ByteBuf buf = buffer.getByteBuf();
      if (!firstNEqual(TCP_MAGIC, buf.array(), TCP_MAGIC.length)) {
        reset();
        return;
      }

      buf.skipBytes(TCP_MAGIC.length);
      msgId = buf.readLong();
      totalLen = buf.readInt();
      headerLen = buf.readInt();
      if (headerLen > totalLen || headerLen <= 0 || totalLen > TCP_MAX_REQUEST_LENGTH) {
        throw new IllegalStateException("possibly attack.");
      }

      if (totalLen == 0) {
        onReadOnePackage(null, null);
        return;
      }

      parser.fixedSizeMode(totalLen);
      status = ParseStatus.TCP_PAYLOAD;
      break;

    case TCP_PAYLOAD:
      Buffer headerBuffer = buffer.slice(0, headerLen);
      Buffer bodyBuffer = buffer.slice(headerLen, buffer.length());
      onReadOnePackage(headerBuffer, bodyBuffer);
      break;

    default:
      break;
  }
}