天天看點

ByteBuffer的allocate()方法詳解ByteBuffer的allocate()方法詳解

ByteBuffer的allocate()方法詳解

allocate()方法用于配置設定緩沖區。但是如果是聚集寫入,與分散讀取,就需要注意這個大小設定,

String property = System.getProperty("user.dir");
        FileOutputStream FileOutputStream = new FileOutputStream(property + "/lib/aab.text");
        FileChannel channel = FileOutputStream.getChannel();
        ByteBuffer allocate1 = ByteBuffer.allocate(90);
        ByteBuffer allocate2 = ByteBuffer.allocate(400);
        allocate1.asCharBuffer().put("400");
        allocate2.asCharBuffer().put("The world is so big, I want to see it.");
        channel.write(new ByteBuffer[]{allocate1,allocate2});
        channel.close();
           

這裡有兩個buffer,分别配置設定緩沖區90位元組,400位元組,下面讀取輸出:

String property = System.getProperty("user.dir");
        FileInputStream FileInputStream = new FileInputStream(property + "/lib/aab.text");
        FileChannel channel = FileInputStream.getChannel();
        ByteBuffer allocate1 = ByteBuffer.allocate(90);
        ByteBuffer allocate2 = ByteBuffer.allocate(400);
        channel.read(new ByteBuffer[]{allocate1,allocate2});
        allocate1.rewind();
        allocate2.rewind();
        String i = allocate1.asCharBuffer().toString();
        String string = allocate2.asCharBuffer().toString();
        System.out.println(1+" : "+i);
        System.out.println(2+" : "+string);

        channel.close();
           

結果

1 : 400                                          
2 : The world is so big, I want to see it.  
           

我們修改一下讀取代碼中緩沖區大小

String property = System.getProperty("user.dir");
        FileInputStream FileInputStream = new FileInputStream(property + "/lib/aab.text");
        FileChannel channel = FileInputStream.getChannel();
        ByteBuffer allocate1 = ByteBuffer.allocate(8);//改為8
        ByteBuffer allocate2 = ByteBuffer.allocate(80);//改為80
        channel.read(new ByteBuffer[]{allocate1,allocate2});
        allocate1.rewind();
        allocate2.rewind();
        String i = allocate1.asCharBuffer().toString();
        String string = allocate2.asCharBuffer().toString();
        System.out.println(1+" : "+i);
        System.out.println(2+" : "+string);

        channel.close();
           

結果

1 : 400 
2 :                                         
           

這裡就沒有讀取到,是以哪怕持久化到硬碟,兩個緩沖區任然是兩個緩沖區,第一個緩沖區的90位元組沒有讀取完,是不會去第二個緩沖區,

第一個緩沖區(90位元組) 第二個緩沖區(400位元組)
400 The world is so big, I want to see it.

再來修改一下代碼

String property = System.getProperty("user.dir");
        FileInputStream FileInputStream = new FileInputStream(property + "/lib/aab.text");
        FileChannel channel = FileInputStream.getChannel();
        ByteBuffer allocate1 = ByteBuffer.allocate(170);//改為170
        ByteBuffer allocate2 = ByteBuffer.allocate(400);
        channel.read(new ByteBuffer[]{allocate1,allocate2});
        allocate1.rewind();
        allocate2.rewind();
        String i = allocate1.asCharBuffer().toString();
        String string = allocate2.asCharBuffer().toString();
        System.out.println(1+" : "+i);
        System.out.println(2+" : "+string);

        channel.close();
           

結果

1 : 400The world is so big, I want to see it.  
2 :                                                                                                                                                                                                         
           

這裡說明每個緩沖區的内容在最開始位置,"400"隻占用了6個位元組,"The world is so big, I want to see it."占用了76個位元組.當第一緩沖區的90位元組讀取完,進入第二緩沖區,170-90=80>76,完全可以讀到第二緩沖區的文本内容