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

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

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

ByteBuf.readInt介绍

[英]Gets a 32-bit integer at the current readerIndexand increases the readerIndex by 4 in this buffer.
[中]获取当前readerIndex处的32位整数,并在此缓冲区中将readerIndex增加4。

代码示例

代码示例来源:origin: weibocom/motan

private void decodeV1(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  long startTime = System.currentTimeMillis();
  in.resetReaderIndex();
  in.skipBytes(2);// skip magic num
  byte messageType = (byte) in.readShort();
  long requestId = in.readLong();
  int dataLength = in.readInt();
  // FIXME 如果dataLength过大,可能导致问题
  if (in.readableBytes() < dataLength) {
    in.resetReaderIndex();
    return;
  }
  checkMaxContext(dataLength, ctx, messageType == MotanConstants.FLAG_REQUEST, requestId);
  byte[] data = new byte[dataLength];
  in.readBytes(data);
  decode(data, out, messageType == MotanConstants.FLAG_REQUEST, requestId).setStartTime(startTime);
}

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

@Override
  public Object decode(ByteBuf buf, State state) throws IOException {
    int decompressSize = buf.readInt();
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
    try {
      LZ4SafeDecompressor decompressor = factory.safeDecompressor();
      ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
      int pos = outBuffer.position();
      decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
      int compressedLength = outBuffer.position() - pos;
      out.writerIndex(compressedLength);
      return innerCodec.getValueDecoder().decode(out, state);
    } finally {
      out.release();
    }
  }
};

代码示例来源:origin: apache/hive

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
  throws Exception {
 if (in.readableBytes() < 4) {
  return;
 }
 in.markReaderIndex();
 int msgSize = in.readInt();
 checkSize(msgSize);
 if (in.readableBytes() < msgSize) {
  // Incomplete message in buffer.
  in.resetReaderIndex();
  return;
 }
 try {
  ByteBuffer nioBuffer = maybeDecrypt(in.nioBuffer(in.readerIndex(), msgSize));
  Input kryoIn = new Input(new ByteBufferInputStream(nioBuffer));
  Object msg = kryos.get().readClassAndObject(kryoIn);
  LOG.trace("Decoded message of type {} ({} bytes)",
    msg != null ? msg.getClass().getName() : msg, msgSize);
  out.add(msg);
 } finally {
  in.skipBytes(msgSize);
 }
}

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

