Android

【NIO】ByteBuffer

2016-03-08  本文已影响2236人  jackLee

1.学习ByteBuffer类首先得学习掌握Buffer的类。
Buffer是一个抽象的基类
派生类:ByteBuffer, CharBuffer, DoubleBuffer, FloatBuffer, IntBuffer, LongBuffer, ShortBuffer

关于Buffer的若干点:

ByteBuffer:

-  看一个完整的ByteBuffer的读写操作:
    -  写操作:
public byte[] sendDefLight_CCT(String addr,int temper,int time){
    ByteBuffer buffer=ByteBuffer.allocate(16);//分配内存大小
    buffer.putShort((short) 18);//存入Short类型,一个Short两个字节
    buffer.put(com.sansi.stellar.local.protocol.common.stringToByte(addr));
    buffer.put((byte)0x00);//put一个byte
    buffer.put((byte)0x04);
    buffer.putShort((short)temper);
    buffer.putShort((short)time);
    byte[] re =(byte[])buffer.flip().array();//将所有的byte返回,链式调用。
    re=packageData(re);//对数据进行crc校验封装
    return re;
}  
    -  读操作:
        public LightStatus(byte[] content) {
    if (content.length >= 11) {
        ByteBuffer buf = ByteBuffer.wrap(content);//Wraps a byte array into a buffer
        failure = buf.get();//Returns the byte at the current position and increases the position by 1
        rgbw = buf.getInt();//Returns the int at the current position and increases the position by 4. 
        cct = (int)(buf.getShort() & 0xFFFF);
        brightness = (int)(buf.get() & 0xFF);
        scene = (int)(buf.get() & 0xFF);
        rate = (int)(buf.getShort() & 0xFFFF);
    }
}
  1. ByteBuffer buffer :
    --buffer = Buffer.locate(20);//分配20bytes大小的内存
    --buffer.put()/1 byte
    --buffer.get()
    --buffer.putChar()//2 bytes
    --buffer.getChar()
    --buffer.putShort()//2bytes
    --buffer.getShort()
    --buffer.putInt()//4bytes
    --buffer.getInt()
    --buffer.limit()://分为读写两种模式:当为写的模式时:返回值为缓存区的大小==buffer.capacity();
    当为读的模式的时候,返回值为当前位置大小==buffer.position();以一个字节为计算单位。
    --buffer.limit(0);//position=limit=0,写模式下重头覆盖缓冲区,与buffer.clear()效果相同。
    --buffer.hasRemaining()://内存空间是否有剩余
    --buffer.clear():清除缓冲区
    --buffer.flip();//进入读模式
    --buffer.compact();//进入写模式
    --buffer.flip().array();//将buffer中的内容以字节形式返回

总结:

        ByteBuffer对应的有读和写的操作,没进行一次操作,底层会自动为我们移动position,
        可以很方便的进行数据协议的操作。
        经过试验发现:比如我buffer缓冲区内存入了一些数据,然后打印出当前的信息:
        发现buffer的byte信息并没有清0,而是后来数据覆盖前者的数据。

参考文档:

上一篇 下一篇

猜你喜欢

热点阅读