ByteBuff常用方法,使用

 

目錄

引入nettyjar包

ByteBuf API 的優點:

bytebuffer的四個重要屬性

Bytebuffer讀和寫的轉換

ByteBuffer相互轉換的Buffer

ByteBuffer常用初始方法(沒有構造)

HeapByteBuffer與DirectByteBuffer區別

public static ByteBuffer allocate(int capacity):初始化堆緩存

public static ByteBuffer allocateDirect(int capacity):直接內存

常用方法

ByteBuffer put(byte b);存放byte類型數據

ByteBuffer put(byte[] src):存放byte數組

ByteBuffer put(byte[] src, int offset, int length):從offset(包含)開始截取length長度的數組到ByteBuffer中

public ByteBuffer put(ByteBuffer src):添加一個只讀緩存的數據

ByteBuffer put(int index, byte b):將給定的字節寫入到這個緩衝區中

ByteBuffer putChar(char value):存放兩個字節的char

ByteBuffer putShort(short value);存放兩個字節的short

ByteBuffer putChar(int index, char value):在指定的位置插入char

ByteBuffer putShort(int index, short value);在指定位置存放兩個字節的short

ByteBuffer putDouble(double value):long和double都是8個字節,添加double和long

ByteBuffer putLong(long value):long和double都是8個字節,添加double和long

putDouble(int index, double value):指定位置添加8個字節的double

ByteBuffer putLong(int index, long value):指定位置添加8個字節的Long

ByteBuffer putFloat(float value);添加四個字節的float

ByteBuffer putInt(int value);添加四個字節的int

ByteBuffer putFloat(int index, float value);指定位置添加四個字節的float

ByteBuffer putInt(int index, int value);指定位置添加四個字節的int

ByteBuffer get(byte[] dst):讀取數據到byte[]數組中,並且pos會發生變化

ByteBuffer get(byte[] dst, int offset, int length):從數組offset下標開始(包含),讀取length長度緩存到數組demo

byte get(int index);獲取緩存位置的值,pos下標不會發生變化

char getChar();讀取兩個字節char

char getChar(int index);讀取指定位置兩個字節char,pos不會變化

double getDouble();讀取8個字節double

double getDouble(int index);讀取指定位置8個字節double,pos不會變化

float getFloat();讀取四個字節float

getFloat(int index);讀取指定位置四個字節float,pos不會變化

int getInt();讀取四個字節int

getInt(int index);讀取指定位置四個字節的int,pos不會變化

long getLong();讀取8個字節的long

long getLong(int index);讀取8個字節的long,pos不會變化

short getShort();讀取兩個字節的short

short getShort(int index);讀取指定位置兩個字節的short,pos不會變化

int arrayOffset():返回給定緩衝區的第一個元素的後備數組內的偏移量(試了幾次,返回的總是0)

DoubleBuffer asDoubleBuffer();創建此字節緩衝區作爲double緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/8

FloatBuffer asFloatBuffer();創建此字節緩衝區作爲float緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/4

IntBuffer asIntBuffer();創建此字節緩衝區作爲int緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/4

LongBuffer asLongBuffer();創建此字節緩衝區作爲long緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/8

ShortBuffer asShortBuffer();創建此字節緩衝區作爲short緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/2

ByteBuffer asReadOnlyBuffer();從原緩存當前位置開始,創建只讀緩存

ByteBuffer compact();壓縮此緩衝區就是把已經讀過的不要了,把剩下的還未讀的元素複製到緩存開始的地方

int compareTo(ByteBuffer that) :將此緩衝區剩餘的元素與另一個緩衝區剩餘元素進行比較。

ByteBuffer duplicate();創建一個共享該緩衝區內容的新字節緩衝區。操作的是同一個數組,所以一個buffer的改變,會影響另一個

boolean isDirect();告知此字節緩衝區是否爲直接緩衝區。

ByteOrder order() :檢索此緩衝區的字節順序。

ByteBuffer order(ByteOrder bo):修改此緩衝區的字節順序。(ByteOrder是枚舉:包含ByteOrderBIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;)

ByteBuffer slice();創建一個新的字節緩衝區,兩個緩存共同使用同一個數組和duplicate類似

ByteBuffer wrap(byte[] array):將字節數組包裝到緩衝區中。數組長度會作爲limit和cap,pos=0,和ByteBuffer原來的值沒有關係

ByteBuffer wrap(byte[] array,int offset, int length):將字節數組包裝到緩衝區中,只包裝從offset開始,length長度

Buffer的方法

int capacity():返回總容量

final Buffer clear():清空數據,position、limit、mark還原

