24h購物| | PChome| 登入
2008-12-22 14:31:57| 人氣588| 回應0 | 上一篇 | 下一篇

Byte Buffers的幾種作法 I

推薦 0 收藏 0 轉貼0 訂閱站台

1.如何產生一個ByteBuffer:ByteBuffer是固定長度的暫存byte values,以下範例示範如何產生一個ByteBuffer。

    //從byte array產生ByteBuffer
    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);
   
    // Create a non-direct ByteBuffer with a 10 byte capacity
    // The underlying storage is a byte array.
    buf = ByteBuffer.allocate(10);
   
    // Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity.
    buf = ByteBuffer.allocateDirect(10);
   
    // To create a ByteBuffer for a memory-mapped file,

2.從ByteBuffer取得Bytes
A ByteBuffer has a capacity that determines how many bytes it contains. This capacity can never change. Any byte in the buffer can be retrieved using the absolute version of get(), which takes an index in the range [0..capacity-1].

The bytes in a ByteBuffer can also be retrieved using the relative version of get(), which uses the position and limit properties of the buffer. In particular, this version of get() retrieves the byte at the position and advances the position by one. get() cannot retrieve bytes past the limit (even though the limit might be less than the capacity). The position is always <= limit and limit is always <= capacity.

 // 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
   
    // 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
   
    // Set the limit
    bbuf.limit(7); // remaining=1
   
    // This convenience method sets the position to 0
    bbuf.rewind(); // remaining=7

3.Putting Bytes into a ByteBuffer
A ByteBuffer has a capacity which determines how many bytes it contains. This capacity can never change. Any byte in the ByteBuffer can be modified using the absolute version of put(), which takes an index in the range [0..capacity-1].

The bytes in a ByteBuffer can also be set using the relative version of put(), which uses the position and limit properties of the buffer. In particular, this version of put() sets the byte at the position and advances the position by one. put() cannot set bytes past the limit (even though limit might be less than the capacity). The position is always <= limit and limit is always <= capacity.

 // 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);
   
    // Get the new position
    int pos = bbuf.position(); // 6
   
    // Get remaining byte count
    int rem = bbuf.remaining(); // 4
   
    // Set the limit
    bbuf.limit(7); // remaining=1
   
    // This convenience method sets the position to 0
    bbuf.rewind(); // remaining=7

4.Converting Between a ByteBuffer an a Byte Array

// Create a ByteBuffer from a byte array
    byte[] bytes = new byte[10];
    ByteBuffer buf = ByteBuffer.wrap(bytes);
   
    // Retrieve bytes between the position and limit   
    bytes = new byte[buf.remaining()];
    buf.get(bytes, 0, bytes.length);
   
    // Retrieve all bytes in the buffer
    buf.clear();
    bytes = new byte[buf.capacity()];
    buf.get(bytes, 0, bytes.length);

台長: softlive
人氣(588) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 工作甘苦(工作心得、創業、求職) | 個人分類: |
此分類下一篇:Byte Buffers的幾種作法 II
此分類上一篇:Apache 2.2 with subversion 1.4.6 in windows

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文