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

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

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

ByteBuf.capacity介绍

[英]Returns the number of bytes (octets) this buffer can contain.
[中]返回此缓冲区可以包含的字节数(八位字节)。

代码示例

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

@Override
public byte[] getData() {
  if (payload.arrayOffset() == 0 && payload.capacity() == payload.array().length) {
    return payload.array();
  } else {
    // Need to copy into a smaller byte array
    byte[] data = new byte[payload.readableBytes()];
    payload.readBytes(data);
    return data;
  }
}

代码示例来源:origin: AppliedEnergistics/Applied-Energistics-2

public ByteArrayInputStream getPacketByteArray( ByteBuf stream, int readerIndex, int readableBytes )
{
  final ByteArrayInputStream bytes;
  if( stream.hasArray() )
  {
    bytes = new ByteArrayInputStream( stream.array(), readerIndex, readableBytes );
  }
  else
  {
    byte[] data = new byte[stream.capacity()];
    stream.getBytes( readerIndex, data, 0, readableBytes );
    bytes = new ByteArrayInputStream( data );
  }
  return bytes;
}

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

public NettyByteBufBuffer(ByteBuf buf) {
  super(buf.array(), buf.arrayOffset(), buf.capacity());
  this.buf = buf;
}

代码示例来源:origin: AppliedEnergistics/Applied-Energistics-2

/**
 * This builds a tag with the actual data that should be sent to the client for update syncs.
 * If the tile entity doesn't need update syncs, it returns null.
 */
private NBTTagCompound writeUpdateData()
{
  final NBTTagCompound data = new NBTTagCompound();
  final ByteBuf stream = Unpooled.buffer();
  try
  {
    this.writeToStream( stream );
    if( stream.readableBytes() == 0 )
    {
      return null;
    }
  }
  catch( final Throwable t )
  {
    AELog.debug( t );
  }
  stream.capacity( stream.readableBytes() );
  data.setByteArray( "X", stream.array() );
  return data;
}

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

@Override
  protected void encode(
      ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception {
    final int size = msg.getSerializedSize();
    final ByteBuf buffer = ctx.alloc().heapBuffer(size, size);
    final byte[] array = buffer.array();
    CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array,
        buffer.arrayOffset(), buffer.capacity());
    msg.writeTo(cobbn);
    buffer.writerIndex(size);
    out.add(buffer);
  }
}

代码示例来源:origin: p455w0rd/WirelessCraftingTerminal

public ByteArrayInputStream getPacketByteArray(ByteBuf stream, int readerIndex, int readableBytes) {
  final ByteArrayInputStream bytes;
  if (stream.hasArray()) {
    bytes = new ByteArrayInputStream(stream.array(), readerIndex, readableBytes);
  }
  else {
    byte[] data = new byte[stream.capacity()];
    stream.getBytes(readerIndex, data, 0, readableBytes);
    bytes = new ByteArrayInputStream(data);
  }
  return bytes;
}

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

@Override
  protected void encode(
      ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception {
    final int size = msg.getSerializedSize();
    final ByteBuf buffer = ctx.alloc().heapBuffer(size, size);
    final byte[] array = buffer.array();
    CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array,
        buffer.arrayOffset(), buffer.capacity());
    msg.writeTo(cobbn);
    buffer.writerIndex(size);
    out.add(buffer);
  }
}

代码示例来源:origin: apache/activemq-artemis

@Before
public void before() {
 BYTE_ENCODE = Unpooled.wrappedBuffer(Base64.decode(STRING_ENCODE, Base64.DONT_BREAK_LINES | Base64.URL_SAFE));
 // some extra caution here, nothing else, to make sure we would get the same encoding back
 Assert.assertEquals(STRING_ENCODE, encodeString(BYTE_ENCODE.array()));
 BYTE_ENCODE.readerIndex(0).writerIndex(BYTE_ENCODE.capacity());
}

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

/**
 * Return an array of the underlying storage from {@code buf} into a byte array.
 * The copy will start at {@code start} and copy {@code length} bytes.
 * If {@code copy} is true a copy will be made of the memory.
 * If {@code copy} is false the underlying storage will be shared, if possible.
 */
public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) {
  int capacity = buf.capacity();
  if (isOutOfBounds(start, length, capacity)) {
    throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length
        + ") <= " + "buf.capacity(" + capacity + ')');
  }
  if (buf.hasArray()) {
    if (copy || start != 0 || length != capacity) {
      int baseOffset = buf.arrayOffset() + start;
      return Arrays.copyOfRange(buf.array(), baseOffset, baseOffset + length);
    } else {
      return buf.array();
    }
  }
  byte[] v = PlatformDependent.allocateUninitializedArray(length);
  buf.getBytes(start, v);
  return v;
}

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

/**
 * Return an array of the underlying storage from {@code buf} into a byte array.
 * The copy will start at {@code start} and copy {@code length} bytes.
 * If {@code copy} is true a copy will be made of the memory.
 * If {@code copy} is false the underlying storage will be shared, if possible.
 */
public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) {
  if (isOutOfBounds(start, length, buf.capacity())) {
    throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length
        + ") <= " + "buf.capacity(" + buf.capacity() + ')');
  }
  if (buf.hasArray()) {
    if (copy || start != 0 || length != buf.capacity()) {
      int baseOffset = buf.arrayOffset() + start;
      return Arrays.copyOfRange(buf.array(), baseOffset, baseOffset + length);
    } else {
      return buf.array();
    }
  }
  byte[] v = new byte[length];
  buf.getBytes(start, v);
  return v;
}

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