Buffer flip():將limit放到position的位置,然後position歸0,相當於只讀取已經能讀取過的緩存

 boolean hasRemaining():是否還有可寫空間,position<>

int remaining():剩下的limit-position能寫的空間

boolean isReadOnly():設置爲只讀,此時添加數據不會生效

int limit():元素容量

Buffer mark():標記當前position的位置,通過reset能夠回到標記

Buffer reset():回到上一次mark標記的位置

int position():指針的位置

position(int newPosition):設置指針的位置


引入nettyjar包

<dependency>
     <groupId>io.netty</groupId>
     <artifactId>netty-all</artifactId>
     <version>4.1.28.Final</version>
</dependency>

ByteBuf API 的優點:

  1. 它可以被用戶自定義的緩衝區類型擴展;
  2.  通過內置的複合緩衝區類型實現了透明的零拷貝;可以作爲一個緩衝區,是因爲它是內存中的一段連續的空間
  3. 容量可以按需增長(類似於JDK 的StringBuilder);
  4. 在讀和寫這兩種模式之間切換不需要調用ByteBuffer 的flip()方法;
  5. 讀和寫使用了不同的索引;
  6. 支持方法的鏈式調用;
  7. 支持引用計數;
  8. 支持池化。

bytebuffer的四個重要屬性

capacity

當前bytebuffer的總容量,ByteBuffer.allocate(32)表示bytebuffer能存儲的字節是32

position 當前讀寫指針的位置
limit

position能夠讀到limit或者position能夠

寫到limit

mark 用於對position的標記


Bytebuffer讀和寫的轉換

 

ByteBuffer相互轉換的Buffer

ByteBuffer常用初始方法(沒有構造)

HeapByteBuffer與DirectByteBuffer區別

參考此博客:https://www.cnblogs.com/huilei/p/10149655.html總結

  HeapByteBuffer DirectByteBuffer
存儲地方和理由 在jvm堆上的緩存,底層是一個數組,使用類封裝了索引limit,position,capacity 數據存在操作系統的內存中,而不是jvm,DirectByteBuffer中有一個引用指向操作系統內存中的數據,通過這個引用操作數據
優缺點 因爲是存儲在jvm堆當中,所以寫速度快,容易回收 跟外設(IO)設備交互比較多,因爲外設讀取jvm堆中的數據時,不是直接讀取的,而是把jvm離的數據讀到一個操作系統內存塊中,再從這個內存塊中讀取數據。所以使用DirectByteBuffer可以省去這一步,實現傳說中的零拷貝

 題外:外設之所以要把jvm對裏的數據copy出來到系統內存裏面操作,不是因爲操作系統不能直接操作jvm內存,而是因爲jvm在進行gc會手的時候,會對數據進行移動,一旦出現這種情況,外設就會出現數據錯亂的情況。

public static ByteBuffer allocate(int capacity):初始化堆緩存

demo

public class Test {
    public static void main(String[] args) {
        ByteBuffer byteBuf = ByteBuffer.allocate(32);
        System.out.println(byteBuf);
    }
}

結果:

java.nio.HeapByteBuffer[pos=0 lim=32 cap=32]

public static ByteBuffer allocateDirect(int capacity):直接內存

demo

public class Test {
    public static void main(String[] args) {
        ByteBuffer byteBuf = ByteBuffer.allocateDirect(32);
        System.out.println(byteBuf);
    }
}

結果

java.nio.DirectByteBuffer[pos=0 lim=32 cap=32]

常用方法

ByteBuffer put(byte b);存放byte類型數據

demo

public class Test {
    public static void main(String[] args) {
        ByteBuffer byteBuf = ByteBuffer.allocate(32);
        byteBuf.put((byte) 1);
        System.out.println(byteBuf);
    }
}

ByteBuffer put(byte[] src):存放byte數組

ByteBuffer put(byte[] src, int offset, int length):從offset(包含)開始截取length長度的數組到ByteBuffer中

demo:讀取3、4、5到緩存

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[]{ 1,2,3,4,5,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.put(b,2,3);
        System.out.println(byteBuf.toString());
        //切換寫爲讀模式
        byteBuf.flip();
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
    }
}

結果:

java.nio.HeapByteBuffer[pos=3 lim=32 cap=32]
3
4
5

public ByteBuffer put(ByteBuffer src):添加一個只讀緩存的數據

demo:src一定要切換成只讀,否則無效(我被這個坑了一個小時,最後纔想起來看參數註釋)

public class Test {
    public static void main(String[] args) {
        //byte[] b = new byte[]{ 1,2,3,4,5,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(3);
        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);
        byteBuf.put((byte) 3);

