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

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

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

ByteBuffer.remaining介绍

暂无

代码示例

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

@Override
protected void processRemaining(ByteBuffer buffer) {
 b += buffer.remaining();
 for (int i = 0; buffer.hasRemaining(); i += 8) {
  finalM ^= (buffer.get() & 0xFFL) << i;
 }
}

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

@Override
public void setBytes(int index, ByteBuffer src) {
  src.get(array, index, src.remaining());
}

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

/**
 * Initialize an instance based upon the underlying storage from {@code value}.
 * There is a potential to share the underlying array storage if {@link ByteBuffer#hasArray()} is {@code true}.
 * if {@code copy} is {@code true} a copy will be made of the memory.
 * if {@code copy} is {@code false} the underlying storage will be shared, if possible.
 */
public AsciiString(ByteBuffer value, boolean copy) {
  this(value, value.position(), value.remaining(), copy);
}

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

@Override
public Hasher putBytes(ByteBuffer b) {
 if (b.hasArray()) {
  putBytes(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   putByte(b.get());
  }
 }
 return this;
}

代码示例来源:origin: apache/kafka

/**
 * transfers appReadBuffer contents (decrypted data) into dst bytebuffer
 * @param dst ByteBuffer
 */
private int readFromAppBuffer(ByteBuffer dst) {
  appReadBuffer.flip();
  int remaining = Math.min(appReadBuffer.remaining(), dst.remaining());
  if (remaining > 0) {
    int limit = appReadBuffer.limit();
    appReadBuffer.limit(appReadBuffer.position() + remaining);
    dst.put(appReadBuffer);
    appReadBuffer.limit(limit);
  }
  appReadBuffer.compact();
  return remaining;
}

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

static ByteBuffer encodeChunk(ByteBuffer data) {
  if (data.remaining() == 0) {
    return ByteBuffer.wrap(LAST_CHUNK);
  }
  byte[] startBytes = getChunkHeaderBytes(data.remaining());
  ByteBuffer chunkBuffer = ByteBuffer.allocate(startBytes.length + data.remaining() + 2);
  chunkBuffer.put(startBytes);
  chunkBuffer.put(data);
  chunkBuffer.put(LINE_SEPARATOR_BYTES);
  chunkBuffer.flip();
  return chunkBuffer;
}

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

@Override
public ByteBuf getBytes(int index, ByteBuffer dst) {
  checkIndex(index, dst.remaining());
  dst.put(array, index, dst.remaining());
  return this;
}

代码示例来源:origin: Tencent/tinker

/**
 * Returns the checksum of all but the first 12 bytes of {@code dex}.
 */
public int computeChecksum() throws IOException {
  Adler32 adler32 = new Adler32();
  byte[] buffer = new byte[8192];
  ByteBuffer data = this.data.duplicate(); // positioned ByteBuffers aren't thread safe
  data.limit(data.capacity());
  data.position(CHECKSUM_OFFSET + SizeOf.CHECKSUM);
  while (data.hasRemaining()) {
    int count = Math.min(buffer.length, data.remaining());
    data.get(buffer, 0, count);
    adler32.update(buffer, 0, count);
  }
  return (int) adler32.getValue();
}

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

private void convertToMutableByteBuffer()
{
 ByteBuffer tmpBuffer = ByteBuffer.allocate(storageBuffer.remaining());
 tmpBuffer.put(storageBuffer.asReadOnlyBuffer());
 tmpBuffer.position(0);
 storageBuffer = tmpBuffer;
 initPosition = 0;
}

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

private Hasher putBytesInternal(ByteBuffer readBuffer) {
 // If we have room for all of it, this is easy
 if (readBuffer.remaining() <= buffer.remaining()) {
  buffer.put(readBuffer);
  munchIfFull();
  return this;
 }
 // First add just enough to fill buffer size, and munch that
 int bytesToCopy = bufferSize - buffer.position();
 for (int i = 0; i < bytesToCopy; i++) {
  buffer.put(readBuffer.get());
 }
 munch(); // buffer becomes empty here, since chunkSize divides bufferSize
 // Now process directly from the rest of the input buffer
 while (readBuffer.remaining() >= chunkSize) {
  process(readBuffer);
 }
 // Finally stick the remainder back in our usual buffer
 buffer.put(readBuffer);
 return this;
}

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

