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

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

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

ByteBuffer.hasRemaining介绍

暂无

代码示例

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

@Override
public int read() {
 if (!byteBuffer.hasRemaining()) {
  return -1;
 }
 return byteBuffer.get();
}

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

@Override
public int read(ByteBuffer dst) throws IOException {
  int read=0;
  while(dst.hasRemaining() && bytes_to_read.hasRemaining()) {
    dst.put(bytes_to_read.get());
    read++;
  }
  return read > 0? read : closed? -1 : read;
}

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

public static boolean readAndFlip( ReadableByteChannel channel, ByteBuffer buffer, int bytes )
    throws IOException
{
  buffer.clear();
  buffer.limit( bytes );
  while ( buffer.hasRemaining() )
  {
    int read = channel.read( buffer );
    if ( read == -1 )
    {
      return false;
    }
  }
  buffer.flip();
  return true;
}

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

private int copy(final ByteBuffer src, final ByteBuffer dst) {
  if (src != null && src.hasRemaining()) {
    final int bytesToCopy = Math.min(dst.remaining(), src.remaining());
    if (bytesToCopy < 1) {
      return bytesToCopy;
    }
    final byte[] buff = new byte[bytesToCopy];
    src.get(buff);
    dst.put(buff);
    return bytesToCopy;
  }
  return 0;
}

代码示例来源:origin: igniterealtime/Openfire

private void tlsWrite(ByteBuffer buf) throws IOException {
  ByteBuffer tlsBuffer;
  ByteBuffer tlsOutput;
  do {
    // TODO Consider optimizing by not creating new instances each time
    tlsBuffer = ByteBuffer.allocate(Math.min(buf.remaining(), wrapper.getAppBuffSize()));
    tlsOutput = ByteBuffer.allocate(wrapper.getNetBuffSize());
    while (tlsBuffer.hasRemaining() && buf.hasRemaining()) {
      tlsBuffer.put(buf.get());
    }
    tlsBuffer.flip();
    wrapper.wrap(tlsBuffer, tlsOutput);
    tlsOutput.flip();
    writeToSocket(tlsOutput);
    tlsOutput.clear();
  } while (buf.hasRemaining());
}

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

/**
 * Calculates hash code for the given byte buffers contents. Compatible with {@link Arrays#hashCode(byte[])}
 * with the same content. Does not change buffers positions.
 *
 * @param bufs Byte buffers.
 * @return Hash code.
 */
public static int hashCode(ByteBuffer... bufs) {
  int res = 1;
  for (ByteBuffer buf : bufs) {
    int pos = buf.position();
    while (buf.hasRemaining())
      res = 31 * res + buf.get();
    buf.position(pos);
  }
  return res;
}

代码示例来源:origin: TooTallNate/Java-WebSocket