        ByteBuffer byteBuf2 = ByteBuffer.allocate(32);

        //這裏一定要切換成寫模式
        byteBuf.flip();
        byteBuf2 = byteBuf2.put(byteBuf);
        byteBuf2.put((byte)4);
        byteBuf2.flip();

        System.out.println(byteBuf2.get());
        System.out.println(byteBuf2.get());
        System.out.println(byteBuf2.get());
        System.out.println(byteBuf2.get());
    }
}

結果:

1
2
3
4

ByteBuffer put(int index, byte b):將給定的字節寫入到這個緩衝區中

demo:已經存放了三個元素,下標是從0開始的,替換3,只能替換已經寫入的值,否則會把報錯

public class Test {
    public static void main(String[] args) {
        //byte[] b = new byte[]{ 1,2,3,4,5,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(3);
        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);
        byteBuf.put((byte) 3);
        System.out.println(byteBuf);
        //已經存放了三個元素,下標是從0開始的,替換3,只能替換已經寫入的值,否則會把報錯
        byteBuf.put(2,(byte)6);
        byteBuf.flip();
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());

    }
}

結果

java.nio.HeapByteBuffer[pos=3 lim=3 cap=3]
1
2
6

ByteBuffer putChar(char value):存放兩個字節的char

ByteBuffer putShort(short value);存放兩個字節的short

demo:char和byte區別可以參考:https://blog.csdn.net/m0_37846887/article/details/80774131

public class Test {
    public static void main(String[] args) {
        //byte[] b = new byte[]{ 1,2,3,4,5,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(32);
        //char佔兩個字節,第一個字節爲空,第二個字節爲ASCII嗎49
        byteBuf.putChar('1');
        byteBuf.putChar('2');
        byteBuf.putChar('3');

        System.out.println(byteBuf);

        byteBuf.flip();
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.getChar());
        System.out.println(byteBuf.getChar());

    }
}

結果:一個char是兩位字節,所以pos=6

java.nio.HeapByteBuffer[pos=6 lim=32 cap=32]
0
49
2
3

ByteBuffer putChar(int index, char value):在指定的位置插入char

ByteBuffer putShort(int index, short value);在指定位置存放兩個字節的short

public class Test {
    public static void main(String[] args) {
        //byte[] b = new byte[]{ 1,2,3,4,5,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(32);
        //char佔兩個字節,第一個字節爲空,第二個字節爲ASCII嗎49
        byteBuf.putChar('1');
        byteBuf.putChar('2');
        byteBuf.putChar('3');
        byteBuf.putChar(4,'6');
        System.out.println(byteBuf);

        byteBuf.flip();
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.getChar());
        System.out.println(byteBuf.getChar());

    }
}

結果:

java.nio.HeapByteBuffer[pos=6 lim=32 cap=32]
0
49
2
6

ByteBuffer putDouble(double value):long和double都是8個字節,添加double和long

ByteBuffer putLong(long value):long和double都是8個字節,添加double和long

putDouble(int index, double value):指定位置添加8個字節的double

ByteBuffer putLong(int index, long value):指定位置添加8個字節的Long

demo

public class Test {
    public static void main(String[] args) {
        //byte[] b = new byte[]{ 1,2,3,4,5,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(32);
        //char佔兩個字節,第一個字節爲空,第二個字節爲ASCII嗎49
        byteBuf.putLong(1L);
        byteBuf.putLong(2L);
        byteBuf.putDouble(1.02D);
        byteBuf.putDouble(2.02D);
        System.out.println(byteBuf);

        byteBuf.flip();
        System.out.println(byteBuf.getLong());
        System.out.println(byteBuf.getLong());
        System.out.println(byteBuf.getDouble());
        System.out.println(byteBuf.getDouble());
    }
}

結果:添加了四個8個字節,所以pos=32

java.nio.HeapByteBuffer[pos=32 lim=32 cap=32]
1
2
1.02
2.02

ByteBuffer putFloat(float value);添加四個字節的float

ByteBuffer putInt(int value);添加四個字節的int

 

ByteBuffer putFloat(int index, float value);指定位置添加四個字節的float

 

ByteBuffer putInt(int index, int value);指定位置添加四個字節的int

demo:同上putDouble()

get();讀取一個字節,pos+1

demo

public class Test {
    public static void main(String[] args) {
        //byte[] b = new byte[]{ 1,2,3,4,5,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(32);
        //char佔兩個字節,第一個字節爲空,第二個字節爲ASCII嗎49
        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);
        byteBuf.put((byte) 3);
        byteBuf.put((byte) 4);
        byteBuf.put((byte) 5);
        System.out.println(byteBuf);

