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

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

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

ByteBuf.arrayOffset介绍

[英]Returns the offset of the first byte within the backing byte array of this buffer.
[中]返回此缓冲区的备份字节数组中第一个字节的偏移量。

代码示例

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

private void deflate(ByteBuf out) {
  int numBytes;
  do {
    int writerIndex = out.writerIndex();
    numBytes = deflater.deflate(
        out.array(), out.arrayOffset() + writerIndex, out.writableBytes(), Deflater.SYNC_FLUSH);
    out.writerIndex(writerIndex + numBytes);
  } while (numBytes > 0);
}

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

static int setBytes(AbstractByteBuf buf, long addr, int index, InputStream in, int length) throws IOException {
  buf.checkIndex(index, length);
  ByteBuf tmpBuf = buf.alloc().heapBuffer(length);
  try {
    byte[] tmp = tmpBuf.array();
    int offset = tmpBuf.arrayOffset();
    int readBytes = in.read(tmp, offset, length);
    if (readBytes > 0) {
      PlatformDependent.copyMemory(tmp, offset, addr, readBytes);
    }
    return readBytes;
  } finally {
    tmpBuf.release();
  }
}

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

private void deflate(ByteBuf out) {
  int numBytes;
  do {
    int writerIndex = out.writerIndex();
    numBytes = deflater.deflate(
        out.array(), out.arrayOffset() + writerIndex, out.writableBytes(), Deflater.SYNC_FLUSH);
    out.writerIndex(writerIndex + numBytes);
  } while (numBytes > 0);
}

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

static void getBytes(AbstractByteBuf buf, long addr, int index, OutputStream out, int length) throws IOException {
  buf.checkIndex(index, length);
  if (length != 0) {
    int len = Math.min(length, ByteBufUtil.WRITE_CHUNK_SIZE);
    if (len <= ByteBufUtil.MAX_TL_ARRAY_LEN || !buf.alloc().isDirectBufferPooled()) {
      getBytes(addr, ByteBufUtil.threadLocalTempArray(len), 0, len, out, length);
    } else {
      // if direct buffers are pooled chances are good that heap buffers are pooled as well.
      ByteBuf tmpBuf = buf.alloc().heapBuffer(len);
      try {
        byte[] tmp = tmpBuf.array();
        int offset = tmpBuf.arrayOffset();
        getBytes(addr, tmp, offset, len, out, length);
      } finally {
        tmpBuf.release();
      }
    }
  }
}

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

/**
 * @param buffer buffer with a backing byte array
 */
SeekAheadOptimize(ByteBuf buffer) {
  if (!buffer.hasArray()) {
    throw new IllegalArgumentException("buffer hasn't backing byte array");
  }
  this.buffer = buffer;
  bytes = buffer.array();
  readerIndex = buffer.readerIndex();
  origPos = pos = buffer.arrayOffset() + readerIndex;
  limit = buffer.arrayOffset() + buffer.writerIndex();
}

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

private static void deflate(Deflater deflater, ByteBuf out) {
  int numBytes;
  do {
    int writerIndex = out.writerIndex();
    numBytes = deflater.deflate(out.array(), out.arrayOffset() + writerIndex, out.writableBytes(),
        Deflater.SYNC_FLUSH);
    out.writerIndex(writerIndex + numBytes);
  } while (numBytes > 0);
}

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

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

private boolean compressInto(ByteBuf compressed) {
  byte[] out = compressed.array();
  int off = compressed.arrayOffset() + compressed.writerIndex();
  int toWrite = compressed.writableBytes();
  int numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
  compressed.writerIndex(compressed.writerIndex() + numBytes);
  return numBytes == toWrite;
}

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