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

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

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

ByteBuf.readSlice介绍

[英]Returns a new slice of this buffer's sub-region starting at the current readerIndex and increases the readerIndex by the size of the new slice (= length).

Also be aware that this method will NOT call #retain() and so the reference count will NOT be increased.
[中]返回从当前readerIndex开始的此缓冲区子区域的新切片,并将readerIndex增加新切片的大小(=长度)。
还要注意,此方法不会调用#retain(),因此引用计数不会增加。

代码示例

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

private void readUnknownFrame(ChannelHandlerContext ctx, ByteBuf payload, Http2FrameListener listener)
    throws Http2Exception {
  payload = payload.readSlice(payload.readableBytes());
  listener.onUnknownFrame(ctx, frameType, streamId, flags, payload);
}

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

@Override
  public ByteBuf encode(Object in) throws IOException {
    ByteBuf buf = innerCodec.getValueEncoder().encode(in);
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
      int chunksAmount = (int)Math.ceil(buf.readableBytes() / (double)Short.MAX_VALUE);
      for (int i = 1; i <= chunksAmount; i++) {
        int chunkSize = Math.min(Short.MAX_VALUE, buf.readableBytes());
        ByteBuf chunk = buf.readSlice(chunkSize);
        int lenIndex = out.writerIndex();
        out.writeInt(0);
        snappyEncoder.get().encode(chunk, out, chunk.readableBytes());
        int compressedDataLength = out.writerIndex() - 4 - lenIndex;
        out.setInt(lenIndex, compressedDataLength);
      }
      return out;
    } finally {
      buf.release();
      snappyEncoder.get().reset();
    }
  }
};

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

@Override
  public ByteBuf encode(Object in) throws IOException {
    ByteBuf buf = innerCodec.getValueEncoder().encode(in);
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
      int chunksAmount = (int)Math.ceil(buf.readableBytes() / (double)Short.MAX_VALUE);
      for (int i = 1; i <= chunksAmount; i++) {
        int chunkSize = Math.min(Short.MAX_VALUE, buf.readableBytes());
        ByteBuf chunk = buf.readSlice(chunkSize);
        int lenIndex = out.writerIndex();
        out.writeInt(0);
        snappyEncoder.get().encode(chunk, out, chunk.readableBytes());
        int compressedDataLength = out.writerIndex() - 4 - lenIndex;
        out.setInt(lenIndex, compressedDataLength);
      }
      return out;
    } finally {
      buf.release();
      snappyEncoder.get().reset();
    }
  }
};

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

