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

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

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

ByteBuf.getBytes介绍

[英]Transfers this buffer's data to the specified destination starting at the specified absolute index until the destination becomes non-writable. This method is basically same with #getBytes(int,ByteBuf,int,int), except that this method increases the writerIndex of the destination by the number of the transferred bytes while #getBytes(int,ByteBuf,int,int) does not. This method does not modify readerIndex or writerIndex of the source buffer (i.e. this).
[中]从指定的绝对索引开始将此缓冲区的数据传输到指定的目标,直到目标变为不可写。此方法与#getBytes(int,ByteBuf,int,int)基本相同,只是此方法将目标的writerIndex增加传输的字节数,而#getBytes(int,ByteBuf,int,int)不增加。此方法不会修改源缓冲区的readerIndex或writerIndex(即此)。

代码示例

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

public Entry(ByteBuf keyBuf, ByteBuf valueBuf) {
  key = new byte[keyBuf.readableBytes()];
  keyBuf.getBytes(keyBuf.readerIndex(), key);
  
  value = new byte[valueBuf.readableBytes()];
  valueBuf.getBytes(valueBuf.readerIndex(), value);
}

代码示例来源:origin: Graylog2/graylog2-server

private void extractData() {
  final int length = payload.readableBytes() - HEADER_TOTAL_LENGTH;
  final byte[] buf = new byte[length];
  // The rest of the payload is data.
  payload.getBytes(HEADER_TOTAL_LENGTH, buf, 0, length);
  this.data = buf;
}

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

public static byte[] byteBuf2Bytes(ByteBuf buf) {
 int readable = buf.readableBytes();
 int readerIndex = buf.readerIndex();
 if (buf.hasArray()) {
  byte[] array = buf.array();
  if (buf.arrayOffset() == 0 && readerIndex == 0 && array.length == readable) {
   return array;
  }
 }
 byte[] array = new byte[readable];
 buf.getBytes(readerIndex, array);
 return array;
}

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

@SuppressWarnings("deprecation")
static String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) {
  if (len == 0) {
    return StringUtil.EMPTY_STRING;
  }
  final byte[] array;
  final int offset;
  if (src.hasArray()) {
    array = src.array();
    offset = src.arrayOffset() + readerIndex;
  } else {
    array = threadLocalTempArray(len);
    offset = 0;
    src.getBytes(readerIndex, array, 0, len);
  }
  if (CharsetUtil.US_ASCII.equals(charset)) {
    // Fast-path for US-ASCII which is used frequently.
    return new String(array, 0, offset, len);
  }
  return new String(array, offset, len, charset);
}

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

@Override
public byte[] get() {
  if (byteBuf == null) {
    return EMPTY_BUFFER.array();
  }
  byte[] array = new byte[byteBuf.readableBytes()];
  byteBuf.getBytes(byteBuf.readerIndex(), array);
  return array;
}

代码示例来源:origin: alipay/sofa-bolt

private void checkCRC(ByteBuf in, int startIndex) {
  int endIndex = in.readerIndex();
  int expectedCrc = in.readInt();
  byte[] frame = new byte[endIndex - startIndex];
  in.getBytes(startIndex, frame, 0, endIndex - startIndex);
  int actualCrc = CrcUtil.crc32(frame);
  if (expectedCrc != actualCrc) {
    String err = "CRC check failed!";
    logger.error(err);
    throw new RuntimeException(err);
  }
}

代码示例来源:origin: apache/servicecomb-java-chassis

public static byte[] getBytesFast(ByteBuf byteBuf) {
 if (byteBuf.hasArray()) {
  return byteBuf.array();
 }
 byte[] arr = new byte[byteBuf.writerIndex()];
 byteBuf.getBytes(0, arr);
 return arr;
}

代码示例来源:origin: Netflix/zuul

