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

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

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

ByteBuf.readBytes介绍

[英]Transfers this buffer's data to a newly created buffer starting at the current readerIndex and increases the readerIndexby the number of the transferred bytes (= length). The returned buffer's readerIndex and writerIndex are 0 and length respectively.
[中]将此缓冲区的数据传输到从当前readerIndex开始的新创建的缓冲区,并将readerIndex增加传输的字节数(=长度)。返回的缓冲区的readerIndex和writerIndex分别为0和length。

代码示例

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

@Override
  public Object decode(ByteBuf buf, State state) {
    byte[] result = new byte[buf.readableBytes()];
    buf.readBytes(result);
    return fromByteArrayReverse(result);
  }
};

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

private BytesValue(final ByteBuf buffer) {
  int len = buffer.readInt();
  val = new byte[len];
  buffer.readBytes(val);
}

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

@Override
  public String decode(ByteBuf source) {
    if (source.readableBytes() >= 4) {
      int length = source.readInt();
      if (length > 0 && source.readableBytes() > 0) {
        byte[] bytes = new byte[length];
        source.readBytes(bytes);
        return new String(bytes);
      }
    }
    return null;
  }
}

代码示例来源:origin: normanmaurer/netty-in-action

@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in,
    List<Object> out) throws Exception {
    while (in.readableBytes() >= frameLength) {
      ByteBuf buf = in.readBytes(frameLength);
      out.add(buf);
    }
  }
}

代码示例来源:origin: ainilili/ratel

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  int startIndex = -1;
  int endIndex = -1;
  if((startIndex = in.indexOf(in.readerIndex(), in.writerIndex(), TransferProtocolUtils.PROTOCOL_HAED)) != -1 && 
      (endIndex = in.indexOf(startIndex + 1, in.writerIndex(), TransferProtocolUtils.PROTOCOL_TAIL)) != -1) {
    endIndex ++;
    byte[] bytes = new byte[endIndex - startIndex];
    in.skipBytes(startIndex - in.readerIndex());
    in.readBytes(bytes, 0, bytes.length);
    out.add(bytes);
  }
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
    throws Exception {
    if (in.readableBytes() < 4) {
      return;
    }

    in.markReaderIndex();
    int length = in.order(ByteOrder.LITTLE_ENDIAN).readInt();
    if (in.readableBytes() < length) {
      in.resetReaderIndex();
      return;
    }

    ByteBuf buf = ctx.alloc().buffer(length);
    in.readBytes(buf, length);
    out.add(buf.retain());
  }
}

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

params.add(source.readInt());
  return true;
case ClusterConstants.PARAM_TYPE_STRING:
  int length = source.readInt();
  byte[] bytes = new byte[length];
  source.readBytes(bytes);
  params.add(new String(bytes));
  return true;
case ClusterConstants.PARAM_TYPE_BOOLEAN:
  params.add(source.readBoolean());
  return true;
case ClusterConstants.PARAM_TYPE_DOUBLE:

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

if (in.readableBytes() < 4) {
  return NOT_ENOUGH_INPUT;
  for (; copies > 0; copies--) {
    out.readerIndex(initialIndex - offset);
    out.readBytes(out, offset);
    out.readBytes(out, length % offset);
  out.readBytes(out, length);

代码示例来源:origin: normanmaurer/netty-in-action

@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in,
    List<Object> out)
    throws Exception {
    int readableBytes = in.readableBytes();
    if (readableBytes > maxFrameSize) {
      // discard the bytes
      in.clear();
      throw new TooLongFrameException();
    }
    ByteBuf buf = in.readBytes(readableBytes);
    out.add(buf);
  }
}

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

@Override
protected void decode(
    ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
  if (done) {
    int readable = actualReadableBytes();
    if (readable == 0) {
      // if non is readable just return null
      // https://github.com/netty/netty/issues/1159
      return;
    }
    out.add(buffer.readBytes(readable));
  } else {
    int oldSize = out.size();
    super.decode(ctx, buffer, out);
    if (failOnMissingResponse) {
      int size = out.size();
      for (int i = oldSize; i < size; i++) {
        decrement(out.get(i));
      }
    }
  }
}

