java.nio.ByteBuffer.slice()方法的使用及代码示例

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

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

ByteBuffer.slice介绍

[英]Returns a sliced buffer that shares its content with this buffer.

The sliced buffer's capacity will be this buffer's remaining(), and it's zero position will correspond to this buffer's current position. The new buffer's position will be 0, limit will be its capacity, and its mark is cleared. The new buffer's read-only property and byte order are the same as this buffer's.

The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffer's position, limit and mark are independent.
[中]返回与此缓冲区共享其内容的切片缓冲区。
切片缓冲区的容量将是该缓冲区的剩余容量(),其零位置将对应于该缓冲区的当前位置。新缓冲区的位置将为0,限制将为其容量,其标记将被清除。新缓冲区的只读属性和字节顺序与此缓冲区的相同。
新缓冲区与此缓冲区共享其内容,这意味着任何一个缓冲区的内容更改都将对另一个缓冲区可见。两个缓冲器的位置、极限和标记是独立的。

代码示例

代码示例来源:origin: spring-projects/spring-framework

private DefaultDataBuffer(DefaultDataBufferFactory dataBufferFactory, ByteBuffer byteBuffer) {
  Assert.notNull(dataBufferFactory, "DefaultDataBufferFactory must not be null");
  Assert.notNull(byteBuffer, "ByteBuffer must not be null");
  this.dataBufferFactory = dataBufferFactory;
  ByteBuffer slice = byteBuffer.slice();
  this.byteBuffer = slice;
  this.capacity = slice.remaining();
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public ByteBuffer toByteBuffer(int index, int length) {
  if (index == 0 && length == capacity()) {
    return buffer.duplicate();
  } else {
    return ((ByteBuffer) buffer.duplicate().position(
        index).limit(index + length)).slice();
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public ByteBuffer asByteBuffer(int index, int length) {
  checkIndex(index, length);
  ByteBuffer duplicate = this.byteBuffer.duplicate();
  // Explicit access via Buffer base type for compatibility
  // with covariant return type on JDK 9's ByteBuffer...
  Buffer buffer = duplicate;
  buffer.position(index);
  buffer.limit(index + length);
  return duplicate.slice();
}

代码示例来源:origin: apache/incubator-druid

public static ByteBuffer getSlice(ByteBuffer buffer, int sliceSize, int i)
 {
  final ByteBuffer slice = buffer.duplicate();
  slice.position(sliceSize * i);
  slice.limit(slice.position() + sliceSize);
  return slice.slice();
 }
}

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

@Override
public IntBuffer slice () {
  byteBuffer.limit(limit << 2);
  byteBuffer.position(position << 2);
  IntBuffer result = new IntToByteBufferAdapter(byteBuffer.slice());
  byteBuffer.clear();
  return result;
}

代码示例来源:origin: sannies/mp4parser

@Override
public void parseDetail(ByteBuffer bb) throws IOException {
  data = bb.slice();
  bb.position(bb.position() + data.remaining());
}

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

ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
  super(buffer.remaining());
  if (!buffer.isReadOnly()) {
    throw new IllegalArgumentException("must be a readonly buffer: " + StringUtil.simpleClassName(buffer));
  }
  this.allocator = allocator;
  this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN);
  writerIndex(this.buffer.limit());
}

代码示例来源:origin: sannies/mp4parser

@Override
public void _parseDetails(ByteBuffer content) {
  int boxSize = content.remaining();
  data = content.slice(); //Keep this in case we fail to parse
  successfulParse = false;
  try {
    tags.clear();
    while (content.remaining() > 0) {
      XtraTag tag = new XtraTag();
      tag.parse(content);
      tags.addElement(tag);
    }
    int calcSize = detailSize();
    if (boxSize != calcSize) {
      throw new RuntimeException("Improperly handled Xtra tag: Calculated sizes don't match ( " + boxSize + "/" + calcSize + ")");
    }
    successfulParse = true;
  } catch (Exception e) {
    successfulParse = false;
    LOG.error("Malformed Xtra Tag detected: {}", e.toString());
    content.position(content.position() + content.remaining());
  } finally {
    content.order(ByteOrder.BIG_ENDIAN); //Just in case we bailed out mid-parse we don't want to leave the byte order in MS land
  }
}

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

/**
 * Creates a new direct buffer by wrapping the specified initial buffer.
 *
 * @param maxCapacity the maximum capacity of the underlying direct buffer
 */
protected UnpooledDirectByteBuf(ByteBufAllocator alloc, ByteBuffer initialBuffer, int maxCapacity) {
  super(maxCapacity);
  if (alloc == null) {
    throw new NullPointerException("alloc");
  }
  if (initialBuffer == null) {
    throw new NullPointerException("initialBuffer");
  }
  if (!initialBuffer.isDirect()) {
    throw new IllegalArgumentException("initialBuffer is not a direct buffer.");
  }
  if (initialBuffer.isReadOnly()) {
    throw new IllegalArgumentException("initialBuffer is a read-only buffer.");
  }
  int initialCapacity = initialBuffer.remaining();
  if (initialCapacity > maxCapacity) {
    throw new IllegalArgumentException(String.format(
        "initialCapacity(%d) > maxCapacity(%d)", initialCapacity, maxCapacity));
  }
  this.alloc = alloc;
  doNotFree = true;
  setByteBuffer(initialBuffer.slice().order(ByteOrder.BIG_ENDIAN));
  writerIndex(initialCapacity);
}