@Override
public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
  if (in.readableBytes() < 8)
  int compressedFileSize = copy.readInt();
  if (size + 3 + breaks > in.readableBytes())
    logger.trace("Index {} archive {}: Not enough data yet {} > {}", index, file, size + 3 + breaks, in.readableBytes());
    return;
    int bytesToRead = Math.min(bytesInBlock, size - compressedData.writerIndex());
    logger.trace("{}/{}: reading block {}/{}, read so far this block: {}, file status: {}/{}",
      index, file,
      (totalRead % CHUNK_SIZE), CHUNK_SIZE,
  logger.trace("{}/{}: done downloading file, remaining buffer {}",
    index, file,
    in.readableBytes());

代码示例来源:origin: org.apache.distributedlog/distributedlog-protocol

if (LOG.isTraceEnabled()) {
    LOG.trace("Found position {} beyond {}", recordStream.getCurrentPosition(), dlsn);
    if (LOG.isTraceEnabled()) {
      LOG.trace("Found position {} beyond {}", currTxId, txId);
  recordSetReader = LogRecordSet.of(record);
} else {
  int length = in.readInt();
  if (length < 0) {
  if (LOG.isTraceEnabled()) {
    LOG.trace("Skipped Record with TxId {} DLSN {}",
      currTxId, recordStream.getCurrentPosition());

代码示例来源: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;
  int magicCode = in.getInt(in.readerIndex() + RemotingHeader.LENGTH_FIELD);
  if (DEFAULT_MAGIC_CODE != magicCode) {
    throw new IOException("Illegal Data, MagicCode=" + Integer.toHexString(magicCode));
  int total = in.readInt();
  if (in.readableBytes() < total) {
    in.resetReaderIndex();
    return;
  short headerSize = in.readShort();
  RemotingHeader remotingHeader = decodeHeader(in);

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

int idLength = buf.indexOf(buf.readerIndex(), buf.writerIndex(), (byte) 0x00) - buf.readerIndex();
String id = buf.readBytes(idLength).toString(StandardCharsets.US_ASCII);
buf.readByte();
  int blockEnd = buf.readInt() + buf.readerIndex();
    position.setLatitude(buf.readDoubleLE());
    position.setAltitude(buf.readDoubleLE());
    position.setSpeed(buf.readShort());
    position.setCourse(buf.readShort());
    position.set(Position.KEY_SATELLITES, buf.readByte());
  } else {
        break;
      case 3:
        position.set(name, buf.readInt());
        break;
      case 4:

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

@Override
  public Object decode(ByteBuf buf, State state) throws IOException {
    int decompressSize = buf.readInt();
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer(decompressSize);
    try {
      LZ4SafeDecompressor decompressor = factory.safeDecompressor();
      ByteBuffer outBuffer = out.internalNioBuffer(out.writerIndex(), out.writableBytes());
      int pos = outBuffer.position();
      decompressor.decompress(buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()), outBuffer);
      int compressedLength = outBuffer.position() - pos;
      out.writerIndex(compressedLength);
      return innerCodec.getValueDecoder().decode(out, state);
    } finally {
      out.release();
    }
  }
};

代码示例来源:origin: alibaba/fescar

@Override
public boolean decode(ByteBuf in) {
  int leftLen = in.readableBytes();
  int read = 0;
  int xidLen = in.readShort();
  if (xidLen > 0) {
    if (leftLen < xidLen) {
  leftLen --;
  int resourceIdLen = in.readShort();
  if (resourceIdLen > 0) {
    if (leftLen < resourceIdLen) {
  int applicationDataLen = in.readInt();
  if (applicationDataLen > 0) {
    if (leftLen < applicationDataLen) {

代码示例来源:origin: alipay/sofa-bolt

public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        if (type == RpcCommandType.REQUEST || type == RpcCommandType.REQUEST_ONEWAY) {
          if (in.readableBytes() >= RpcProtocolV2.getRequestHeaderLength() - 3) {
            short cmdCode = in.readShort();
            byte ver2 = in.readByte();
            int requestId = in.readInt();
            byte serializer = in.readByte();
            byte protocolSwitchValue = in.readByte();
            int timeout = in.readInt();
            short classLen = in.readShort();
            short headerLen = in.readShort();
            int contentLen = in.readInt();
            byte[] clazz = null;
            byte[] header = null;
            short cmdCode = in.readShort();
            byte ver2 = in.readByte();
            int requestId = in.readInt();
            byte serializer = in.readByte();
            byte protocolSwitchValue = in.readByte();
            short classLen = in.readShort();
            short headerLen = in.readShort();
            int contentLen = in.readInt();
            byte[] clazz = null;
            byte[] header = null;

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

format = formats.get(buf.getUnsignedByte(buf.readerIndex()));
} else if (!formats.isEmpty()) {
  format = formats.values().iterator().next();
      break;
    case 0x07:
      position.setLatitude(buf.readInt() * 0.000001);
      break;
    case 0x08:
      position.setLongitude(buf.readInt() * 0.000001);
      break;
    case 0x09:
      position.setAltitude(buf.readShort() * 0.1);
      break;
    case 0x0a:
      position.setCourse(buf.readShort() * 0.1);
      break;
    case 0x0b:
      break;
    case 0x14:
      position.set(Position.KEY_RSSI, buf.readShort());
      break;
    case 0x16:

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

switch (currentState) {
case INIT_BLOCK:
  if (in.readableBytes() < HEADER_LENGTH) {
    break;
  int blockType = token & 0xF0;
  int compressedLength = Integer.reverseBytes(in.readInt());
  if (compressedLength < 0 || compressedLength > MAX_BLOCK_SIZE) {
    throw new DecompressionException(String.format(
  int decompressedLength = Integer.reverseBytes(in.readInt());
  final int maxDecompressedLength = 1 << compressionLevel;
  if (decompressedLength < 0 || decompressedLength > maxDecompressedLength) {
  int currentChecksum = Integer.reverseBytes(in.readInt());
  if (decompressedLength == 0 && compressedLength == 0) {
    if (currentChecksum != 0) {
  currentChecksum = this.currentChecksum;
  if (in.readableBytes() < compressedLength) {
    break;
        uncompressed = in.retainedSlice(in.readerIndex(), decompressedLength);
        break;
      case BLOCK_TYPE_COMPRESSED:

代码示例来源:origin: alibaba/fescar

@Override
public boolean decode(ByteBuf in) {
  int i = in.readableBytes();
  if (i < 1) {
    return false;
  short len = in.readShort();
  if (len > 0) {
    if (i < len) {
  len = in.readShort();
  if (len > 0) {
    if (i < len) {
  len = in.readShort();
  if (len > 0) {
    if (i < len) {
  int iLen = in.readInt();
  if (iLen > 0) {
    if (i < iLen) {

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

position.setLatitude(buf.readInt() * 0.0000001);
position.setLongitude(buf.readInt() * 0.0000001);
if (type != MSG_MINI_EVENT_REPORT) {
  position.setAltitude(buf.readInt() * 0.01);
  position.setSpeed(UnitsConverter.knotsFromCps(buf.readUnsignedInt()));
position.setCourse(buf.readShort());
if (type == MSG_MINI_EVENT_REPORT) {
  position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
  position.set(Position.KEY_SATELLITES, buf.getUnsignedByte(buf.readerIndex()) & 0xf);
  position.setValid((buf.readUnsignedByte() & 0x20) == 0);
} else {
  position.set(Position.KEY_RSSI, buf.readShort());
int accType = BitUtil.from(buf.getUnsignedByte(buf.readerIndex()), 6);
int accCount = BitUtil.to(buf.readUnsignedByte(), 6);
  if (buf.readableBytes() >= 4) {
    position.set("acc" + i, buf.readUnsignedInt());

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

if (buf.getUnsignedByte(buf.readerIndex()) == 0) {
  String imei = buf.toString(buf.readerIndex(), 15, StandardCharsets.US_ASCII);
    position.setLongitude(buf.readInt() / 10000000.0);
    position.setLatitude(buf.readInt() / 10000000.0);
    position.setAltitude(buf.readShort());
    position.setCourse(buf.readUnsignedShort());

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

switch (currentState) {
case INIT_BLOCK:
  if (in.readableBytes() < HEADER_LENGTH) {
    break;
  int blockType = token & 0xF0;
  int compressedLength = Integer.reverseBytes(in.readInt());
  if (compressedLength < 0 || compressedLength > MAX_BLOCK_SIZE) {
    throw new DecompressionException(String.format(
  int decompressedLength = Integer.reverseBytes(in.readInt());
  final int maxDecompressedLength = 1 << compressionLevel;
  if (decompressedLength < 0 || decompressedLength > maxDecompressedLength) {
  int currentChecksum = Integer.reverseBytes(in.readInt());
  if (decompressedLength == 0 && compressedLength == 0) {
    if (currentChecksum != 0) {
  currentChecksum = this.currentChecksum;
  if (in.readableBytes() < compressedLength) {
    break;
        uncompressed = in.retainedSlice(in.readerIndex(), decompressedLength);
        break;
      case BLOCK_TYPE_COMPRESSED:

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

if (buffer.readableBytes() < Byte.BYTES) {
  return;
 if (buffer.readableBytes() < octetsLength) {
  buffer.resetReaderIndex();
  return;
 currentState = DecoderState.READ_SENDER_PORT;
case READ_SENDER_PORT:
 if (buffer.readableBytes() < Integer.BYTES) {
  return;
 senderPort = buffer.readInt();
 senderAddress = new Address(senderIp.getHostName(), senderPort, senderIp);
 currentState = DecoderState.READ_TYPE;
    return;
   subjectLength = buffer.readShort();
   currentState = DecoderState.READ_SUBJECT;
  case READ_SUBJECT:

代码示例来源:origin: jwpttcg66/NettyGameServer

netMessageHead.setLength(byteBuf.readInt());
netMessageHead.setVersion(byteBuf.readByte());
short cmd = byteBuf.readShort();
netMessageHead.setCmd(cmd);
netMessageHead.setSerial(byteBuf.readInt());
netMessageHead.setTocken(byteBuf.readInt());
int byteLength = byteBuf.readableBytes();
byte[] bytes = new byte[byteLength];
byteBuf.getBytes(byteBuf.readerIndex(), bytes);
netMessageBody.setBytes(bytes);
netMessage.setNetMessageHead(netMessageHead);

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

private Position decodeMgMessage(Channel channel, SocketAddress remoteAddress, ByteBuf buf) {
  int flags = buf.getUnsignedByte(buf.readerIndex());
  position.setLatitude(convertCoordinate(buf.readInt()));
  position.setLongitude(convertCoordinate(buf.readInt()));
  position.setAltitude(UnitsConverter.metersFromFeet(buf.readShort()));
  position.setCourse(buf.readUnsignedShort());
  position.setSpeed(UnitsConverter.knotsFromMph(buf.readUnsignedByte()));

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

switch (currentState) {
case INIT_BLOCK:
  if (in.readableBytes() < 4) {
    break;
  if (in.readableBytes() < 2 + (isCompressed ? 2 : 0) + (hasChecksum ? 4 : 0)) {
    break;
  currentChecksum = hasChecksum ? in.readInt() : 0;
  chunkLength = in.readUnsignedShort();
  originalLength = isCompressed ? in.readUnsignedShort() : chunkLength;
  if (in.readableBytes() < chunkLength) {
    break;
  final int idx = in.readerIndex();
  final int originalLength = this.originalLength;

相关文章

ByteBuf类方法