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

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

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

ByteBuf.isReadable介绍

[英]Returns trueif and only if (this.writerIndex - this.readerIndex) is greater than 0.
[中]仅当(this.writerIndex-this.readerIndex)大于0时返回trueif。

代码示例

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

@Override
public int read() throws IOException {
  if (buffer.isReadable()) {
    return buffer.readByte() & 0xff;
  }
  return -1;
}

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

/**
 * Creates a new buffer which wraps the specified buffer's readable bytes.
 * A modification on the specified buffer's content will be visible to the
 * returned buffer.
 * @param buffer The buffer to wrap. Reference count ownership of this variable is transferred to this method.
 * @return The readable portion of the {@code buffer}, or an empty buffer if there is no readable portion.
 * The caller is responsible for releasing this buffer.
 */
public static ByteBuf wrappedBuffer(ByteBuf buffer) {
  if (buffer.isReadable()) {
    return buffer.slice();
  } else {
    buffer.release();
    return EMPTY_BUFFER;
  }
}

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

private void decodeHeartbeat(ByteBuf in, List<Object> out) {
  while (in.isReadable()) {
    if (in.readByte() == Packet.HB_PACKET_BYTE) {
      out.add(Packet.HB_PACKET);
    } else {
      in.readerIndex(in.readerIndex() - 1);
      break;
    }
  }
}

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

@Override
  public Object decode(ByteBuf buf, State state) throws IOException {
    List<Object> result = new ArrayList<Object>(3);
    Object key = MapCacheEventCodec.this.decode(buf, state, codec.getMapKeyDecoder());
    result.add(key);
    Object value = MapCacheEventCodec.this.decode(buf, state, codec.getMapValueDecoder());
    result.add(value);
    
    if (buf.isReadable()) {
      Object oldValue = MapCacheEventCodec.this.decode(buf, state, codec.getMapValueDecoder());
      result.add(oldValue);
    }
    
    return result;
  }
};

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

