io.netty.buffer.ByteBuf.toString()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.7k)|赞(0)|评价(0)|浏览(302)

本文整理了Java中io.netty.buffer.ByteBuf.toString()方法的一些代码示例,展示了ByteBuf.toString()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteBuf.toString()方法的具体详情如下:
包路径:io.netty.buffer.ByteBuf
类名称:ByteBuf
方法名:toString

ByteBuf.toString介绍

[英]Returns the string representation of this buffer. This method does not necessarily return the whole content of the buffer but returns the values of the key properties such as #readerIndex(), #writerIndex() and #capacity().
[中]返回此缓冲区的字符串表示形式。此方法不一定返回缓冲区的全部内容,但返回关键属性的值,如#readerIndex()、#writeridex()和#capacity()。

代码示例

代码示例来源:origin: redisson/redisson

@Override
  public Object decode(ByteBuf buf, State state) {
    String str = buf.toString(charset);
    buf.readerIndex(buf.readableBytes());
    return str;
  }
};

代码示例来源:origin: redisson/redisson

@Override
  public Object decode(ByteBuf buf, State state) {
    String str = buf.toString(charset);
    buf.readerIndex(buf.readableBytes());
    return str;
  }
};

代码示例来源:origin: wildfly/wildfly

static String readUsAscii(ByteBuf buffer, int length) {
    String s = buffer.toString(buffer.readerIndex(), length, CharsetUtil.US_ASCII);
    buffer.skipBytes(length);
    return s;
  }
}

代码示例来源:origin: jamesdbloom/mockserver

private boolean isHttp(ByteBuf msg) {
  String method = msg.toString(msg.readerIndex(), 8, StandardCharsets.US_ASCII);
  return method.startsWith("GET ") ||
    method.startsWith("POST ") ||
    method.startsWith("PUT ") ||
    method.startsWith("HEAD ") ||
    method.startsWith("OPTIONS ") ||
    method.startsWith("PATCH ") ||
    method.startsWith("DELETE ") ||
    method.startsWith("TRACE ") ||
    method.startsWith("CONNECT ");
}

代码示例来源:origin: traccar/traccar

