Java Buffer.hasRemaining() 方法及代码示例
表明当前缓冲区的位置和限制之间是否尚有元素.
定义
public final boolean hasRemaining()
返回值
当且仅当此缓冲区中至少有一个元素剩余时才返回 true
.
示例
使用 hasRemaining() 方法得知缓冲区中是否还有剩余元素的示例
package com.yi21.buffer; import java.nio.ByteBuffer; public class Yi21BufferHasRemaining { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.wrap("Hello 21yi!".getBytes()); while(buffer.hasRemaining()) { System.out.print((char) buffer.get()); } } }
执行结果为 :
Hello 21yi!