@Override
  protected void encode(
      ChannelHandlerContext ctx, MessageNano msg, List<Object> out) throws Exception {
    final int size = msg.getSerializedSize();
    final ByteBuf buffer = ctx.alloc().heapBuffer(size, size);
    final byte[] array = buffer.array();
    CodedOutputByteBufferNano cobbn = CodedOutputByteBufferNano.newInstance(array,
        buffer.arrayOffset(), buffer.capacity());
    msg.writeTo(cobbn);
    buffer.writerIndex(size);
    out.add(buffer);
  }
}

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

/**
 * Return an array of the underlying storage from {@code buf} into a byte array.
 * The copy will start at {@code start} and copy {@code length} bytes.
 * If {@code copy} is true a copy will be made of the memory.
 * If {@code copy} is false the underlying storage will be shared, if possible.
 */
public static byte[] getBytes(ByteBuf buf, int start, int length, boolean copy) {
  int capacity = buf.capacity();
  if (isOutOfBounds(start, length, capacity)) {
    throw new IndexOutOfBoundsException("expected: " + "0 <= start(" + start + ") <= start + length(" + length
        + ") <= " + "buf.capacity(" + capacity + ')');
  }
  if (buf.hasArray()) {
    if (copy || start != 0 || length != capacity) {
      int baseOffset = buf.arrayOffset() + start;
      return Arrays.copyOfRange(buf.array(), baseOffset, baseOffset + length);
    } else {
      return buf.array();
    }
  }
  byte[] v = PlatformDependent.allocateUninitializedArray(length);
  buf.getBytes(start, v);
  return v;
}

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

@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
  checkSrcIndex(index, length, srcIndex, src.capacity());
  if (src.hasMemoryAddress()) {
    PlatformDependent.copyMemory(src.memoryAddress() + srcIndex, array, index, length);
  } else  if (src.hasArray()) {
    setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
  } else {
    src.getBytes(srcIndex, array, index, length);
  }
  return this;
}

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

@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
  checkDstIndex(index, length, dstIndex, dst.capacity());
  if (dst.hasMemoryAddress()) {
    PlatformDependent.copyMemory(array, index, dst.memoryAddress() + dstIndex, length);
  } else if (dst.hasArray()) {
    getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
  } else {
    dst.setBytes(dstIndex, array, index, length);
  }
  return this;
}

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

@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
  checkDstIndex(index, length, dstIndex, dst.capacity());
  if (dst.hasArray()) {
    getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
  } else if (dst.nioBufferCount() > 0) {
    for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) {
      int bbLen = bb.remaining();
      getBytes(index, bb);
      index += bbLen;
    }
  } else {
    dst.setBytes(dstIndex, this, index, length);
  }
  return this;
}

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

@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
  checkDstIndex(index, length, dstIndex, dst.capacity());
  if (dst.hasArray()) {
    getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
  } else if (dst.nioBufferCount() > 0) {
    for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) {
      int bbLen = bb.remaining();
      getBytes(index, bb);
      index += bbLen;
    }
  } else {
    dst.setBytes(dstIndex, this, index, length);
  }
  return this;
}

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

@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
  checkDstIndex(index, length, dstIndex, dst.capacity());
  if (dst.hasArray()) {
    getBytes(index, dst.array(), dst.arrayOffset() + dstIndex, length);
  } else if (dst.nioBufferCount() > 0) {
    for (ByteBuffer bb: dst.nioBuffers(dstIndex, length)) {
      int bbLen = bb.remaining();
      getBytes(index, bb);
      index += bbLen;
    }
  } else {
    dst.setBytes(dstIndex, this, index, length);
  }
  return this;
}

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

@Override
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
  checkSrcIndex(index, length, srcIndex, src.capacity());
  if (src.hasArray()) {
    setBytes(index, src.array(), src.arrayOffset() + srcIndex, length);
  } else if (src.nioBufferCount() > 0) {
    for (ByteBuffer bb: src.nioBuffers(srcIndex, length)) {
      int bbLen = bb.remaining();
      setBytes(index, bb);
      index += bbLen;
    }
  } else {
    src.getBytes(srcIndex, this, index, length);
  }
  return this;
}

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

static void getBytes(AbstractByteBuf buf, long addr, int index, ByteBuf dst, int dstIndex, int length) {
  buf.checkIndex(index, length);
  checkNotNull(dst, "dst");
  if (isOutOfBounds(dstIndex, length, dst.capacity())) {
    throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
  }
  if (dst.hasMemoryAddress()) {
    PlatformDependent.copyMemory(addr, dst.memoryAddress() + dstIndex, length);
  } else if (dst.hasArray()) {
    PlatformDependent.copyMemory(addr, dst.array(), dst.arrayOffset() + dstIndex, length);
  } else {
    dst.setBytes(dstIndex, buf, index, length);
  }
}

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

@Override
public ByteBuf getBytes(int index, ByteBuf dst, int dstIndex, int length) {
  checkIndex(index, length);
  if (dst == null) {
    throw new NullPointerException("dst");
  }
  if (dstIndex < 0 || dstIndex > dst.capacity() - length) {
    throw new IndexOutOfBoundsException("dstIndex: " + dstIndex);
  }
  if (dst.hasMemoryAddress()) {
    PlatformDependent.copyMemory(addr(index), dst.memoryAddress() + dstIndex, length);
  } else if (dst.hasArray()) {
    PlatformDependent.copyMemory(addr(index), dst.array(), dst.arrayOffset() + dstIndex, length);
  } else {
    dst.setBytes(dstIndex, this, index, length);
  }
  return this;
}

相关文章

ByteBuf类方法