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

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

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

ByteBuf.release介绍

暂无

代码示例

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

static ByteBuf expandCumulation(ByteBufAllocator alloc, ByteBuf cumulation, int readable) {
  ByteBuf oldCumulation = cumulation;
  cumulation = alloc.buffer(oldCumulation.readableBytes() + readable);
  cumulation.writeBytes(oldCumulation);
  oldCumulation.release();
  return cumulation;
}

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

/**
 * Read the given amount of bytes into a new {@link ByteBuf} that is allocated from the {@link ByteBufAllocator}.
 */
public static ByteBuf readBytes(ByteBufAllocator alloc, ByteBuf buffer, int length) {
  boolean release = true;
  ByteBuf dst = alloc.buffer(length);
  try {
    buffer.readBytes(dst);
    release = false;
    return dst;
  } finally {
    if (release) {
      dst.release();
    }
  }
}

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

private void writeChunk(ByteBuf chunk, ByteBuf out)
{
  try
  {
    out.writeBytes(chunk);
  }
  finally
  {
    chunk.release();
  }
}

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

private void end(ChannelHandlerContext ctx, ByteBuf buf, boolean h2c) {
 if (current > 0) {
  ByteBuf msg = Unpooled.buffer(current + buf.readableBytes());
  msg.writeBytes(HTTP_2_PREFACE_ARRAY, 0, current);
  msg.writeBytes(buf);
  buf.release();
  buf = msg;
 }
 configure(ctx, h2c);
 ctx.pipeline().remove(this);
 ctx.fireChannelRead(buf);
}

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

