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,完全可以读到第二缓冲区的文本内容

你可能感兴趣的:(NIO)