集册 Java实例教程 从ByteBuffer获取字节

从ByteBuffer获取字节

欢马劈雪     最近更新时间:2020-01-02 10:19:05

578
从ByteBuffer获取字节

import java.nio.ByteBuffer;

/** 
 来自 时 代 J a v a - nowjava.com**/

public class Main {

  public static void main(String[] argv) throws Exception {

    // Create an empty ByteBuffer with a 10 byte capacity

    ByteBuffer bbuf = ByteBuffer.allocate(10);


    // Get the ByteBuffer's capacity

    int capacity = bbuf.capacity(); // 10


    // Use the absolute get().

    // This method does not affect the position.

    byte b = bbuf.get(5); // position=0/*来 自 时代Java - N o w  J a v a . c o m*/


    // Set the position

    bbuf.position(5);


    // Use the relative get()

    b = bbuf.get();


    // Get the new position

    int pos = bbuf.position(); // 6


    // Get remaining byte count

    int rem = bbuf.remaining(); // 4


    
展开阅读全文