checkNotNull(buf, "buf");
checkNotNull(charset, "charset");
final int maxIndex = buf.readerIndex() + buf.readableBytes();
if (index < 0 || length < 0 || index > maxIndex - length) {
  throw new IndexOutOfBoundsException("index: " + index + " length: " + length);
      ByteBuf heapBuffer = buf.alloc().heapBuffer(length);
      try {
        heapBuffer.writeBytes(buf, index, length);
        decoder.decode(heapBuffer.internalNioBuffer(heapBuffer.readerIndex(), length));
      } finally {
        heapBuffer.release();

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

buffer.release();
if (length > buffer.readableBytes()) {
  if (releaseOnClose) {
    buffer.release();
      + length + ", maximum is " + buffer.readableBytes());
startIndex = buffer.readerIndex();
endIndex = startIndex + length;
buffer.markReaderIndex();

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

@Override
public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
  if (decodeState == STATE_CALLING_CHILD_DECODE) {
    decodeState = STATE_HANDLER_REMOVED_PENDING;
    return;
  }
  ByteBuf buf = cumulation;
  if (buf != null) {
    // Directly set this to null so we are sure we not access it in any other method here anymore.
    cumulation = null;
    int readable = buf.readableBytes();
    if (readable > 0) {
      ByteBuf bytes = buf.readBytes(readable);
      buf.release();
      ctx.fireChannelRead(bytes);
    } else {
      buf.release();
    }
    numReads = 0;
    ctx.fireChannelReadComplete();
  }
  handlerRemoved0(ctx);
}

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

@Override
public ByteBuf copy(int index, int length) {
  checkIndex(index, length);
  boolean release = true;
  ByteBuf buf = alloc().buffer(length);
  try {
    buf.writeBytes(this, index, length);
    release = false;
    return buf;
  } finally {
    if (release) {
      buf.release();
    }
  }
}

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

if (finished) {
  in.skipBytes(in.readableBytes());
  return;
final int inputLength = in.readableBytes();
if (inputLength == 0) {
  return;
  if (in.hasArray()) {
    z.next_in = in.array();
    z.next_in_index = in.arrayOffset() + in.readerIndex();
  } else {
    byte[] array = new byte[inputLength];
    in.getBytes(in.readerIndex(), array);
    z.next_in = array;
    z.next_in_index = 0;
    if (decompressed.isReadable()) {
      out.add(decompressed);
    } else {
      decompressed.release();

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

public static byte[] hash128toArray(ByteBuf objectState) {
  long[] hash = hash128(objectState);
  ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE);
  try {
    buf.writeLong(hash[0]).writeLong(hash[1]);
    byte[] dst = new byte[buf.readableBytes()];
    buf.readBytes(dst);
    return dst;
  } finally {
    buf.release();
  }
}

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

@Override
public ByteBuf capacity(int newCapacity) {
 if (newCapacity > capacity()) {
  ByteBuf newBuf = allocator.buffer(newCapacity);
  newBuf.writeBytes(buffer, 0, buffer.capacity());
  newBuf.readerIndex(buffer.readerIndex());
  newBuf.writerIndex(buffer.writerIndex());
  buffer.release();
  buffer = newBuf;
  return newBuf;
 } else {
  return super.capacity(newCapacity);
 }
}

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

private int readInt() {
  unprocessedBytes -= 4;
  assert unprocessed != null;
  final ByteBuf firstBuf = unprocessed.peek();
  assert firstBuf != null;
  final int firstBufLen = firstBuf.readableBytes();
  if (firstBufLen >= 4) {
    final int value = firstBuf.readInt();
    if (!firstBuf.isReadable()) {
      unprocessed.remove().release();
    }
    return value;
  }
  return readIntSlowPath();
}

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

dst = alloc.heapBuffer(length);
} else {
  dst = alloc.buffer(length);
  final ByteBuffer dstBuf = dst.internalNioBuffer(dst.readerIndex(), length);
  final int pos = dstBuf.position();
  CoderResult cr = encoder.encode(src, dstBuf, true);
} finally {
  if (release) {
    dst.release();

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

@Override
  public Object decode(ByteBuf buf, State state) throws IOException {
    ByteBuf out = ByteBufAllocator.DEFAULT.buffer();
    try {
      while (buf.isReadable()) {
        int chunkSize = buf.readInt();
        ByteBuf chunk = buf.readSlice(chunkSize);
        snappyDecoder.get().decode(chunk, out);
        snappyDecoder.get().reset();
      }
      return innerCodec.getValueDecoder().decode(out, state);
    } finally {
      snappyDecoder.get().reset();
      out.release();
    }
  }
};

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

/**
 * Read plaintext data from the OpenSSL internal BIO
 */
private int readPlaintextData(final ByteBuffer dst) {
  final int sslRead;
  final int pos = dst.position();
  if (dst.isDirect()) {
    sslRead = SSL.readFromSSL(ssl, bufferAddress(dst) + pos, dst.limit() - pos);
    if (sslRead > 0) {
      dst.position(pos + sslRead);
    }
  } else {
    final int limit = dst.limit();
    final int len = min(maxEncryptedPacketLength0(), limit - pos);
    final ByteBuf buf = alloc.directBuffer(len);
    try {
      sslRead = SSL.readFromSSL(ssl, memoryAddress(buf), len);
      if (sslRead > 0) {
        dst.limit(pos + sslRead);
        buf.getBytes(buf.readerIndex(), dst);
        dst.limit(limit);
      }
    } finally {
      buf.release();
    }
  }
  return sslRead;
}

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

/**
 * Compose {@code cumulation} and {@code next} into a new {@link ByteBufAllocator#ioBuffer()}.
 * @param alloc The allocator to use to allocate the new buffer.
 * @param cumulation The current cumulation.
 * @param next The next buffer.
 * @return The result of {@code cumulation + next}.
 */
protected final ByteBuf copyAndCompose(ByteBufAllocator alloc, ByteBuf cumulation, ByteBuf next) {
  ByteBuf newCumulation = alloc.ioBuffer(cumulation.readableBytes() + next.readableBytes());
  try {
    newCumulation.writeBytes(cumulation).writeBytes(next);
  } catch (Throwable cause) {
    newCumulation.release();
    safeRelease(next);
    throwException(cause);
  }
  cumulation.release();
  next.release();
  return newCumulation;
}

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

checkNotNull(buf, "buf");
checkNotNull(charset, "charset");
final int maxIndex = buf.readerIndex() + buf.readableBytes();
if (index < 0 || length < 0 || index > maxIndex - length) {
  throw new IndexOutOfBoundsException("index: " + index + " length: " + length);
      ByteBuf heapBuffer = buf.alloc().heapBuffer(length);
      try {
        heapBuffer.writeBytes(buf, index, length);
        decoder.decode(heapBuffer.internalNioBuffer(heapBuffer.readerIndex(), length));
      } finally {
        heapBuffer.release();

相关文章

ByteBuf类方法