1.ByteBuffer的解析
http://blog.csdn.net/ya_1249463314/article/details/79271333
2.什麼是堆位元組緩衝區
HeapByteBuffer堆位元組緩衝區是將緩衝區分配在jvm中的heap堆上,其實現本身是一個位元組數組,實際上就是作業系統中的使用者記憶體,而不是核心記憶體。
3.所屬包
package java.nio;
4.繼承與實現關係
class HeapByteBuffer extends ByteBuffer
5.構造器
/** * 指定參數構造器, * 初始化標記mark為-1,設定堆緩衝數組空間大小為cap,位元組數組的位移量為0 */ HeapByteBuffer(int cap, int lim) { super(-1, 0, lim, cap, new byte[cap], 0); }/** * 指定參數構造器, * 初始化標記mark為-1,設定讀寫的開始位置為off,讀寫的最大容量為off+len * 設定緩衝區的最大容量為buf位元組數組的容量,設定堆緩衝數組為buf,位元組數組的位移量為0 */ HeapByteBuffer(byte[] buf, int off, int len) { super(-1, off, off + len, buf.length, buf, 0); }/** * 指定參數構造器, * 初始化標記為mark,設定讀寫的開始位置為pos,讀寫的最大容量為lim * 設定緩衝區的最大容量為cap,設定堆緩衝數組為buf,位元組數組的位移量為off */ protected HeapByteBuffer(byte[] buf, int mark, int pos, int lim, int cap, int off) { super(mark, pos, lim, cap, buf, off); }
6.常用方法
/** * 建立新的堆位元組緩衝區,其內容是此緩衝區內容的共用子序列 * 標記mark為-1,讀寫開始位置為0,讀寫的最大容量為緩衝區剩餘元素的容量 * 緩衝區的最大容量為緩衝區剩餘元素的容量,位元組數組的位移量為當前讀寫位置加上原位元組數組的位移量 */ public ByteBuffer slice() { return new HeapByteBuffer(hb, -1, 0, this.remaining(), this.remaining(), this.position() + offset); }/** * 建立共用此緩衝區內容的新的堆位元組緩衝區 * 標記mark為當前的緩衝區的mark,讀寫的開始位置為當前緩衝區的pos, * 讀寫的最大容量為當前緩衝區的lim,緩衝區的最大容量為當前緩衝區的最大容量cap * 數組的位移量為當前數組的位移量offset */ public ByteBuffer duplicate() { return new HeapByteBuffer(hb, this.markValue(), this.position(), this.limit(), this.capacity(), offset); }/** * 建立一個唯讀堆位元組緩衝區 * 標記mark為當前的緩衝區的mark,讀寫的開始位置為當前緩衝區的pos, * 讀寫的最大容量為當前緩衝區的lim,緩衝區的最大容量為當前緩衝區的最大容量cap * 數組的位移量為當前數組的位移量offset */ public ByteBuffer asReadOnlyBuffer() { return new HeapByteBufferR(hb, this.markValue(), this.position(), this.limit(), this.capacity(), offset); }//設定讀取位元組數組的位移量為i+offset protected int ix(int i) { return i + offset; }//擷取下一個位元組 public byte get() { return hb[ix(nextGetIndex())]; }//擷取指定下標的位元組 public byte get(int i) { return hb[ix(checkIndex(i))]; }//將當前的緩衝區中的位元組數組從position加1中開始複製到輸入的位元組數組dst中 public ByteBuffer get(byte[] dst, int offset, int length) {//檢查下標是否越界 checkBounds(offset, length, dst.length);//檢查緩衝區是否溢出 if (length > remaining()) throw new BufferUnderflowException();//將緩衝區中的位元組數組的指定位置position+offset開始複製位元組到指定的dst位元組數組 System.arraycopy(hb, ix(position()), dst, offset, length);//設定讀寫的開始位置為position+length position(position() + length);//返回當前的位元組緩衝區 return this; }//緩衝區是否為直接在核心上緩衝區,返回false,因為現在是在jvm堆位元組緩衝區 public boolean isDirect() { return false; }//緩衝區是否為唯讀 public boolean isReadOnly() { return false; }//儲存位元組x到位元組緩衝區中,返回位元組緩衝區 public ByteBuffer put(byte x) { hb[ix(nextPutIndex())] = x; return this; }//將指定位元組緩衝區的位置的位元組更新為x,返回位元組緩衝區 public ByteBuffer put(int i, byte x) { hb[ix(checkIndex(i))] = x; return this; }//將指定的位元組數組src的元素儲存到當前的位元組緩衝區中 public ByteBuffer put(byte[] src, int offset, int length) {//檢查下標是否越界 checkBounds(offset, length, src.length);//檢查緩衝區是否溢出 if (length > remaining()) throw new BufferOverflowException();//將輸入的位元組數組中src從指定位移量offset開始複製位元組到當前的位元組緩衝區 System.arraycopy(src, offset, hb, ix(position()), length);//設定當前的位元組緩衝區中的讀寫開始位置為原有的position加上新增的位元組數組的length position(position() + length);//返回當前的位元組緩衝區 return this; }//將指定的位元組緩衝區的位元組儲存到當前的位元組緩衝區 public ByteBuffer put(ByteBuffer src) {//判斷src是否為堆位元組緩衝區 if (src instanceof HeapByteBuffer) { if (src == this) throw new IllegalArgumentException();//擷取堆位元組緩衝區 HeapByteBuffer sb = (HeapByteBuffer)src;//擷取位元組緩衝區中剩餘位元組的容量 int n = sb.remaining(); if (n > remaining()) throw new BufferOverflowException();//將指定的位元組緩衝區的位元組複製到當前的位元組緩衝區中 System.arraycopy(sb.hb, sb.ix(sb.position()), hb, ix(position()), n);//設定輸入的位元組緩衝區src的讀寫開始位置 sb.position(sb.position() + n);//設定當前緩衝區的讀寫開始位置 position(position() + n); } else if (src.isDirect()) {//判斷src是否為直接核心緩衝區//擷取輸入位元組緩衝區的位元組容量為n int n = src.remaining(); if (n > remaining()) throw new BufferOverflowException();//將指定的位元組緩衝區src中的位元組寫入到當前的位元組緩衝區hb src.get(hb, ix(position()), n);//設定緩衝區中讀寫的開始位置為position position(position() + n); } else {//將輸入的指定緩衝區中的元素轉移到另一個緩衝區中 super.put(src); } return this; }//壓縮當前的位元組緩衝區 public ByteBuffer compact() {//將當前的緩衝區中的位元組數組hb從position+offset開始複製到hb中 System.arraycopy(hb, ix(position()), hb, ix(0), remaining());//設定當前的緩衝區中讀寫的開始位置為位元組數組的剩餘元素大小 position(remaining());//設定讀寫的最大容量為當前緩衝區的最大容量 limit(capacity());//設定標記mark為-1 discardMark(); return this; }//擷取指定下標的位元組 byte _get(int i) { return hb[i]; }//將指定下標的位元組更新為b void _put(int i, byte b) { hb[i] = b; }