代码示例来源:origin: Meituan-Dianping/walle

/**
   * Sets the offset of the start of the ZIP Central Directory in the APK's ZIP End of Central
   * Directory record.
   *
   * @param zipEndOfCentralDirectory APK's ZIP End of Central Directory record
   * @param offset offset of the ZIP Central Directory relative to the start of the archive. Must
   *        be between {@code 0} and {@code 2^32 - 1} inclusive.
   */
  public static void setZipEocdCentralDirectoryOffset(
      ByteBuffer zipEndOfCentralDirectory, long offset) {
    ByteBuffer eocd = zipEndOfCentralDirectory.slice();
    eocd.order(ByteOrder.LITTLE_ENDIAN);
    ZipUtils.setZipEocdCentralDirectoryOffset(eocd, offset);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public DefaultDataBuffer slice(int index, int length) {
  checkIndex(index, length);
  int oldPosition = this.byteBuffer.position();
  // Explicit access via Buffer base type for compatibility
  // with covariant return type on JDK 9's ByteBuffer...
  Buffer buffer = this.byteBuffer;
  try {
    buffer.position(index);
    ByteBuffer slice = this.byteBuffer.slice();
    // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
    ((Buffer) slice).limit(length);
    return new SlicedDefaultDataBuffer(slice, this.dataBufferFactory, length);
  }
  finally {
    buffer.position(oldPosition);
  }
}

代码示例来源:origin: apache/incubator-druid

public void writeBytesNoPaddingTo(HeapByteBufferWriteOutBytes out)
{
 ByteBuffer toWrite = buffer.slice();
 toWrite.limit(toWrite.limit() - (Integer.BYTES - numBytes));
 out.write(toWrite);
}

代码示例来源:origin: apache/incubator-druid

@Override
public Object get(ByteBuffer buf, int position)
{
 ByteBuffer mutationBuffer = buf.duplicate();
 mutationBuffer.position(position);
 // | k (byte) | numLongs (int) | bitset (long[numLongs]) |
 int sizeBytes = 1 + Integer.BYTES + (buf.getInt(position + 1) * Long.BYTES);
 mutationBuffer.limit(position + sizeBytes);
 return mutationBuffer.slice();
}

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

@Override
public IntBuffer slice () {
  byteBuffer.limit(limit << 2);
  byteBuffer.position(position << 2);
  IntBuffer result = new IntToByteBufferAdapter(byteBuffer.slice());
  byteBuffer.clear();
  return result;
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public ByteBuffer toByteBuffer(int index, int length) {
  if (index == 0 && length == capacity()) {
    return buffer.duplicate();
  } else {
    return ((ByteBuffer) buffer.duplicate().position(
        index).limit(index + length)).slice();
  }
}

代码示例来源:origin: sannies/mp4parser

@Override
public void _parseDetails(ByteBuffer content) {
  data = content.slice();
  content.position(content.position() + content.remaining());
}

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

ReadOnlyByteBufferBuf(ByteBufAllocator allocator, ByteBuffer buffer) {
  super(buffer.remaining());
  if (!buffer.isReadOnly()) {
    throw new IllegalArgumentException("must be a readonly buffer: " + StringUtil.simpleClassName(buffer));
  }
  this.allocator = allocator;
  this.buffer = buffer.slice().order(ByteOrder.BIG_ENDIAN);
  writerIndex(this.buffer.limit());
}

代码示例来源:origin: Meituan-Dianping/walle

/**
 * Constructs a new {@code ByteBufferDigestSource} based on the data contained in the provided
 * buffer between the buffer's position and limit.
 */
private ByteBufferDataSource(ByteBuffer buffer, boolean sliceRequired) {
  mBuffer = (sliceRequired) ? buffer.slice() : buffer;
  mSize = buffer.remaining();
}

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

/**
 * Creates a new buffer which wraps the specified buffer's slice.
 */
public ByteBufferBackedChannelBuffer(ByteBuffer buffer) {
  if (buffer == null) {
    throw new NullPointerException("buffer");
  }
  order = buffer.order();
  this.buffer = buffer.slice().order(order);
  capacity = buffer.remaining();
  writerIndex(capacity);
}

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

static ShortBuffer asShortBuffer(ByteBuffer byteBuffer) {
  ByteBuffer slice = byteBuffer.slice();
  slice.order(byteBuffer.order());
  return new ByteBufferAsShortBuffer(slice);
}

相关文章

微信公众号

最新文章

更多