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

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

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

ByteBuf.retain介绍

[英]Returns a retained buffer which shares the whole region of this buffer. Modifying the content of the returned buffer or this buffer affects each other's content while they maintain separate indexes and marks. This method is identical to buf.slice(0, buf.capacity()). This method does not modify readerIndex or writerIndex of this buffer.

Note that this method returns a #retain() buffer unlike #slice(int,int). This method behaves similarly to duplicate().retain() except that this method may return a buffer implementation that produces less garbage.
[中]返回共享此缓冲区整个区域的保留缓冲区。修改返回的缓冲区或此缓冲区的内容会影响彼此的内容,同时它们会维护单独的索引和标记。此方法与buf相同。切片(0,buf.capacity()。此方法不修改此缓冲区的readerIndex或writerIndex。
请注意,此方法返回一个#retain()缓冲区,与#slice(int,int)不同。此方法的行为类似于duplicate()。retain(),但此方法可能返回产生较少垃圾的缓冲区实现。

代码示例

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

@Override
  protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    out.add(new SctpMessage(protocolIdentifier, streamIdentifier, unordered, msg.retain()));
  }
}

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

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
  int length = msg.readableBytes() + lengthAdjustment;
  if (lengthIncludesLengthFieldLength) {
    length += lengthFieldLength;
          "length does not fit into a byte: " + length);
    out.add(ctx.alloc().buffer(1).order(byteOrder).writeByte((byte) length));
    break;
  case 2:
          "length does not fit into a short integer: " + length);
    out.add(ctx.alloc().buffer(2).order(byteOrder).writeShort((short) length));
    break;
  case 3:
          "length does not fit into a medium integer: " + length);
    out.add(ctx.alloc().buffer(3).order(byteOrder).writeMedium(length));
    break;
  case 4:
    throw new Error("should not reach here");
  out.add(msg.retain());

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

writeParameters(req.parameters(), buffer);
  ByteBufUtil.writeShortBE(buffer, CRLF_SHORT);
  out.add(buffer);
  release = false;
  if (req.command().isContentExpected()) {
out.add(content.retain());
if (msg instanceof LastSmtpContent) {
  out.add(DOT_CRLF_BUFFER.retainedDuplicate());
  contentExpected = false;

代码示例来源:origin: andsel/moquette

@Override
  protected void decode(ChannelHandlerContext chc, BinaryWebSocketFrame frame, List<Object> out)
      throws Exception {
    // convert the frame to a ByteBuf
    ByteBuf bb = frame.content();
    // System.out.println("WebSocketFrameToByteBufDecoder decode - " +
    // ByteBufUtil.hexDump(bb));
    bb.retain();
    out.add(bb);
  }
}

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

private static ByteBuf encodeContent(StompContentSubframe content, ChannelHandlerContext ctx) {
  if (content instanceof LastStompContentSubframe) {
    ByteBuf buf = ctx.alloc().buffer(content.content().readableBytes() + 1);
    buf.writeBytes(content.content());
    buf.writeByte(StompConstants.NUL);
    return buf;
  } else {
    return content.content().retain();
  }
}

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

private static void writeBulkStringContent(ByteBufAllocator allocator, BulkStringRedisContent msg,
                      List<Object> out) {
  out.add(msg.content().retain());
  if (msg instanceof LastBulkStringRedisContent) {
    out.add(allocator.ioBuffer(RedisConstants.EOL_LENGTH).writeShort(RedisConstants.EOL_SHORT));
  }
}

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

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
  int length = msg.readableBytes() + lengthAdjustment;
  if (lengthIncludesLengthFieldLength) {
    length += lengthFieldLength;
          "length does not fit into a byte: " + length);
    out.add(ctx.alloc().buffer(1).order(byteOrder).writeByte((byte) length));
    break;
  case 2:
          "length does not fit into a short integer: " + length);
    out.add(ctx.alloc().buffer(2).order(byteOrder).writeShort((short) length));
    break;
  case 3:
          "length does not fit into a medium integer: " + length);
    out.add(ctx.alloc().buffer(3).order(byteOrder).writeMedium(length));
    break;
  case 4:
    throw new Error("should not reach here");
  out.add(msg.retain());

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