int dataLength = in.readableBytes();
if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
  for (;;) {
    final int lengthIdx = out.writerIndex() + 1;
    if (dataLength < MIN_COMPRESSIBLE_LENGTH) {
      ByteBuf slice = in.readSlice(dataLength);
      writeUnencodedChunk(slice, out, dataLength);
      break;
      ByteBuf slice = in.readSlice(Short.MAX_VALUE);
      calculateAndWriteChecksum(slice, out);
      snappy.encode(slice, out, Short.MAX_VALUE);
      dataLength -= Short.MAX_VALUE;
    } else {
      ByteBuf slice = in.readSlice(dataLength);
      calculateAndWriteChecksum(slice, out);
      snappy.encode(slice, out, dataLength);

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

private boolean decodeBulkStringContent(ByteBuf in, List<Object> out) throws Exception {
  final int readableBytes = in.readableBytes();
  if (readableBytes == 0 || remainingBulkLength == 0 && readableBytes < RedisConstants.EOL_LENGTH) {
    return false;
  }
  // if this is last frame.
  if (readableBytes >= remainingBulkLength + RedisConstants.EOL_LENGTH) {
    ByteBuf content = in.readSlice(remainingBulkLength);
    readEndOfLine(in);
    // Only call retain after readEndOfLine(...) as the method may throw an exception.
    out.add(new DefaultLastBulkStringRedisContent(content.retain()));
    resetDecoder();
    return true;
  }
  // chunked write.
  int toRead = Math.min(remainingBulkLength, readableBytes);
  remainingBulkLength -= toRead;
  out.add(new DefaultBulkStringRedisContent(in.readSlice(toRead).retain()));
  return true;
}

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

private static void readGoAwayFrame(ChannelHandlerContext ctx, ByteBuf payload,
    Http2FrameListener listener) throws Http2Exception {
  int lastStreamId = readUnsignedInt(payload);
  long errorCode = payload.readUnsignedInt();
  ByteBuf debugData = payload.readSlice(payload.readableBytes());
  listener.onGoAwayRead(ctx, lastStreamId, errorCode, debugData);
}

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

int dataLength = in.readableBytes();
if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
  for (;;) {
    final int lengthIdx = out.writerIndex() + 1;
    if (dataLength < MIN_COMPRESSIBLE_LENGTH) {
      ByteBuf slice = in.readSlice(dataLength);
      writeUnencodedChunk(slice, out, dataLength);
      break;
      ByteBuf slice = in.readSlice(Short.MAX_VALUE);
      calculateAndWriteChecksum(slice, out);
      snappy.encode(slice, out, Short.MAX_VALUE);
      dataLength -= Short.MAX_VALUE;
    } else {
      ByteBuf slice = in.readSlice(dataLength);
      calculateAndWriteChecksum(slice, out);
      snappy.encode(slice, out, dataLength);

代码示例来源:origin: ReactiveX/RxNetty

incompleteBuffer.ensureWritable(in.readableBytes());
  incompleteBuffer.writeBytes(in);
} else {
  ByteBuf lineBuf = in.readSlice(lastReadIndex - startIndex);
  String line;
  if (null != incompleteBuffer) {

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

private void readDataFrame(ChannelHandlerContext ctx, ByteBuf payload,
    Http2FrameListener listener) throws Http2Exception {
  int padding = readPadding(payload);
  verifyPadding(padding);
  // Determine how much data there is to read by removing the trailing
  // padding.
  int dataLength = lengthWithoutTrailingPadding(payload.readableBytes(), padding);
  ByteBuf data = payload.readSlice(dataLength);
  listener.onDataRead(ctx, streamId, data, padding, flags.endOfStream());
  payload.skipBytes(payload.readableBytes());
}

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

return null;
  return buffer.readSlice(length);
} else {
  final int length = buffer.readableBytes();
  if (length > v2MaxHeaderSize) {
    discardedBytes = length;
  discarding = false;
} else {
  discardedBytes = buffer.readableBytes();
  buffer.skipBytes(discardedBytes);

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

private void readContinuationFrame(ByteBuf payload, Http2FrameListener listener)
    throws Http2Exception {
  // Process the initial fragment, invoking the listener's callback if end of headers.
  final ByteBuf continuationFragment = payload.readSlice(payload.readableBytes());
  headersContinuation.processFragment(flags.endOfHeaders(), continuationFragment,
      listener);
  resetHeadersContinuationIfEnd(flags.endOfHeaders());
}

代码示例来源:origin: qunarcorp/qmq

private RawMessageExtend doDeserializeRawMessagesExtend(ByteBuf body) {
    body.markReaderIndex();
    int headerStart = body.readerIndex();
    long bodyCrc = body.readLong();
    MessageHeader header = deserializeMessageHeader(body);
    header.setBodyCrc(bodyCrc);
    int bodyLen = body.readInt();
    int headerLen = body.readerIndex() - headerStart;
    int totalLen = headerLen + bodyLen;

    body.resetReaderIndex();
    ByteBuf messageBuf = body.readSlice(totalLen);
    // client config error,prefer to send after ten second
    long scheduleTime = System.currentTimeMillis() + 10000;
    if (Flags.isDelay(header.getFlag())) {
      scheduleTime = header.getExpireTime();
    }

    return new RawMessageExtend(header, messageBuf, messageBuf.readableBytes(), scheduleTime);
  }
}

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

return null;
  ByteBuf frame = buffer.readSlice(length);
  buffer.skipBytes(DELIMITER_LENGTH);
  return frame;
} else {
  final int length = buffer.readableBytes();
  if (length > V1_MAX_LENGTH) {
    discardedBytes = length;
  discarding = false;
} else {
  discardedBytes = buffer.readableBytes();
  buffer.skipBytes(discardedBytes);

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

int dataLength = in.readableBytes();
if (dataLength > MIN_COMPRESSIBLE_LENGTH) {
  for (;;) {
    final int lengthIdx = out.writerIndex() + 1;
    if (dataLength < MIN_COMPRESSIBLE_LENGTH) {
      ByteBuf slice = in.readSlice(dataLength);
      writeUnencodedChunk(slice, out, dataLength);
      break;
      ByteBuf slice = in.readSlice(Short.MAX_VALUE);
      calculateAndWriteChecksum(slice, out);
      snappy.encode(slice, out, Short.MAX_VALUE);
      dataLength -= Short.MAX_VALUE;
    } else {
      ByteBuf slice = in.readSlice(dataLength);
      calculateAndWriteChecksum(slice, out);
      snappy.encode(slice, out, dataLength);

代码示例来源:origin: qunarcorp/qmq

@Override
protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> list) throws Exception {
  if (in.readableBytes() < RemotingHeader.MIN_HEADER_SIZE + RemotingHeader.LENGTH_FIELD) return;
  if (in.readableBytes() < total) {
    in.resetReaderIndex();
    return;
    ByteBuf bodyData = in.readSlice(bodyLength);
    bodyData.retain();
    remotingCommand.setBody(bodyData);

代码示例来源:origin: qunarcorp/qmq

private List<BaseMessage> deserializeBaseMessage(ByteBuf input) {
  if (input.readableBytes() == 0) return Collections.emptyList();
  List<BaseMessage> result = Lists.newArrayList();
  long pullLogOffset = input.readLong();
  //ignore consumer offset
  input.readLong();
  while (input.isReadable()) {
    BaseMessage message = new BaseMessage();
    byte flag = input.readByte();
    input.skipBytes(8 + 8);
    String subject = PayloadHolderUtils.readString(input);
    String messageId = PayloadHolderUtils.readString(input);
    readTags(input, message, flag);
    int bodyLen = input.readInt();
    ByteBuf body = input.readSlice(bodyLen);
    HashMap<String, Object> attrs = deserializeMapWrapper(subject, messageId, body);
    message.setMessageId(messageId);
    message.setSubject(subject);
    message.setAttrs(attrs);
    message.setProperty(BaseMessage.keys.qmq_pullOffset, pullLogOffset);
    result.add(message);
    pullLogOffset++;
  }
  return result;
}

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

private List<Position> decodeRetransmission(ByteBuf buf, DeviceSession deviceSession) {
  List<Position> positions = new LinkedList<>();
  int count = buf.readUnsignedByte();
  for (int i = 0; i < count; i++) {
    buf.readUnsignedByte(); // alarm
    int endIndex = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) '\\');
    if (endIndex < 0) {
      endIndex = buf.writerIndex() - 4;
    }
    String sentence = buf.readSlice(endIndex - buf.readerIndex()).toString(StandardCharsets.US_ASCII);
    Position position = new Position(getProtocolName());
    position.setDeviceId(deviceSession.getDeviceId());
    position = decodeRegular(position, sentence);
    if (position != null) {
      positions.add(position);
    }
    if (buf.readableBytes() > 4) {
      buf.readUnsignedByte(); // delimiter
    }
  }
  return positions;
}

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

private static HAProxyTLV readNextTLV(final ByteBuf header) {
  if (header.readableBytes() < 4) {
    return null;
  case PP2_TYPE_SSL:
    final ByteBuf rawContent = header.retainedSlice(header.readerIndex(), length);
    final ByteBuf byteBuf = header.readSlice(length);
    final byte client = byteBuf.readByte();
    final int verify = byteBuf.readInt();
    if (byteBuf.readableBytes() >= 4) {
      } while (byteBuf.readableBytes() >= 4);

代码示例来源:origin: blynkkk/blynk-server

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) {
  if (in.readableBytes() < MobileMessageDecoder.PROTOCOL_APP_HEADER_SIZE) {
    return;
    int length = (int) in.readUnsignedInt();
    if (in.readableBytes() < length) {
      in.resetReaderIndex();
      return;
    ByteBuf buf = in.readSlice(length);
    switch (command) {
      case GET_ENHANCED_GRAPH_DATA :
      case LOAD_PROFILE_GZIPPED :
      case GET_PROJECT_BY_TOKEN :
        byte[] bytes = new byte[buf.readableBytes()];
        buf.readBytes(bytes);
        message = new BinaryMessage(messageId, command, bytes);

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

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  if (corrupted) {
    in.skipBytes(in.readableBytes());
    return;
    final int inSize = in.readableBytes();
    if (inSize < 4) {
            snappy.decode(in.readSlice(chunkLength - 4), uncompressed);

相关文章

ByteBuf类方法