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

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

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

ByteBuf.hasArray介绍

[英]Returns true if and only if this buffer has a backing byte array. If this method returns true, you can safely call #array() and #arrayOffset().
[中]当且仅当此缓冲区具有后备字节数组时返回true。如果此方法返回true,则可以安全地调用#array()和#arrayOffset()。

代码示例

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

@Override
public int getBytesRemainingInBuffer() {
  final ByteBuf buf = this.buf;
  if (buf.hasArray()) {
    return buf.readableBytes();
  } else {
    return -1;
  }
}

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

@Override
public int getBufferPosition() {
  final ByteBuf buf = this.buf;
  if (!buf.hasArray())  {
    return 0;
  } else {
    return buf.arrayOffset() + buf.readerIndex();
  }
}

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

@Override
public byte[] array() {
  if (byteBuf.hasArray()) {
    // 堆内 ByteBuf
    return byteBuf.array();
  } else {
    // 堆外 ByteBuf
    byte[] bs = new byte[byteBuf.readableBytes()];
    byteBuf.readBytes(bs);
    return bs;
  }
}

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

@Override
  public void update(ByteBuf b, int off, int len) {
    if (b.hasArray()) {
      update(b.array(), b.arrayOffset() + off, len);
    } else {
      try {
        method.invoke(checksum, CompressionUtil.safeNioBuffer(b));
      } catch (Throwable cause) {
        throw new Error();
      }
    }
  }
}

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

if (!undecodedChunk.hasArray()) {
  return readLineStandard(undecodedChunk, charset);
int readerIndex = undecodedChunk.readerIndex();
try {
  ByteBuf line = buffer(64);
  undecodedChunk.readerIndex(readerIndex);
  throw new NotEnoughDataDecoderException(e);
undecodedChunk.readerIndex(readerIndex);
throw new NotEnoughDataDecoderException();

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

@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: alipay/sofa-rpc

@Override
public byte[] array() {
  if (byteBuf.hasArray()) {
    // 堆内 ByteBuf
    return byteBuf.array();
  } else {
    // 堆外 ByteBuf
    byte[] bs = new byte[byteBuf.readableBytes()];
    byteBuf.readBytes(bs);
    return bs;
  }
}

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

/**
 * @see #update(byte[], int, int)
 */
public void update(ByteBuf b, int off, int len) {
  if (b.hasArray()) {
    update(b.array(), b.arrayOffset() + off, len);
  } else {
    b.forEachByte(off, len, updateProcessor);
  }
}

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

private void inspectByteBufs(ByteBuf[] bufs) {
  for (ByteBuf buf : bufs) {
   if (!buf.hasArray()) {
    withoutArray = true;
    break;
   }
   totalSize += buf.readableBytes();
   totalNioBuffers += buf.nioBufferCount();
  }
 }
}

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

if (!undecodedChunk.hasArray()) {
  return loadDataMultipartStandard(undecodedChunk, delimiter, httpData);
final int startReaderIndex = undecodedChunk.readerIndex();
final int delimeterLength = delimiter.length();
int index = 0;
  throw new ErrorDataDecoderException(e);
undecodedChunk.readerIndex(lastPosition);
return delimiterFound;

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

@Override
protected void channelRead0(ChannelHandlerContext context,
    FullHttpResponse response) throws Exception {
  byte[] content = null;
  ByteBuf byteBuf = response.content();
  if (byteBuf.hasArray()) {
    content = byteBuf.array();
  } else {
    content = new byte[byteBuf.readableBytes()];
    byteBuf.readBytes(content);
  }
  future.set(new Netty4HttpResponse(context, response, content));
}

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

@Override
  public void update(ByteBuf b, int off, int len) {
    if (b.hasArray()) {
      update(b.array(), b.arrayOffset() + off, len);
    } else {
      try {
        method.invoke(checksum, CompressionUtil.safeNioBuffer(b));
      } catch (Throwable cause) {
        throw new Error();
      }
    }
  }
}

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

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

/**
 * @see #update(byte[], int, int)
 */
public void update(ByteBuf b, int off, int len) {
  if (b.hasArray()) {
    update(b.array(), b.arrayOffset() + off, len);
  } else {
    b.forEachByte(off, len, updateProcessor);
  }
}

相关文章

ByteBuf类方法