        byteBuf.flip();
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf.get());
        System.out.println(byteBuf);
    }
}

結果

java.nio.HeapByteBuffer[pos=5 lim=32 cap=32]
flip之後java.nio.HeapByteBuffer[pos=0 lim=5 cap=32]
1
2
3
4
5
java.nio.HeapByteBuffer[pos=5 lim=5 cap=32]

 

ByteBuffer get(byte[] dst):讀取數據到byte[]數組中,並且pos會發生變化

demo

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[5];
        ByteBuffer byteBuf = ByteBuffer.allocate(32);
        
        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);
        byteBuf.put((byte) 3);
        byteBuf.put((byte) 4);
        byteBuf.put((byte) 5);
        System.out.println(byteBuf);

        byteBuf.flip();
        byteBuf.get(b);
        System.out.println("flip之後"+byteBuf);
        System.out.println(byteBuf);
        System.out.println(Arrays.toString(b));
    }
}
結果

java.nio.HeapByteBuffer[pos=5 lim=32 cap=32]
flip之後java.nio.HeapByteBuffer[pos=5 lim=5 cap=32]
java.nio.HeapByteBuffer[pos=5 lim=5 cap=32]
[1, 2, 3, 4, 5]


ByteBuffer get(byte[] dst, int offset, int length):從數組offset下標開始(包含),讀取length長度緩存到數組
demo

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[5];
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);
        byteBuf.put((byte) 3);
        byteBuf.put((byte) 4);
        byteBuf.put((byte) 5);
        System.out.println(byteBuf);

        byteBuf.flip();
        byteBuf.get(b,1,2);
        System.out.println("flip之後"+byteBuf);
        System.out.println(byteBuf);
        System.out.println(Arrays.toString(b));
    }
}
結果
java.nio.HeapByteBuffer[pos=5 lim=32 cap=32]
flip之後java.nio.HeapByteBuffer[pos=2 lim=5 cap=32]
java.nio.HeapByteBuffer[pos=2 lim=5 cap=32]
[0, 1, 2, 0, 0]


byte get(int index);獲取緩存位置的值,pos下標不會發生變化

demo

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[5];
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);
        byteBuf.put((byte) 3);
        byteBuf.put((byte) 4);
        byteBuf.put((byte) 5);
        System.out.println(byteBuf);

        byteBuf.flip();
        //byteBuf.get(b,1,2);
        System.out.println("flip之後"+byteBuf);

        System.out.println(byteBuf.get(2));
        System.out.println("讀取之後pos沒有發生變化"+byteBuf);
        System.out.println(Arrays.toString(b));
    }
}
結果
java.nio.HeapByteBuffer[pos=5 lim=32 cap=32]
flip之後java.nio.HeapByteBuffer[pos=0 lim=5 cap=32]
3
java.nio.HeapByteBuffer[pos=0 lim=5 cap=32]
[0, 0, 0, 0, 0]



char getChar();讀取兩個字節char


char getChar(int index);讀取指定位置兩個字節char,pos不會變化


double getDouble();讀取8個字節double


double getDouble(int index);讀取指定位置8個字節double,pos不會變化


float getFloat();讀取四個字節float


getFloat(int index);讀取指定位置四個字節float,pos不會變化


int getInt();讀取四個字節int


getInt(int index);讀取指定位置四個字節的int,pos不會變化


long getLong();讀取8個字節的long


long getLong(int index);讀取8個字節的long,pos不會變化


short getShort();讀取兩個字節的short


short getShort(int index);讀取指定位置兩個字節的short,pos不會變化

 

byte[] array():返回存儲數據的數組,bytebuf必須是寫模式

demo

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[5];
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);
        byteBuf.put((byte) 3);
        byteBuf.put((byte) 4);
        byteBuf.put((byte) 5);
        System.out.println(Arrays.toString(byteBuf.array()));
    }
}
結果
[1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

int arrayOffset():返回給定緩衝區的第一個元素的後備數組內的偏移量(試了幾次,返回的總是0)

CharBuffer asCharBuffer():創建此字節緩衝區作爲char緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/2

demo:

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[5];
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.putChar('1');
        byteBuf.putChar('2');
        CharBuffer charBuffer = byteBuf.asCharBuffer();
        charBuffer.put('3');
        System.out.println(byteBuf);
        System.out.println(charBuffer);

        byteBuf.flip();
        charBuffer.flip();
        System.out.println(Arrays.toString(byteBuf.array()));
        System.out.println(charBuffer.get());
    }
}

結果:新緩衝只有3,而舊的緩存有1,2,3