while (in.isReadable()) {
  final int startIndex = in.readerIndex();
    incompleteBuffer.ensureWritable(in.readableBytes());
    incompleteBuffer.writeBytes(in);
  } else {
    if (null != incompleteBuffer) {
      line = incompleteBuffer.toString(encoding) + lineBuf.toString(encoding);
      incompleteBuffer.release();
      incompleteBuffer = null;
    } else {
      line = lineBuf.toString(encoding);
    out.add(line);
    in.skipBytes(1); // Skip new line character.

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

private void fetchEncoderOutput(List<Object> out) {
  for (;;) {
    ByteBuf buf = encoder.readOutbound();
    if (buf == null) {
      break;
    }
    if (!buf.isReadable()) {
      buf.release();
      continue;
    }
    out.add(new DefaultHttpContent(buf));
  }
}

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

@Override
public Object decode(ByteBuf buf, State state) throws IOException {
  byte type = buf.readByte();
  if (type == 0x0) {
    return new LocalCachedMapClear();
      byte[] value = new byte[valueLen];
      buf.readBytes(value);
      entries.add(new LocalCachedMapUpdate.Entry(key, value));
      if (!buf.isReadable()) {
        break;
    byte len = buf.readByte();
    CharSequence requestId = buf.readCharSequence(len, CharsetUtil.UTF_8);
    long timeout = buf.readLong();
    byte len = buf.readByte();
    CharSequence requestId = buf.readCharSequence(len, CharsetUtil.UTF_8);
    int hashesCount = buf.readInt();

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

private List<RawMessageExtend> deserializeRawMessagesExtend(RemotingCommand request) {
  final ByteBuf body = request.getBody();
  if (body.readableBytes() == 0) return Collections.emptyList();
  List<RawMessageExtend> messages = Lists.newArrayList();
  while (body.isReadable()) {
    messages.add(doDeserializeRawMessagesExtend(body));
  }
  return messages;
}

代码示例来源: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: Graylog2/graylog2-server

public static NetFlowV9Packet parsePacket(ByteBuf bb, NetFlowV9FieldTypeRegistry typeRegistry, Map<Integer, NetFlowV9Template> cache, @Nullable NetFlowV9OptionTemplate optionTemplate) {
  final int dataLength = bb.readableBytes();
  final NetFlowV9Header header = parseHeader(bb);
  NetFlowV9OptionTemplate optTemplate = optionTemplate;
  List<NetFlowV9BaseRecord> records = new ArrayList<>();
  while (bb.isReadable()) {
    bb.markReaderIndex();
    int flowSetId = bb.readUnsignedShort();

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

if (!in.isReadable()) {
  return;
final int length = Math.min(in.readableBytes(), blockCompressor.availableSize());
final int bytesWritten = blockCompressor.write(in, in.readerIndex(), length);
in.skipBytes(bytesWritten);
if (!blockCompressor.isFull()) {
  if (in.isReadable()) {
    break;
  } else {

代码示例来源: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: eclipse-vertx/vert.x

/**
 * Return a buffer from HTTP/2 codec that Vert.x can use:
 *
 * - if it's a direct buffer (coming likely from OpenSSL) : we get a heap buffer version
 * - if it's a composite buffer we do the same
 * - otherwise we increase the ref count
 */
static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
 if (buf == Unpooled.EMPTY_BUFFER) {
  return buf;
 }
 if (buf.isDirect() || buf instanceof CompositeByteBuf) {
  if (buf.isReadable()) {
   ByteBuf buffer =  allocator.heapBuffer(buf.readableBytes());
   buffer.writeBytes(buf);
   return buffer;
  } else {
   return Unpooled.EMPTY_BUFFER;
  }
 }
 return buf.retain();
}

代码示例来源:origin: AsyncHttpClient/async-http-client

protected long transfer(ByteBuf source, WritableByteChannel target, MultipartState sourceFullyWrittenState) throws IOException {
 int transferred = 0;
 if (target instanceof GatheringByteChannel) {
  transferred = source.readBytes((GatheringByteChannel) target, source.readableBytes());
 } else {
  for (ByteBuffer byteBuffer : source.nioBuffers()) {
   int len = byteBuffer.remaining();
   int written = target.write(byteBuffer);
   transferred += written;
   if (written != len) {
    // couldn't write full buffer, exit loop
    break;
   }
  }
  // assume this is a basic single ByteBuf
  source.readerIndex(source.readerIndex() + transferred);
 }
 if (source.isReadable()) {
  slowTarget = true;
 } else {
  state = sourceFullyWrittenState;
 }
 return transferred;
}

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

public static ByteBuf safeBuffer(ByteBuf buf, ByteBufAllocator allocator) {
 if (buf == Unpooled.EMPTY_BUFFER) {
  return buf;
 }
 if (buf.isDirect() || buf instanceof CompositeByteBuf) {
  try {
   if (buf.isReadable()) {
    ByteBuf buffer =  allocator.heapBuffer(buf.readableBytes());
    buffer.writeBytes(buf);
    return buffer;
   } else {
    return Unpooled.EMPTY_BUFFER;
   }
  } finally {
   buf.release();
  }
 }
 return buf;
}

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

@Override
protected void channelRead0(ChannelHandlerContext ctx, ByteBuf buf) throws Exception {
  updateNow();
  while (buf.isReadable()) {
    if (incomingBuffer.remaining() > buf.readableBytes()) {
      int newLimit = incomingBuffer.position()
          + buf.readableBytes();
      incomingBuffer.limit(newLimit);

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

if (!in.isReadable()) {
  return NOT_ENOUGH_INPUT;
  int copies = length / offset;
  for (; copies > 0; copies--) {
    out.readerIndex(initialIndex - offset);
    out.readBytes(out, offset);
    out.readerIndex(initialIndex - offset);
    out.readBytes(out, length % offset);
  out.readerIndex(initialIndex - offset);
  out.readBytes(out, length);

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

@Override
public String readLine() throws IOException {
  if (!buffer.isReadable()) {
    return null;
  }
  if (lineBuf != null) {
    lineBuf.setLength(0);
  }
  loop: do {
    int c = buffer.readUnsignedByte();
    switch (c) {
      case '\n':
        break loop;
      case '\r':
        if (buffer.isReadable() && (char) buffer.getUnsignedByte(buffer.readerIndex()) == '\n') {
          buffer.skipBytes(1);
        }
        break loop;
      default:
        if (lineBuf == null) {
          lineBuf = new StringBuilder();
        }
        lineBuf.append((char) c);
    }
  } while (buffer.isReadable());
  return lineBuf != null && lineBuf.length() > 0 ? lineBuf.toString() : StringUtil.EMPTY_STRING;
}

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

/**
 * Skip one empty line
 *
 * @return True if one empty line was skipped
 */
private boolean skipOneLine() {
  if (!undecodedChunk.isReadable()) {
    return false;
  }
  byte nextByte = undecodedChunk.readByte();
  if (nextByte == HttpConstants.CR) {
    if (!undecodedChunk.isReadable()) {
      undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
      return false;
    }
    nextByte = undecodedChunk.readByte();
    if (nextByte == HttpConstants.LF) {
      return true;
    }
    undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 2);
    return false;
  }
  if (nextByte == HttpConstants.LF) {
    return true;
  }
  undecodedChunk.readerIndex(undecodedChunk.readerIndex() - 1);
  return false;
}

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

private static void sendResponse(Channel channel, int device, long id, int crc) {
  if (channel != null) {
    ByteBuf response = Unpooled.buffer();
    response.writeByte(device);
    response.writeByte(MSG_ACK);
    response.writeIntLE((int) id);
    response.writeShortLE(crc);
    response.writeShortLE(Checksum.crc16(
        Checksum.CRC16_XMODEM, response.nioBuffer()));
    ByteBuf encoded = Unpooled.buffer();
    encoded.writeByte(0x01); // header
    while (response.isReadable()) {
      int b = response.readByte();
      if (b == 0x01 || b == 0x04 || b == 0x10 || b == 0x11 || b == 0x13) {
        encoded.writeByte(0x10);
        b += 0x20;
      }
      encoded.writeByte(b);
    }
    response.release();
    encoded.writeByte(0x04); // ending
    channel.writeAndFlush(new NetworkMessage(encoded, channel.remoteAddress()));
  }
}

相关文章

ByteBuf类方法