Code example for ByteBuffer

Methods: capacity, limit, position

0
package com.koushikdutta.async; 
 
import java.nio.ByteBuffer; 
 
public class BinarySink extends BufferedDataSink { 
    public BinarySink(DataSink datasink) {
        super(datasink);
    } 
     
    private void writeFull(ByteBuffer b) {
        b.position(0);
        b.limit(b.capacity());
        write(b);
    } 
 
    public void writeInt(int i) {
        ByteBuffer bb = ByteBuffer.allocate(4);
        bb.putInt(i);
        writeFull(bb);
    } 
     
    public void writeByte(byte b) {
        ByteBuffer bb = ByteBuffer.allocate(1);
        bb.put(b);
        writeFull(bb);
    }