public static boolean isBinary(ByteBuf buf) {
  String header = buf.toString(buf.readerIndex(), 4, StandardCharsets.US_ASCII);
  if (header.equals("+ACK")) {
    return buf.getByte(buf.readerIndex() + header.length()) != (byte) ':';
  } else {
    return BINARY_HEADERS.contains(header);
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

private void parseCloseFrame() {
 int length = length();
 if (length < 2) {
  closeStatusCode = 1000;
  closeReason = null;
 } else {
  int index = binaryData.readerIndex();
  closeStatusCode = binaryData.getShort(index);
  if (length == 2) {
   closeReason = null;
  } else {
   closeReason = binaryData.toString(index + 2, length - 2, StandardCharsets.UTF_8);
  }
 }
}

代码示例来源:origin: traccar/traccar

private static String readString(ByteBuf buf) {
  String result = null;
  int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0);
  if (index > buf.readerIndex()) {
    result = buf.readSlice(index - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
  }
  buf.readByte();
  return result;
}

代码示例来源:origin: traccar/traccar

@Override
protected Object decode(
    Channel channel, SocketAddress remoteAddress, Object msg) throws Exception {
  ByteBuf buf = (ByteBuf) msg;
  buf.skipBytes(3); // header
  buf.readUnsignedShort(); // length
  String sentence = buf.toString(buf.readerIndex(), buf.readableBytes() - 3, StandardCharsets.US_ASCII);
  buf.skipBytes(3); // footer
  return decodeMessage(channel, remoteAddress, sentence);
}

代码示例来源:origin: eclipse-vertx/vert.x

static Function<DnsRecord, String> address(int octets) {
  return record -> {
    ByteBuf data = ((DnsRawRecord)record).content();
    int size = data.readableBytes();
    if (size != octets) {
      throw new DecoderException("Invalid content length, or reader index when decoding address [index: "
        + data.readerIndex() + ", expected length: " + octets + ", actual: " + size + "].");
    }
    byte[] address = new byte[octets];
    data.getBytes(data.readerIndex(), address);
    try {
      return InetAddress.getByAddress(address).getHostAddress();
    } catch (UnknownHostException e) {
      throw new DecoderException("Could not convert address "
        + data.toString(data.readerIndex(), size, CharsetUtil.UTF_8) + " to InetAddress.");
    }
  };
}

代码示例来源:origin: eclipse-vertx/vert.x

if (pointer) {
  if (position == -1) {
    position = buf.readerIndex() + 1;
  buf.readerIndex((len & 0x3f) << 8 | buf.readUnsignedByte());
} else {
  name.append(buf.toString(buf.readerIndex(), len, CharsetUtil.UTF_8)).append(".");
  buf.skipBytes(len);

代码示例来源:origin: traccar/traccar

@Override
protected Object decode(
    ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {
  if (buf.readableBytes() >= MESSAGE_HEADER) {
    int length = Integer.parseInt(buf.toString(2, 2, StandardCharsets.US_ASCII)) + 5;
    if (buf.readableBytes() >= length) {
      ByteBuf frame = buf.readRetainedSlice(length);
      while (buf.isReadable() && buf.getUnsignedByte(buf.readerIndex()) != '$') {
        buf.readByte();
      }
      return frame;
    }
  }
  return null;
}

代码示例来源:origin: traccar/traccar

@Override
protected Object decode(
    ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {
  if (buf.readableBytes() < 10) {
    return null;
  }
  int index = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) ',');
  if (index != -1) {
    int length = index - buf.readerIndex() + Integer.parseInt(
        buf.toString(buf.readerIndex() + 3, index - buf.readerIndex() - 3, StandardCharsets.US_ASCII));
    if (buf.readableBytes() >= length) {
      return buf.readRetainedSlice(length);
    }
  }
  return null;
}

代码示例来源:origin: wildfly/wildfly

/**
 * Returns the reason text as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a> If a reason
 * text is not supplied, an empty string is returned.
 */
public String reasonText() {
  ByteBuf binaryData = content();
  if (binaryData == null || binaryData.capacity() <= 2) {
    return "";
  }
  binaryData.readerIndex(2);
  String reasonText = binaryData.toString(CharsetUtil.UTF_8);
  binaryData.readerIndex(0);
  return reasonText;
}

代码示例来源:origin: traccar/traccar

@Override
protected Object decode(
    ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {
  if (buf.readableBytes() < 10) {
    return null;
  }
  String type = buf.toString(buf.readerIndex() + 5, 2, StandardCharsets.US_ASCII);
  int count = type.equals("F3") ? 12 : 14;
  int index = 0;
  while (index >= 0 && count > 0) {
    index = buf.indexOf(index + 1, buf.writerIndex(), (byte) '#');
    if (index > 0) {
      count -= 1;
    }
  }
  return index > 0 ? buf.readRetainedSlice(index + 1 - buf.readerIndex()) : null;
}

代码示例来源:origin: atomix/atomix

static String readString(ByteBuf buffer, int length) {
 if (buffer.isDirect()) {
  final String result = buffer.toString(buffer.readerIndex(), length, StandardCharsets.UTF_8);
  buffer.skipBytes(length);
  return result;
 } else if (buffer.hasArray()) {
  final String result = new String(buffer.array(), buffer.arrayOffset() + buffer.readerIndex(), length, StandardCharsets.UTF_8);
  buffer.skipBytes(length);
  return result;
 } else {
  final byte[] array = new byte[length];
  buffer.readBytes(array);
  return new String(array, StandardCharsets.UTF_8);
 }
}

代码示例来源:origin: traccar/traccar

private DeviceSession parseIdentification(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
  int length = buf.readUnsignedShort();
  String imei = buf.toString(buf.readerIndex(), length, StandardCharsets.US_ASCII);
  DeviceSession deviceSession = getDeviceSession(channel, remoteAddress, imei);
  if (channel != null) {
    ByteBuf response = Unpooled.buffer(1);
    if (deviceSession != null) {
      response.writeByte(1);
    } else {
      response.writeByte(0);
    }
    channel.writeAndFlush(new NetworkMessage(response, remoteAddress));
  }
  return deviceSession;
}

代码示例来源:origin: Graylog2/graylog2-server

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
  final int frameSizeValueLength = findFrameSizeValueLength(buffer);
  // We have not found the frame length value byte size yet.
  if (frameSizeValueLength <= 0) {
    return;
  }
  // Convert the frame length value bytes into an integer without mutating the buffer reader index.
  final String lengthString = buffer.slice(buffer.readerIndex(), frameSizeValueLength).toString(StandardCharsets.UTF_8);
  final int length = Integer.parseInt(lengthString);
  final int skipLength = frameSizeValueLength + 1; // Frame length value bytes and the whitespace that follows it.
  // We have to take the skipped bytes (frame size value length + whitespace) into account when checking if
  // the buffer has enough data to read the complete message.
  if (buffer.readableBytes() - skipLength < length) {
    // We cannot read the complete frame yet.
    return;
  } else {
    // Skip the frame length value bytes and the whitespace that follows it.
    buffer.skipBytes(skipLength);
  }
  final ByteBuf frame = buffer.readRetainedSlice(length);
  out.add(frame);
}

代码示例来源:origin: traccar/traccar

@Override
protected Object decode(
    ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {
  if (buf.readableBytes() < 10) {
    return null;
  }
  int beginIndex = BufferUtil.indexOf("$$", buf);
  if (beginIndex == -1) {
    return null;
  } else if (beginIndex > buf.readerIndex()) {
    buf.readerIndex(beginIndex);
  }
  int length;
  if (buf.getByte(buf.readerIndex() + 2) == (byte) '0') {
    length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 4, StandardCharsets.US_ASCII));
  } else {
    length = Integer.parseInt(buf.toString(buf.readerIndex() + 2, 2, StandardCharsets.US_ASCII), 16);
  }
  if (length <= buf.readableBytes()) {
    return buf.readRetainedSlice(length);
  }
  return null;
}

代码示例来源:origin: wildfly/wildfly

private static Result<String> decodeString(ByteBuf buffer, int minBytes, int maxBytes) {
  final Result<Integer> decodedSize = decodeMsbLsb(buffer);
  int size = decodedSize.value;
  int numberOfBytesConsumed = decodedSize.numberOfBytesConsumed;
  if (size < minBytes || size > maxBytes) {
    buffer.skipBytes(size);
    numberOfBytesConsumed += size;
    return new Result<String>(null, numberOfBytesConsumed);
  }
  String s = buffer.toString(buffer.readerIndex(), size, CharsetUtil.UTF_8);
  buffer.skipBytes(size);
  numberOfBytesConsumed += size;
  return new Result<String>(s, numberOfBytesConsumed);
}

代码示例来源:origin: wildfly/wildfly

private long parseRedisNumber(ByteBuf byteBuf) {
  final int readableBytes = byteBuf.readableBytes();
  final boolean negative = readableBytes > 0 && byteBuf.getByte(byteBuf.readerIndex()) == '-';
  final int extraOneByteForNegative = negative ? 1 : 0;
  if (readableBytes <= extraOneByteForNegative) {
    throw new RedisCodecException("no number to parse: " + byteBuf.toString(CharsetUtil.US_ASCII));
  }
  if (readableBytes > RedisConstants.POSITIVE_LONG_MAX_LENGTH + extraOneByteForNegative) {
    throw new RedisCodecException("too many characters to be a valid RESP Integer: " +
                   byteBuf.toString(CharsetUtil.US_ASCII));
  }
  if (negative) {
    return -parsePositiveNumber(byteBuf.skipBytes(extraOneByteForNegative));
  }
  return parsePositiveNumber(byteBuf);
}

相关文章

ByteBuf类方法