boolean mask = role == Role.CLIENT;
int sizebytes = getSizeBytes(mes);
ByteBuffer buf = ByteBuffer.allocate( 1 + ( sizebytes > 1 ? sizebytes + 1 : sizebytes ) + ( mask ? 4 : 0 ) + mes.remaining() );
byte optcode = fromOpcode( framedata.getOpcode() );
byte one = ( byte ) ( framedata.isFin() ? -128 : 0 );
one |= optcode;
buf.put( one );
byte[] payloadlengthbytes = toByteArray( mes.remaining(), sizebytes );
assert ( payloadlengthbytes.length == sizebytes );
  buf.put( ( byte ) ( payloadlengthbytes[0] | getMaskByte(mask) ) );
} else if( sizebytes == 2 ) {
  buf.put( ( byte ) ( ( byte ) 126 | getMaskByte(mask)));
  buf.put( payloadlengthbytes );
} else if( sizebytes == 8 ) {
  maskkey.putInt( reuseableRandom.nextInt() );
  buf.put( maskkey.array() );
  for( int i = 0; mes.hasRemaining(); i++ ) {
    buf.put( ( byte ) ( mes.get() ^ maskkey.get( i % 4 ) ) );
  buf.put( mes );
  mes.flip();
assert ( buf.remaining() == 0 ) : buf.remaining();
buf.flip();
return buf;

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

private void read(ByteBuffer buffer) throws IOException {
 buffer.clear();
 while (buffer.hasRemaining())
  if (channel.read(buffer) == -1)
   throw new EOFException();
 buffer.flip();
}

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

public static ChannelBuffer wrappedBuffer(ByteBuffer buffer) {
  if (!buffer.hasRemaining()) {
    return EMPTY_BUFFER;
  }
  if (buffer.hasArray()) {
    return wrappedBuffer(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
  } else {
    return new ByteBufferBackedChannelBuffer(buffer);
  }
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

public void put(byte data) {
  if (lineByteBuffer.hasRemaining()) {
    lineByteBuffer.put(data);
  } else {
    final ByteBuffer newLineByteBuffer = ByteBuffer.allocate(lineByteBuffer.capacity() + bufferSize);
    lineByteBuffer.flip();
    newLineByteBuffer.put(lineByteBuffer);
    newLineByteBuffer.put(data);
    this.lineByteBuffer = newLineByteBuffer;
  }
}

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

private void convertToDenseStorage()
{
 ByteBuffer tmpBuffer = ByteBuffer.allocate(getNumBytesForDenseStorage());
 // put header
 setVersion(tmpBuffer);
 setRegisterOffset(tmpBuffer, getRegisterOffset());
 setNumNonZeroRegisters(tmpBuffer, getNumNonZeroRegisters());
 setMaxOverflowValue(tmpBuffer, getMaxOverflowValue());
 setMaxOverflowRegister(tmpBuffer, getMaxOverflowRegister());
 storageBuffer.position(getPayloadBytePosition());
 tmpBuffer.position(getPayloadBytePosition(tmpBuffer));
 // put payload
 while (storageBuffer.hasRemaining()) {
  tmpBuffer.put(storageBuffer.getShort(), storageBuffer.get());
 }
 tmpBuffer.rewind();
 storageBuffer = tmpBuffer;
 initPosition = 0;
}

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

/**
 * Zero a buffer.  Ensures that any potentially sensitive information in the buffer is
 * overwritten.
 *
 * @param buffer the buffer
 */
public static void zero(ByteBuffer buffer) {
  buffer.clear();
  while (buffer.remaining() >= 8) {
    buffer.putLong(0L);
  }
  while (buffer.hasRemaining()) {
    buffer.put((byte) 0);
  }
  buffer.clear();
}

代码示例来源:origin: EsotericSoftware/kryonet

private boolean writeToSocket () throws IOException {
  SocketChannel socketChannel = this.socketChannel;
  if (socketChannel == null) throw new SocketException("Connection is closed.");
  ByteBuffer buffer = writeBuffer;
  buffer.flip();
  while (buffer.hasRemaining()) {
    if (bufferPositionFix) {
      buffer.compact();
      buffer.flip();
    }
    if (socketChannel.write(buffer) == 0) break;
  }
  buffer.compact();
  return buffer.position() == 0;
}

代码示例来源:origin: TeamNewPipe/NewPipe

private int parse_mdia(ByteBuffer data) {
  while (data.hasRemaining()) {
    int end = data.position() + data.getInt();
    if (data.getInt() == ATOM_MDHD) {
      byte version = data.get();
      data.position(data.position() + 3 + ((version == 0 ? 4 : 8) * 2));
      return data.getInt();
    }
    data.position(end);
  }
  return 0;// this NEVER should happen
}

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

@Override
public VSizeColumnarInts get(int index)
{
 if (index >= size) {
  throw new IAE("Index[%d] >= size[%d]", index, size);
 }
 ByteBuffer myBuffer = theBuffer.asReadOnlyBuffer();
 int startOffset = 0;
 int endOffset;
 if (index == 0) {
  endOffset = myBuffer.getInt();
 } else {
  myBuffer.position(myBuffer.position() + ((index - 1) * Integer.BYTES));
  startOffset = myBuffer.getInt();
  endOffset = myBuffer.getInt();
 }
 myBuffer.position(valuesOffset + startOffset);
 myBuffer.limit(myBuffer.position() + (endOffset - startOffset) + bufferBytes);
 return myBuffer.hasRemaining() ? new VSizeColumnarInts(myBuffer, numBytes) : null;
}

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

private boolean reportSlaveMaxOffset(final long maxOffset) {
  this.reportOffset.position(0);
  this.reportOffset.limit(8);
  this.reportOffset.putLong(maxOffset);
  this.reportOffset.position(0);
  this.reportOffset.limit(8);
  for (int i = 0; i < 3 && this.reportOffset.hasRemaining(); i++) {
    try {
      this.socketChannel.write(this.reportOffset);
    } catch (IOException e) {
      log.error(this.getServiceName()
        + "reportSlaveMaxOffset this.socketChannel.write exception", e);
      return false;
    }
  }
  return !this.reportOffset.hasRemaining();
}

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

@Override
public void addValue(int val) throws IOException
{
 if (endBuffer == null) {
  throw new IllegalStateException("written out already");
 }
 if (!endBuffer.hasRemaining()) {
  endBuffer.rewind();
  flattener.write(endBuffer);
  endBuffer.clear();
 }
 intBuffer.putInt(0, val);
 if (isBigEndian) {
  endBuffer.put(intBuffer.array(), Integer.BYTES - numBytes, numBytes);
 } else {
  endBuffer.put(intBuffer.array(), 0, numBytes);
 }
 numInserted++;
}

相关文章