ByteBuffer

208 阅读2分钟

先粘点代码,有时间再来完善:

ByteBuffer buf = ByteBuffer.allocate(8);
//        ByteBuffer buffer2 = ByteBuffer.allocateDirect(8);
        System.out.println(buf);

        // mark:标记,用于保存当前position的位置。之后position位置发生了变化,然后调用reset()可以将position恢复到mark的位置。
        // capacity:容量
        // limit:界限表示缓冲区中可操作的数据的大小。(limit后面的数据不能进行读写)
        // position:表示缓冲区中正在操作数据的位置。
        // flip(),读数据模式,即position变成0,把limit变成数据的结尾。实现就是把limit设置为position,再把position设置为0
        // rewind(),重复读数据,即把数据恢复到flip()的状态。实现就是把position设置为0
        // clear(),清空缓冲区,其实数据还在,只是把capacity、mark、limit、position恢复为初始化状态。
        // hasRemaining(),判断缓冲区是否有可用空间
        // remaining(),判断缓冲区的剩余可用大小

        // allocate()分配的是非直接缓冲区,将缓冲区建立在JVM的内存中。只时就是创建了一个数组。
        // allocateDirect()分配的是直接缓冲区,将缓冲区建立在物理内存中。
        // isDirect()判断是否是直接缓冲区
        buf.put((byte) 1);
        buf.put((byte) 1);
        buf.put((byte) 1);
        System.out.println(buf);
        System.out.println("是否有可用空间:" + buf.hasRemaining());
        System.out.println("剩余可用大小:" + buf.remaining());

        buf.flip();
        System.out.println("装有的数据量:" + (buf.limit() - buf.position()));
        System.out.println(buf);

        buf.rewind();
        System.out.println(buf);
private static void copy1() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("C:\\Users\\EvenDai\\Desktop\\动画.mp4");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\EvenDai\\Desktop\\动画2.mp4");
        byte[] buffer = new byte[1024 * 8];
        int len;
        while ((len = fis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
        }
        fis.close();
        fos.close();
        System.out.println("复制文件时间:" + (System.currentTimeMillis() - start));
    }

    private static void copy2() throws IOException {
        long start = System.currentTimeMillis();
        FileInputStream fis = new FileInputStream("C:\\Users\\EvenDai\\Desktop\\动画.mp4");
        FileOutputStream fos = new FileOutputStream("C:\\Users\\EvenDai\\Desktop\\动画2.mp4");
        FileChannel inChannel = fis.getChannel();
        FileChannel outChannel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocateDirect(1024 * 8);
        while (inChannel.read(buffer) > 0) {
            buffer.flip();
            outChannel.write(buffer);
            buffer.clear();
        }
        inChannel.close();
        outChannel.close();
        fis.close();
        fos.close();
        System.out.println("复制文件时间:" + (System.currentTimeMillis() - start));
    }

    private static void copy3() throws IOException {
        long start = System.currentTimeMillis();
        String filePath = "C:\\Users\\EvenDai\\Desktop\\动画.mp4";
        String filePath2 = "C:\\Users\\EvenDai\\Desktop\\动画2.mp4";
        FileChannel inChannel = FileChannel.open(Paths.get(filePath), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get(filePath2), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        MappedByteBuffer inBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        MappedByteBuffer outBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());
        outBuffer.put(inBuffer);
        inChannel.close();
        outChannel.close();
        System.out.println("复制文件时间:" + (System.currentTimeMillis() - start));
    }

    private static void copy4() throws IOException {
        long start = System.currentTimeMillis();
        String filePath = "C:\\Users\\EvenDai\\Desktop\\动画.mp4";
        String filePath2 = "C:\\Users\\EvenDai\\Desktop\\动画2.mp4";
        FileChannel inChannel = FileChannel.open(Paths.get(filePath), StandardOpenOption.READ);
        FileChannel outChannel = FileChannel.open(Paths.get(filePath2), StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
        inChannel.transferTo(0, inChannel.size(), outChannel);
        inChannel.close();
        outChannel.close();
        System.out.println("复制文件时间:" + (System.currentTimeMillis() - start));
    }
public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\even888\\Desktop\\test.txt");
        FileOutputStream fos = new FileOutputStream(file);
        FileChannel channel = fos.getChannel();
        ByteBuffer buffer = ByteBuffer.allocateDirect(4);
        buffer.put((byte) 0);
        buffer.put((byte) 0);
        buffer.put((byte) 0);
        buffer.put((byte) 0);
        buffer.flip(); // 如果不调用这个函数,写出去的内容将为0字节
        channel.write(buffer);
        channel.close();
        fos.close();
    }

这说明,一般在通过一个api获得一个ByteBuffer对象时,别人在给你这个ByteBuffer的时候就已经调用过buffer.flip()方法了。