代码示例来源:origin: luxiaoxun/NettyRpc

@Override
public final void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
  if (in.readableBytes() < 4) {
    return;
  }
  in.markReaderIndex();
  int dataLength = in.readInt();
  /*if (dataLength <= 0) {
    ctx.close();
  }*/
  if (in.readableBytes() < dataLength) {
    in.resetReaderIndex();
    return;
  }
  byte[] data = new byte[dataLength];
  in.readBytes(data);
  Object obj = SerializationUtil.deserialize(data, genericClass);
  //Object obj = JsonUtil.deserialize(data,genericClass); // Not use this, have some bugs
  out.add(obj);
}

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

@Override
  public Object decode(ByteBuf buf, State state) {
    byte[] result = new byte[buf.readableBytes()];
    buf.readBytes(result);
    return result;
  }
};

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

@Override
public boolean decode(ByteBuf in) {
  int i = in.readableBytes();
  if (i < 4) { return false; }
  i -= 4;
  int length = in.readInt();
  if (i < length) { return false; }
  byte[] buffer = new byte[length];
  in.readBytes(buffer);
  ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
  decode(byteBuffer);
  return true;
}

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

public static byte[] readBytes(ByteBuf in) {
  int len = in.readInt();
  byte[] bs = new byte[len];
  in.readBytes(bs);
  return bs;
}

代码示例来源:origin: ethereum/ethereumj

@Override
  public void decode(ChannelHandlerContext ctx, DatagramPacket packet, List<Object> out) throws Exception {
    ByteBuf buf = packet.content();
    byte[] encoded = new byte[buf.readableBytes()];
    buf.readBytes(encoded);
    try {
      Message msg = Message.decode(encoded);
      DiscoveryEvent event = new DiscoveryEvent(msg, packet.sender());
      out.add(event);
    } catch (Exception e) {
      throw new RuntimeException("Exception processing inbound message from " + ctx.channel().remoteAddress() + ": " + toHexString(encoded), e);
    }
  }
}

代码示例来源:origin: line/armeria

@Override
protected void decode(
    ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
  if (done) {
    final int readable = actualReadableBytes();
    if (readable == 0) {
      // if non is readable just return null
      // https://github.com/netty/netty/issues/1159
      return;
    }
    out.add(buffer.readBytes(readable));
  } else {
    final int oldSize = out.size();
    super.decode(ctx, buffer, out);
    if (failOnMissingResponse) {
      final int size = out.size();
      for (int i = oldSize; i < size; i++) {
        decrement(out.get(i));
      }
    }
  }
}

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

@Override
  public Object decode(ByteBuf buf, State state) {
    byte[] result = new byte[buf.readableBytes()];
    buf.readBytes(result);
    return fromByteArrayReverse(result);
  }
};

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

@Override
public boolean decode(ByteBuf in) {
  int i = in.readableBytes();
  if (i < 4) {
    return false;
  }
  i -= 4;
  int length = in.readInt();
  if (i < length) {
    return false;
  }
  byte[] buffer = new byte[length];
  in.readBytes(buffer);
  ByteBuffer byteBuffer = ByteBuffer.wrap(buffer);
  doDecode(byteBuffer);
  return true;
}

代码示例来源:origin: mpusher/mpush

public byte[] decodeBytes(ByteBuf body) {
  int fieldLength = body.readShort();
  if (fieldLength == 0) return null;
  if (fieldLength == Short.MAX_VALUE) {
    fieldLength += body.readInt();
  }
  byte[] bytes = new byte[fieldLength];
  body.readBytes(bytes);
  return bytes;
}

代码示例来源:origin: GlowstoneMC/Glowstone

@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out)
    throws Exception {
    // check for length field readability
    in.markReaderIndex();
    if (!readableVarInt(in)) {
      return;
    }

    // check for contents readability
    int length = ByteBufUtils.readVarInt(in);
    if (in.readableBytes() < length) {
      in.resetReaderIndex();
      return;
    }

    // read contents into buf
    ByteBuf buf = ctx.alloc().buffer(length);
    in.readBytes(buf, length);
    out.add(buf);
  }
}

相关文章

ByteBuf类方法