java.nio.HeapByteBuffer[pos=4 lim=32 cap=32]
             
[0, 49, 0, 50, 0, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
3

DoubleBuffer asDoubleBuffer();創建此字節緩衝區作爲double緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/8

FloatBuffer asFloatBuffer();創建此字節緩衝區作爲float緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/4

IntBuffer asIntBuffer();創建此字節緩衝區作爲int緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/4

LongBuffer asLongBuffer();創建此字節緩衝區作爲long緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/8

 

ShortBuffer asShortBuffer();創建此字節緩衝區作爲short緩衝區的視圖鏡像,新緩衝區的內容將從該緩衝區的當前位置開始,容量是就緩衝區剩餘容量/2

 

ByteBuffer asReadOnlyBuffer();從原緩存當前位置開始,創建只讀緩存

demo

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[5];
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.putChar('1');
        byteBuf.putChar('2');
        byteBuf.putChar('2');
        
        byteBuf.flip();
        ByteBuffer buffer = byteBuf.asReadOnlyBuffer();
        System.out.println(buffer);
        System.out.println(buffer.getChar());
    }
}

結果

java.nio.HeapByteBufferR[pos=0 lim=6 cap=32]
1

ByteBuffer compact();壓縮此緩衝區就是把已經讀過的不要了,把剩下的還未讀的元素複製到緩存開始的地方

demo查看此文章

int compareTo(ByteBuffer that) :將此緩衝區剩餘的元素與另一個緩衝區剩餘元素進行比較。

ByteBuffer duplicate();創建一個共享該緩衝區內容的新字節緩衝區。操作的是同一個數組,所以一個buffer的改變,會影響另一個

demo

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[5];
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.put((byte)1);
        byteBuf.put((byte)2);
        byteBuf.put((byte)3);
        byteBuf.put((byte)4);
        byteBuf.put((byte)5);
        byteBuf.put((byte)6);
        byteBuf.put((byte)7);
        byteBuf.put((byte)8);

        //操作的是同一個數組
        ByteBuffer byteBuf2 = byteBuf.duplicate();

        System.out.println(Arrays.toString(byteBuf.array()));
        System.out.println(Arrays.toString(byteBuf2.array()));
        byteBuf.put((byte)9);
        byteBuf2.put((byte)10);
        System.out.println(Arrays.toString(byteBuf.array()));
        System.out.println(Arrays.toString(byteBuf2.array()));
    }
}

結果:

[1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

 

boolean isDirect();告知此字節緩衝區是否爲直接緩衝區。

ByteOrder order() :檢索此緩衝區的字節順序。

查看ByteBuffer字節序



ByteBuffer order(ByteOrder bo):修改此緩衝區的字節順序。(ByteOrder是枚舉:包含ByteOrderBIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;)

ByteBuffer slice();創建一個新的字節緩衝區,兩個緩存共同使用同一個數組和duplicate類似

ByteBuffer wrap(byte[] array):將字節數組包裝到緩衝區中。數組長度會作爲limit和cap,pos=0,和ByteBuffer原來的值沒有關係

demo

public class Test {
    public static void main(String[] args) {
        byte[] b = new byte[]{3,4,6};
        ByteBuffer byteBuf = ByteBuffer.allocate(32);

        byteBuf.put((byte) 1);
        byteBuf.put((byte) 2);

        ByteBuffer wrap = byteBuf.wrap(b);
        System.out.println(Arrays.toString(byteBuf.array()));
        System.out.println(Arrays.toString(wrap.array()));
        System.out.println(wrap);
        System.out.println(Arrays.toString(b));
    }

}

結果:

[1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[3, 4, 6]
java.nio.HeapByteBuffer[pos=0 lim=3 cap=3]
[3, 4, 6]

 

ByteBuffer wrap(byte[] array,int offset, int length):將字節數組包裝到緩衝區中,只包裝從offset開始,length長度

 

Buffer的方法

int capacity():返回總容量

final Buffer clear():清空數據,position、limit、mark還原

Buffer flip():將limit放到position的位置,然後position歸0,相當於只讀取已經能讀取過的緩存

 boolean hasRemaining():是否還有可寫空間,position<limit

 

int remaining():剩下的limit-position能寫的空間

 

boolean isReadOnly():設置爲只讀,此時添加數據不會生效

 

int limit():元素容量

 

Buffer mark():標記當前position的位置,通過reset能夠回到標記

 

Buffer reset():回到上一次mark標記的位置

 

int position():指針的位置

 

position(int newPosition):設置指針的位置

 

Buffer rewind():position=0,並清除remark標記

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章