KB-20071120-032

From Triled Wiki

Jump to: navigation, search

Code and description are taken from original post.

If you allocate a direct IntBuffer, and then use the bulk put method, the position is not advanced. e.g.:

ByteBuffer bb = ByteBuffer.allocateDirect(intCount*4);
IntBuffer ib = bb.asIntBuffer();
ib.put(new int[] {1,2,3,4,5});

ib.position() should return 5, but it is still zero.

More detailed code:

package test.buffer;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class BufferTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        TextView tv = new TextView(this);
        this.setContentView(tv);

        int[] data = new int[5];

        ByteBuffer bb = ByteBuffer.allocateDirect(4*10);
        IntBuffer ib2 = bb.asIntBuffer();

        tv.append("Direct Buffer Before (should be 0): "+ib2.position() + "\n");
        ib2.put(data);
        tv.append("Direct Buffer After (should be 5): "+ib2.position() + "\n");
        
        // rest of code is skipped, see original post

    }
}

Result output:

Direct Buffer Before (should be 0): 0
Direct Buffer After (should be 5): 0
Personal tools