- package java.nio;
- public abstract class ByteBuffer
- extends Buffer
- implements Comparable
- {
- /*申请指定空间的ByteBuffer,DirectByteBuffer与HeapByteBuffer的区别稍后介绍*/
- public static ByteBuffer allocate(int capacity) {
- if (capacity < 0)
- throw new IllegalArgumentException();
- return new HeapByteBuffer(capacity, capacity);
- }
- public static ByteBuffer allocateDirect(int capacity) {
- // Android-changed: Android's DirectByteBuffers carry a MemoryRef.
- // return new DirectByteBuffer(capacity);
- DirectByteBuffer.MemoryRef memoryRef = new DirectByteBuffer.MemoryRef(capacity);
- return new DirectByteBuffer(capacity, memoryRef);
- }
-
- /*Byte数组转ByteBuffer*/
- public static ByteBuffer wrap(byte[] array,
- int offset, int length)
- {
- try {
- return new HeapByteBuffer(array, offset, length);
- } catch (IllegalArgumentException x) {
- throw new IndexOutOfBoundsException();
- }
- }
- public static ByteBuffer wrap(byte[] array) {
- return wrap(array, 0, array.length);
- }
-
- /*ByteBuffer转Byte数组*/
- public abstract Object array();
-
- /*get 方法,会改变position,remain不足会抛BufferUnderflowException异常*/
- public ByteBuffer get(byte[] dst, int offset, int length) {
- checkBounds(offset, length, dst.length);
- if (length > remaining())
- throw new BufferUnderflowException();
- int end = offset + length;
- for (int i = offset; i < end; i++)
- dst[i] = get(); //这个不带参数的get将会调用HeapByteBuffer的get方法,所以他会需要自增position.
- return this;
- }
-
- public final int remaining() {
- return limit - position;
- }
-
- /*put方法,会改变position,抛出BufferOverflowException异常*/
- public abstract ByteBuffer put(byte b);
- public abstract ByteBuffer put(int index, byte b);
- /*当参数为ByteBuffer时,参数中的src的position也会改变,如果目标buffer剩余位置小于*/
- public ByteBuffer put(ByteBuffer src) {
- if (src == this)
- throw new IllegalArgumentException();
- if (isReadOnly())
- throw new ReadOnlyBufferException();
- int n = src.remaining();
- if (n > remaining())
- throw new BufferOverflowException();
- ......
- }
- public ByteBuffer put(byte[] src, int offset, int length) {
- checkBounds(offset, length, src.length);
- if (length > remaining())
- throw new BufferOverflowException();
- int end = offset + length;
- for (int i = offset; i < end; i++)
- this.put(src[i]);
- return this;
- }
-
- /*rewind,重置position*/
- public Buffer rewind() {
- position = 0;
- mark = -1;
- return this;
- }
- /*flip方法要注意,他不但会改变position,还会改变limit,所以在put、get、remaining前后用这个要慎重*/
- public Buffer flip() {
- limit = position;
- position = 0;
- mark = -1;
- return this;
- }
-
- /*position,直接设置position*/
- public Buffer position(int newPosition) {
- if ((newPosition > limit) || (newPosition < 0))
- // Android-changed: Improved error message.
- throw new IllegalArgumentException("Bad position " + newPosition + "/" + limit);
- position = newPosition;
- if (mark > position) mark = -1;
- return this;
- }
-
-
- }
ByteBuffer有两种实现方式:HeapByteBuffer基于Java堆的实现,而DirectByteBuffer 使用了 unsafed 的API 进行了对外的实现。HeapByteBuffer就是创建效率高,读取和写入的效率低。DirectByteBuffer是创建效率低,读取和写入的效率高。