public static OpAddEntry create(ManagedLedgerImpl ml, ByteBuf data, AddEntryCallback callback, Object ctx) {
  OpAddEntry op = RECYCLER.get();
  op.ml = ml;
  op.ledger = null;
  op.data = data.retain();
  op.dataLength = data.readableBytes();
  op.callback = callback;
  op.ctx = ctx;
  op.closeWhenDone = false;
  op.entryId = -1;
  op.startTime = System.nanoTime();
  op.completed = FALSE;
  ml.mbean.addAddEntrySample(op.dataLength);
  if (log.isDebugEnabled()) {
    log.debug("Created new OpAddEntry {}", op);
  }
  return op;
}

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

@Override
  protected void decode(ChannelHandlerContext ctx, SctpMessage msg, List<Object> out) throws Exception {
    if (!msg.isComplete()) {
      throw new CodecException(String.format("Received SctpMessage is not complete, please add %s in the " +
          "pipeline before this handler", SctpMessageCompletionHandler.class.getSimpleName()));
    }
    out.add(msg.content().retain());
  }
}

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

@Override
protected void doWrite(ChannelOutboundBuffer in) throws Exception {
 ByteBuf chunk;
 while (!stream.isNotWritable() && (chunk = (ByteBuf) in.current()) != null) {
  bytesWritten += chunk.readableBytes();
  stream.writeData(chunk.retain(), false);
  stream.handlerContext.flush();
  in.remove();
 }
}

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

@Override
protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> out) throws Exception {
  if (frame instanceof PingWebSocketFrame) {
    frame.content().retain();
    ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content()));
    return;
  }
  if (frame instanceof PongWebSocketFrame && dropPongFrames) {
    return;
  }
  out.add(frame.retain());
}

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

int numChunks = (int) Math.ceil((double) msg.readableBytes() / chunkSize);
int currentChunkLen = min(msg.readableBytes(), chunkSize);
 chunkBuf.retain();
 cbb.addComponent(chunkBuf);
 cbbWriteIndex += currentChunkLen;
 msg.skipBytes(currentChunkLen);
 --numChunks;
 currentChunkLen = min(msg.readableBytes(), chunkSize);
out.add(cbb);

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

final int readableBytes = buf.readableBytes();
if (readableBytes == 0) {
  ReferenceCountUtil.safeRelease(holder);
  buf.retain();
  ReferenceCountUtil.safeRelease(holder);

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

@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception
  {
    if (in.getByte(in.readerIndex()) != UpdateOpcodes.ENCRYPTION)
    {
      ctx.fireChannelRead(in.retain());
      return;
    }

    in.readByte();
    byte xorKey = in.readByte();
    in.readShort(); // always 0

    EncryptionPacket encryptionPacket = new EncryptionPacket();
    encryptionPacket.setKey(xorKey);
    out.add(encryptionPacket);
  }
}

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

@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
  int length = msg.readableBytes() + lengthAdjustment;
  if (lengthIncludesLengthFieldLength) {
    length += lengthFieldLength;
          "length does not fit into a byte: " + length);
    out.add(ctx.alloc().buffer(1).order(byteOrder).writeByte((byte) length));
    break;
  case 2:
          "length does not fit into a short integer: " + length);
    out.add(ctx.alloc().buffer(2).order(byteOrder).writeShort((short) length));
    break;
  case 3:
          "length does not fit into a medium integer: " + length);
    out.add(ctx.alloc().buffer(3).order(byteOrder).writeMedium(length));
    break;
  case 4:
    throw new Error("should not reach here");
  out.add(msg.retain());

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

@Override
protected Object decode(
    ChannelHandlerContext ctx, Channel channel, ByteBuf buf) throws Exception {
  if (buf.readableBytes() < KEEP_ALIVE_LENGTH) {
    return null;
  }
  if (buf.getUnsignedByte(buf.readerIndex()) == 0xD0) {
    // Send response
    ByteBuf frame = buf.readRetainedSlice(KEEP_ALIVE_LENGTH);
    if (channel != null) {
      frame.retain();
      channel.writeAndFlush(new NetworkMessage(frame, channel.remoteAddress()));
    }
    return frame;
  } else {
    int index = BufferUtil.indexOf("\r\n", buf);
    if (index != -1) {
      ByteBuf frame = buf.readRetainedSlice(index - buf.readerIndex());
      buf.skipBytes(2);
      return frame;
    }
  }
  return null;
}

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

out.add(msg);
} else if (!isComplete && frag.isReadable()) {
      isUnordered,
      Unpooled.wrappedBuffer(frag, byteBuf));
  out.add(assembledMsg);
} else {
byteBuf.retain();

相关文章

微信公众号

最新文章

更多

ByteBuf类方法