ByteBuffer源码分析

引言

在nio中,流的读取和写入都是依赖buffer的。jdk在nio包中提供了ByteBuffer、CharBuffer、ShortBuffer、LongBuffer、DoubleBuffer、FloatBuffer等。 6中类型的buffer还分为两种实现,缓存在jvm堆中和缓存在直接内存中。

Buffer

主要属性

// Invariants: mark <= position <= limit <= capacity
    private int mark = -1;
    private int position = 0;
    private int limit;
    private int capacity;

    // Used only by direct buffers
    // NOTE: hoisted here for speed in JNI GetDirectBufferAddress
    long address;

主要方法

这些方法是用来控制buffer的读写。
jdk提供的buffer只有一个position指针,读和写都是从position的位置开始操作。
代码的流程常常是这样的:
buffer.put(1);
buffer.flip();
buffer.get();
用于多次读取操作

public final Buffer mark() {
        mark = position;
        return this;
    }

public final Buffer reset() {
        int m = mark;
        if (m < 0)
            throw new InvalidMarkException();
        position = m;
        return this;
    }

假装清空

public final Buffer clear() {
        position = 0;
        limit = capacity;
        mark = -1;
        return this;
    }

翻转指针

public final Buffer flip() {
        limit = position;
        position = 0;
        mark = -1;
        return this;
    }

剩余容量

public final int remaining() {
        return limit - position;
    }

获取读写指针的方法

final int nextGetIndex() {                          // package-private
        if (position >= limit)
            throw new BufferUnderflowException();
        return position++;
    }

final int nextPutIndex() {                          // package-private
        if (position >= limit)
            throw new BufferOverflowException();
        return position++;
    }

ByteBuffer

主要属性

// byte数组
final byte[] hb;                  // Non-null only for heap buffers
// offset 在派生的时候有用
    final int offset;

实例化方法

创建指定容量的heapBuffer和directBuffer

public static ByteBuffer allocate(int capacity) {
        if (capacity < 0)
            throw new IllegalArgumentException();
        return new HeapByteBuffer(capacity, capacity);
    }

public static ByteBuffer allocateDirect(int capacity) {
        return new DirectByteBuffer(capacity);
    }

通过数组创建ByteBuffer

public static ByteBuffer wrap(byte[] array,
                                    int offset, int length)
    {
        try {
            return new HeapByteBuffer(array, offset, length);
        } catch (IllegalArgumentException x) {
            throw new IndexOutOfBoundsException();
        }
    }

put和get方法

public ByteBuffer put(byte x) {
		
		// 获取put的指针 
        hb[ix(nextPutIndex())] = x;
        return this;
    }

public byte get() {
		// 获取get的指针
        return hb[ix(nextGetIndex())];
    }

protected int ix(int i) {
        return i + offset;
    }

派生ByteBuffer
slice创建的Buffer,读写都是在数组的子序列上进行。依赖于Buffer的当前索引

// 共享数组,position=0 mark=-1 limit=cap,
public ByteBuffer slice() {
        return new HeapByteBuffer(hb,
                                        -1,
                                        0,
                                        this.remaining(),
                                        this.remaining(),
                                        this.position() + offset);
    }

复制一个对象,共享数组,拥有相同数值的mark、position、limit、capacity和offset

public ByteBuffer duplicate() {
        return new HeapByteBuffer(hb,
                                        this.markValue(),
                                        this.position(),
                                        this.limit(),
                                        this.capacity(),
                                        offset);
    }

把已经在本buffer写的元素移动到0位置,position定位到剩余容量起点,limit限制为capacity

public ByteBuffer compact() {
        System.arraycopy(hb, ix(position()), hb, ix(0), remaining());
        position(remaining());
        limit(capacity());
        discardMark();
        return this;
    }

常用代码段

// 拷贝文件
public void copyFile(String copyFrom,String copyTo){
	File file = new File(copyFrom);
        RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
        FileChannel channel = randomAccessFile.getChannel();

        File outFile = new File(copyTo);
        RandomAccessFile outRAF = new RandomAccessFile(outFile, "rw");
        FileChannel outChannel = outRAF.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (channel.read(byteBuffer) > 0){
            while (byteBuffer.hasRemaining()){
                byteBuffer.flip();
                outChannel.write(byteBuffer);
            }
        }
}

// 从网络中读取文件
public void readNetworkFile(){
		File outFile = new File("d://20200418220925641.png");
        RandomAccessFile outRAF = new RandomAccessFile(outFile, "rw");
        FileChannel outChannel = outRAF.getChannel();
        // get stream from net
        InputStream inputStream = new URL("https://img-blog.csdnimg.cn/20200418220925641.png").openConnection()
            .getInputStream();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        byte[] cache = new byte[2 * 1024];
        ByteBuffer wrap = ByteBuffer.wrap(cache);
        int len;
        while ((len = bufferedInputStream.read(cache)) > 0){
            wrap.position(0);
            wrap.limit(len);
            outChannel.write(wrap);
        }
}

public void writeMsgToClient(){
		ServerSocket serverSocket = new ServerSocket(9988);
		// bad case
        Socket accept = serverSocket.accept();
        SocketChannel channel = accept.getChannel();
        // do business logic and get a byte array
        byte[] rlt = "businessLogicRlt".getBytes();
        ByteBuffer wrap = ByteBuffer.wrap(rlt, 0, rlt.length);
        channel.write(wrap);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章