private void write(ByteBuf bb) throws IOException {
  byte[] bytes;
  int offset;
  final int length = bb.readableBytes();
  if (bb.hasArray()) {
    /* avoid memory copy if possible */
    bytes = bb.array();
    offset = bb.arrayOffset();
  } else {
    bytes = new byte[length];
    bb.getBytes(bb.readerIndex(), bytes);
    offset = 0;
  }
  gzos.write(bytes, offset, length);
}

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

public Entry(ByteBuf keyBuf, ByteBuf valueBuf) {
  key = new byte[keyBuf.readableBytes()];
  keyBuf.getBytes(keyBuf.readerIndex(), key);
  
  value = new byte[valueBuf.readableBytes()];
  valueBuf.getBytes(valueBuf.readerIndex(), value);
}

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

@Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
     // copy the ByteBuf content to a byte array
    byte[] array = new byte[msg.readableBytes()];
    msg.getBytes(0, array);

    out.add(array);
  }
}

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

@SuppressWarnings("deprecation")
static String decodeString(ByteBuf src, int readerIndex, int len, Charset charset) {
  if (len == 0) {
    return StringUtil.EMPTY_STRING;
  }
  final byte[] array;
  final int offset;
  if (src.hasArray()) {
    array = src.array();
    offset = src.arrayOffset() + readerIndex;
  } else {
    array = threadLocalTempArray(len);
    offset = 0;
    src.getBytes(readerIndex, array, 0, len);
  }
  if (CharsetUtil.US_ASCII.equals(charset)) {
    // Fast-path for US-ASCII which is used frequently.
    return new String(array, 0, offset, len);
  }
  return new String(array, offset, len, charset);
}

代码示例来源: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: 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: stackoverflow.com

ByteBuf buf = ...
byte[] bytes;
int offset;
int length = buf.readableBytes();

if (buf.hasArray()) {
  bytes = buf.array();
  offset = buf.arrayOffset();
} else {
  bytes = new byte[length];
  buf.getBytes(buf.readerIndex(), bytes);
  offset = 0;
}

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

public LocalCachedMapUpdate(ByteBuf keyBuf, ByteBuf valueBuf) {
  byte[] key = new byte[keyBuf.readableBytes()];
  keyBuf.getBytes(keyBuf.readerIndex(), key);
  
  byte[] value = new byte[valueBuf.readableBytes()];
  valueBuf.getBytes(valueBuf.readerIndex(), value);
  entries = Collections.singletonList(new Entry(key, value));
}

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

@Override
public void writeObject(Object obj) throws IOException {
  ByteBuf buf = Unpooled.buffer(estimatedLength);
  try {
    ObjectOutputStream oout = new CompactObjectOutputStream(new ByteBufOutputStream(buf));
    try {
      oout.writeObject(obj);
      oout.flush();
    } finally {
      oout.close();
    }
    int objectSize = buf.readableBytes();
    writeInt(objectSize);
    buf.getBytes(0, this, objectSize);
  } finally {
    buf.release();
  }
}

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

/**
 * 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: wildfly/wildfly

private int setInput(ByteBuf compressed) {
  int len = compressed.readableBytes();
  if (compressed.hasArray()) {
    decompressor.setInput(compressed.array(), compressed.arrayOffset() + compressed.readerIndex(), len);
  } else {
    byte[] in = new byte[len];
    compressed.getBytes(compressed.readerIndex(), in);
    decompressor.setInput(in, 0, in.length);
  }
  return len;
}

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

public LocalCachedMapUpdate(ByteBuf keyBuf, ByteBuf valueBuf) {
  byte[] key = new byte[keyBuf.readableBytes()];
  keyBuf.getBytes(keyBuf.readerIndex(), key);
  
  byte[] value = new byte[valueBuf.readableBytes()];
  valueBuf.getBytes(valueBuf.readerIndex(), value);
  entries = Collections.singletonList(new Entry(key, value));
}

相关文章

ByteBuf类方法