集册 Java实例教程 将字节放入ByteBuffer

将字节放入ByteBuffer

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

529
将字节放入ByteBuffer
/** 来 自 时代Java - N o w  J a v a . c o m**/

import java.nio.ByteBuffer;


public class Main {

  void m() {

    // Create an empty ByteBuffer with a 10 byte capacity

    ByteBuffer bbuf = ByteBuffer.allocate(10);


    // Get the buffer's capacity

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


    // Use the absolute put().

    // This method does not affect the position.

    bbuf.put((byte) 0xFF); // position=0


    // Set the position

    bbuf.position(5);


    // Use the relative put()

    bbuf.put((byte) 0xFF);

    /**来自 
     N o w J a v a . c o m - 时代Java**/

    // Get the new position

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


    // Get remaining byte count

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


    
展开阅读全文