@Override
public Hasher putBytes(ByteBuffer buffer) {
 ByteOrder bo = buffer.order();
 buffer.order(ByteOrder.LITTLE_ENDIAN);
 while (buffer.remaining() >= 4) {
  putInt(buffer.getInt());
 }
 while (buffer.hasRemaining()) {
  putByte(buffer.get());
 }
 buffer.order(bo);
 return this;
}

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

@Override
public void setBytes(int index, ByteBuffer src) {
  ByteBuffer data = buffer.duplicate();
  data.limit(index + src.remaining()).position(index);
  data.put(src);
}

代码示例来源:origin: apache/kafka

@Override
public void write(ByteBuffer buffer, Object o) {
  ByteBuffer arg = (ByteBuffer) o;
  int pos = arg.position();
  buffer.putInt(arg.remaining());
  buffer.put(arg);
  arg.position(pos);
}

代码示例来源:origin: apache/kafka

@Override
public Object read(ByteBuffer buffer) {
  int size = buffer.getInt();
  if (size < 0)
    return null;
  if (size > buffer.remaining())
    throw new SchemaException("Error reading bytes of size " + size + ", only " + buffer.remaining() + " bytes available");
  ByteBuffer val = buffer.slice();
  val.limit(size);
  buffer.position(buffer.position() + size);
  return val;
}

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

/**
 * Try to read an EOL incrementing the buffer position if successful.
 * @return whether an EOL was consumed
 */
private boolean tryConsumeEndOfLine(ByteBuffer byteBuffer) {
  if (byteBuffer.remaining() > 0) {
    byte b = byteBuffer.get();
    if (b == '\n') {
      return true;
    }
    else if (b == '\r') {
      if (byteBuffer.remaining() > 0 && byteBuffer.get() == '\n') {
        return true;
      }
      else {
        throw new StompConversionException("'\\r' must be followed by '\\n'");
      }
    }
    // Explicit cast for compatibility with covariant return type on JDK 9's ByteBuffer
    ((Buffer) byteBuffer).position(byteBuffer.position() - 1);
  }
  return false;
}

代码示例来源:origin: apache/flink

private static BufferOrEvent generateAndWriteEvent(FileChannel fileChannel, Random rnd, int numberOfChannels) throws IOException {
  long magicNumber = rnd.nextLong();
  byte[] data = new byte[rnd.nextInt(1000)];
  rnd.nextBytes(data);
  TestEvent evt = new TestEvent(magicNumber, data);
  int channelIndex = rnd.nextInt(numberOfChannels);
  ByteBuffer serializedEvent = EventSerializer.toSerializedEvent(evt);
  ByteBuffer header = ByteBuffer.allocate(9);
  header.order(ByteOrder.LITTLE_ENDIAN);
  header.putInt(channelIndex);
  header.putInt(serializedEvent.remaining());
  header.put((byte) 1);
  header.flip();
  FileUtils.writeCompletely(fileChannel, header);
  FileUtils.writeCompletely(fileChannel, serializedEvent);
  return new BufferOrEvent(evt, channelIndex);
}

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

@BeforeClass
public static void setUp()
{
 final ByteBuffer buffer = ByteBuffer.wrap(allBytes);
 while (buffer.hasRemaining()) {
  final byte[] bytes = new byte[Math.min(RANDOM.nextInt(128), buffer.remaining())];
  RANDOM.nextBytes(bytes);
  buffer.put(bytes);
  BYTE_LIST.add(bytes);
 }
}

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

@Test
public void writeWritableByteChannelErrorInWrite() throws Exception {
  DataBuffer foo = stringBuffer("foo");
  DataBuffer bar = stringBuffer("bar");
  Flux<DataBuffer> flux = Flux.just(foo, bar);
  WritableByteChannel channel = mock(WritableByteChannel.class);
  when(channel.write(any()))
      .thenAnswer(invocation -> {
        ByteBuffer buffer = invocation.getArgument(0);
        int written = buffer.remaining();
        buffer.position(buffer.limit());
        return written;
      })
      .thenThrow(new IOException());
  Flux<DataBuffer> writeResult = DataBufferUtils.write(flux, channel);
  StepVerifier.create(writeResult)
      .consumeNextWith(stringConsumer("foo"))
      .consumeNextWith(stringConsumer("bar"))
      .expectError(IOException.class)
      .verify(Duration.ofSeconds(3));
  channel.close();
}

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

/** Updates this hasher with bytes from the given buffer. */
protected void update(ByteBuffer b) {
 if (b.hasArray()) {
  update(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   update(b.get());
  }
 }
}

相关文章