java.nio.Buffer.limit()方法的使用及代码示例

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

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

Buffer.limit介绍

[英]limit - 1 is the last element that can be read or written. Limit must be no less than zero and no greater than capacity.
[中]limit - 1是最后一个可以读取或写入的元素。限额必须不小于零且不大于capacity

代码示例

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

@Override
public ByteBuffer nioBuffer(int index, int length) {
  return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
}

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

private void write(ByteBuffer source) {
  int length = source.remaining();
  ByteBuffer tmp = this.byteBuffer.duplicate();
  int limit = this.writePosition + source.remaining();
  ((Buffer) tmp).clear().position(this.writePosition).limit(limit);
  tmp.put(source);
  this.writePosition += length;
}

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

@Override
public ChannelBuffer copy(int index, int length) {
  ByteBuffer src;
  try {
    src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  ByteBuffer dst = buffer.isDirect()
      ? ByteBuffer.allocateDirect(length)
      : ByteBuffer.allocate(length);
  dst.put(src);
  dst.clear();
  return new ByteBufferBackedChannelBuffer(dst);
}

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

private void reallocIfNeeded() {
  if (size == capacity) {
    // Double the capacity while it is "sufficiently small", and otherwise increase by 50%.
    int newLength = capacity <= 65536 ? capacity << 1 : capacity + capacity >> 1;
    ByteBuffer buffer = Buffer.allocateDirectWithNativeOrder(calculateBufferCapacity(newLength));
    // Copy over the old content of the memory and reset the position as we always act on the buffer as if
    // the position was never increased.
    memory.position(0).limit(size);
    buffer.put(memory);
    buffer.position(0);
    Buffer.free(memory);
    memory = buffer;
    memoryAddress = Buffer.memoryAddress(buffer);
    capacity = newLength;
  }
}

代码示例来源:origin: google/ExoPlayer

@Override
public ByteBuffer getOutput() {
 Assertions.checkNotNull(gvrAudioSurround);
 int writtenBytes = gvrAudioSurround.getOutput(buffer, 0, buffer.capacity());
 buffer.position(0).limit(writtenBytes);
 return buffer;
}

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

@Override
public ByteBuffer internalNioBuffer(int index, int length) {
  checkIndex(index, length);
  return (ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length);
}

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

@SuppressWarnings("unused") // Supposedly called by Caliper
public double timeFold(int reps)
{
 final ByteBuffer buf = allocateEmptyHLLBuffer(targetIsDirect, alignTarget, 0);
 for (int k = 0; k < reps; ++k) {
  for (int i = 0; i < count; ++i) {
   final int pos = positions[i];
   final int size = sizes[i];
   HyperLogLogCollector.makeCollector(
     (ByteBuffer) buf.duplicate().position(0).limit(
       HyperLogLogCollector.getLatestNumBytesForDenseStorage()
     )
   ).fold(
     HyperLogLogCollector.makeCollector(
       (ByteBuffer) chunk.duplicate().limit(pos + size).position(pos)
     )
   );
  }
 }
 return HyperLogLogCollector.makeCollector(buf.duplicate()).estimateCardinality();
}

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

private void getBytes(int index, ByteBuffer dst, boolean internal) {
  checkIndex(index, dst.remaining());
  ByteBuffer tmpBuf;
  if (internal) {
    tmpBuf = internalNioBuffer();
  } else {
    tmpBuf = buffer.duplicate();
  }
  tmpBuf.clear().position(index).limit(index + dst.remaining());
  dst.put(tmpBuf);
}

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

@Override
public ChannelBuffer copy(int index, int length) {
  ByteBuffer src;
  try {
    src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
  } catch (IllegalArgumentException e) {
    throw new IndexOutOfBoundsException();
  }
  ByteBuffer dst = buffer.isDirect()
      ? ByteBuffer.allocateDirect(length)
      : ByteBuffer.allocate(length);
  dst.put(src);
  dst.clear();
  return new ByteBufferBackedChannelBuffer(dst);
}

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

@Override
public ByteBuffer nioBuffer(int index, int length) {
  return (ByteBuffer) buffer.duplicate().position(index).limit(index + length);
}

代码示例来源:origin: square/okio

@Test public void ofByteBuffer() {
 byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8);
 ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
 byteBuffer.position(2).limit(11);
 ByteString byteString = ByteString.of(byteBuffer);
 // Verify that the bytes were copied out.
 byteBuffer.put(4, (byte) 'a');
 assertEquals("llo, Worl", byteString.utf8());
}

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

@Test(expected = BufferUnderflowException.class)
public void testOutOfBounds()
{
 ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'a', 'b', 'c', 'd'});
 bytes.position(1).limit(3);
 StringUtils.fromUtf8(bytes, 3);
}

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

@Override
public int setBytes(int index, FileChannel in, long position, int length) throws IOException {
  checkIndex(index, length);
  ByteBuffer tmpBuf = internalNioBuffer();
  index = idx(index);
  tmpBuf.clear().position(index).limit(index + length);
  try {
    return in.read(tmpBuf, position);
  } catch (ClosedChannelException ignored) {
    return -1;
  }
}

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

@Override
public DefaultDataBuffer read(byte[] destination, int offset, int length) {
  Assert.notNull(destination, "Byte array must not be null");
  assertIndex(this.readPosition <= this.writePosition - length,
      "readPosition %d and length %d should be smaller than writePosition %d",
      this.readPosition, length, this.writePosition);
  ByteBuffer tmp = this.byteBuffer.duplicate();
  int limit = this.readPosition + length;
  ((Buffer) tmp).clear().position(this.readPosition).limit(limit);
  tmp.get(destination, offset, length);
  this.readPosition += length;
  return this;
}

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

@Override
public ByteBuf setBytes(int index, ByteBuffer src) {
  ensureAccessible();
  ByteBuffer tmpBuf = internalNioBuffer();
  if (src == tmpBuf) {
    src = src.duplicate();
  }
  tmpBuf.clear().position(index).limit(index + src.remaining());
  tmpBuf.put(src);
  return this;
}

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

@Override
  protected void memoryCopy(ByteBuffer src, int srcOffset, ByteBuffer dst, int dstOffset, int length) {
    if (length == 0) {
      return;
    }
    if (HAS_UNSAFE) {
      PlatformDependent.copyMemory(
          PlatformDependent.directBufferAddress(src) + srcOffset,
          PlatformDependent.directBufferAddress(dst) + dstOffset, length);
    } else {
      // We must duplicate the NIO buffers because they may be accessed by other Netty buffers.
      src = src.duplicate();
      dst = dst.duplicate();
      src.position(srcOffset).limit(srcOffset + length);
      dst.position(dstOffset);
      dst.put(src);
    }
  }
}

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

@Test
public void testMiddleOfByteArrayConversion()
{
 ByteBuffer bytes = ByteBuffer.wrap(new byte[]{'a', 'b', 'c', 'd'});
 bytes.position(1).limit(3);
 Assert.assertEquals("bc", StringUtils.fromUtf8(bytes, 2));
 bytes.position(1);
 Assert.assertEquals("bc", StringUtils.fromUtf8(bytes));
}

相关文章