这是我第一次开始看源码并且试着总结源码,如果有不适的地方欢迎大家及时指正...
NIO中的Buffer是作为一个容器用来存储数据的,根据存储数据类型的不同,分为七种:CharBuffer,ShortBuffer,IntBuffer,LongBuffer,FloatBuffer,DoubleBuffer,ByteBuffer。即除了Boolean类型之外的其它七种基本数据类型都有自己对应的Buffer类,并且都拥有几个重要的属性:mark, position,limit和capacity。这些xxxBuffer都继承了抽象父类Buffer,他们共同的属性也在Buffer父类中声明;下面是java.nio的包:
这是CharBuffer类:
其他的六个类的声明和CharBuffer类虽然不完全一样,但是都继承了Buffer父类。那么先看一下Buffer父类:
public abstract class 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;
// Creates a new buffer with the given mark, position, limit, and capacity,
// after checking invariants.
//
Buffer(int mark, int pos, int lim, int cap) { // package-private
if (cap < 0)
throw new IllegalArgumentException("Negative capacity: " + cap);
this.capacity = cap;
limit(lim);
position(pos);
if (mark >= 0) {
if (mark > pos)
throw new IllegalArgumentException("mark > position: ("
+ mark + " > " + pos + ")");
this.mark = mark;
}
}
/**
* Returns this buffer's capacity. </p>
*
* @return The capacity of this buffer
*/
public final int capacity() {
return capacity;
}
/**
* Returns this buffer's position. </p>
*
* @return The position of this buffer
*/
public final int position() {
return position;
}
/**
* Sets this buffer's position. If the mark is defined and larger than the
* new position then it is discarded. </p>
*
* @param newPosition
* The new position value; must be non-negative
* and no larger than the current limit
*
* @return This buffer
*
* @throws IllegalArgumentException
* If the preconditions on <tt>newPosition</tt> do not hold
*/
public final Buffer position(int newPosition) {
if ((newPosition > limit) || (newPosition < 0))
throw new IllegalArgumentException();
position = newPosition;
if (mark > position) mark = -1;
return this;
}
/**
* Returns this buffer's limit. </p>
*
* @return The limit of this buffer
*/
public final int limit() {
return limit;
}
/**
* Sets this buffer's limit. If the position is larger than the new limit
* then it is set to the new limit. If the mark is defined and larger than
* the new limit then it is discarded. </p>
*
* @param newLimit
* The new limit value; must be non-negative
* and no larger than this buffer's capacity
*
* @return This buffer
*
* @throws IllegalArgumentException
* If the preconditions on <tt>newLimit</tt> do not hold
*/
public final Buffer limit(int newLimit) {
if ((newLimit > capacity) || (newLimit < 0))
throw new IllegalArgumentException();
limit = newLimit;
if (position > limit) position = limit;
if (mark > limit) mark = -1;
return this;
}
/**
* Sets this buffer's mark at its position. </p>
*
* @return This buffer
*/
public final Buffer mark() {
mark = position;
return this;
}
/**
* Resets this buffer's position to the previously-marked position.
*
* <p> Invoking this method neither changes nor discards the mark's
* value. </p>
*
* @return This buffer
*
* @throws InvalidMarkException
* If the mark has not been set
*/
public final Buffer reset() {
int m = mark;
if (m < 0)
throw new InvalidMarkException();
position = m;
return this;
}
/**
* Clears this buffer. The position is set to zero, the limit is set to
* the capacity, and the mark is discarded.
*
* <p> Invoke this method before using a sequence of channel-read or
* <i>put</i> operations to fill this buffer. For example:
*
* <blockquote><pre>
* buf.clear(); // Prepare buffer for reading
* in.read(buf); // Read data</pre></blockquote>
*
* <p> This method does not actually erase the data in the buffer, but it
* is named as if it did because it will most often be used in situations
* in which that might as well be the case. </p>
*
* @return This buffer
*/
public final Buffer clear() {
position = 0;
limit = capacity;
mark = -1;
return this;
}
/**
* Flips this buffer. The limit is set to the current position and then
* the position is set to zero. If the mark is defined then it is
* discarded.
*
* <p> After a sequence of channel-read or <i>put</i> operations, invoke
* this method to prepare for a sequence of channel-write or relative
* <i>get</i> operations. For example:
*
* <blockquote><pre>
* buf.put(magic); // Prepend header
* in.read(buf); // Read data into rest of buffer
* buf.flip(); // Flip buffer
* out.write(buf); // Write header + data to channel</pre></blockquote>
*
* <p> This method is often used in conjunction with the {@link
* java.nio.ByteBuffer#compact compact} method when transferring data from
* one place to another. </p>
*
* @return This buffer
*/
public final Buffer flip() {
limit = position;
position = 0;
mark = -1;
return this;
}
/**
* Rewinds this buffer. The position is set to zero and the mark is
* discarded.
*
* <p> Invoke this method before a sequence of channel-write or <i>get</i>
* operations, assuming that the limit has already been set
* appropriately. For example:
*
* <blockquote><pre>
* out.write(buf); // Write remaining data
* buf.rewind(); // Rewind buffer
* buf.get(array); // Copy data into array</pre></blockquote>
*
* @return This buffer
*/
public final Buffer rewind() {
position = 0;
mark = -1;
return this;
}
/**
* Returns the number of elements between the current position and the
* limit. </p>
*
* @return The number of elements remaining in this buffer
*/
public final int remaining() {
return limit - position;
}
/**
* Tells whether there are any elements between the current position and
* the limit. </p>
*
* @return <tt>true</tt> if, and only if, there is at least one element
* remaining in this buffer
*/
public final boolean hasRemaining() {
return position < limit;
}
/**
* Tells whether or not this buffer is read-only. </p>
*
* @return <tt>true</tt> if, and only if, this buffer is read-only
*/
public abstract boolean isReadOnly();
/**
* Tells whether or not this buffer is backed by an accessible
* array.
*
* <p> If this method returns <tt>true</tt> then the {@link #array() array}
* and {@link #arrayOffset() arrayOffset} methods may safely be invoked.
* </p>
*
* @return <tt>true</tt> if, and only if, this buffer
* is backed by an array and is not read-only
*
* @since 1.6
*/
public abstract boolean hasArray();
/**
* Returns the array that backs this
* buffer <i>(optional operation)</i>.
*
* <p> This method is intended to allow array-backed buffers to be
* passed to native code more efficiently. Concrete subclasses
* provide more strongly-typed return values for this method.
*
* <p> Modifications to this buffer's content will cause the returned
* array's content to be modified, and vice versa.
*
* <p> Invoke the {@link #hasArray hasArray} method before invoking this
* method in order to ensure that this buffer has an accessible backing
* array. </p>
*
* @return The array that backs this buffer
*
* @throws ReadOnlyBufferException
* If this buffer is backed by an array but is read-only
*
* @throws UnsupportedOperationException
* If this buffer is not backed by an accessible array
*
* @since 1.6
*/
public abstract Object array();
/**
* Returns the offset within this buffer's backing array of the first
* element of the buffer <i>(optional operation)</i>.
*
* <p> If this buffer is backed by an array then buffer position <i>p</i>
* corresponds to array index <i>p</i> + <tt>arrayOffset()</tt>.
*
* <p> Invoke the {@link #hasArray hasArray} method before invoking this
* method in order to ensure that this buffer has an accessible backing
* array. </p>
*
* @return The offset within this buffer's array
* of the first element of the buffer
*
* @throws ReadOnlyBufferException
* If this buffer is backed by an array but is read-only
*
* @throws UnsupportedOperationException
* If this buffer is not backed by an accessible array
*
* @since 1.6
*/
public abstract int arrayOffset();
/**
* Tells whether or not this buffer is
* <a href="ByteBuffer.html#direct"><i>direct</i></a>. </p>
*
* @return <tt>true</tt> if, and only if, this buffer is direct
*
* @since 1.6
*/
public abstract boolean isDirect();
// -- Package-private methods for bounds checking, etc. --
/**
* Checks the current position against the limit, throwing a {@link
* BufferUnderflowException} if it is not smaller than the limit, and then
* increments the position. </p>
*
* @return The current position value, before it is incremented
*/
final int nextGetIndex() { // package-private
if (position >= limit)
throw new BufferUnderflowException();
return position++;
}
final int nextGetIndex(int nb) { // package-private
if (limit - position < nb)
throw new BufferUnderflowException();
int p = position;
position += nb;
return p;
}
/**
* Checks the current position against the limit, throwing a {@link
* BufferOverflowException} if it is not smaller than the limit, and then
* increments the position. </p>
*
* @return The current position value, before it is incremented
*/
final int nextPutIndex() { // package-private
if (position >= limit)
throw new BufferOverflowException();
return position++;
}
final int nextPutIndex(int nb) { // package-private
if (limit - position < nb)
throw new BufferOverflowException();
int p = position;
position += nb;
return p;
}
/**
* Checks the given index against the limit, throwing an {@link
* IndexOutOfBoundsException} if it is not smaller than the limit
* or is smaller than zero.
*/
final int checkIndex(int i) { // package-private
if ((i < 0) || (i >= limit))
throw new IndexOutOfBoundsException();
return i;
}
final int checkIndex(int i, int nb) { // package-private
if ((i < 0) || (nb > limit - i))
throw new IndexOutOfBoundsException();
return i;
}
final int markValue() { // package-private
return mark;
}
final void truncate() { // package-private
mark = -1;
position = 0;
limit = 0;
capacity = 0;
}
final void discardMark() { // package-private
mark = -1;
}
static void checkBounds(int off, int len, int size) { // package-private
if ((off | len | (off + len) | (size - (off + len))) < 0)
throw new IndexOutOfBoundsException();
}
}
在Buffer类中,定义了五个属性,其中重要的是前四个:mark,position,limit和capacity;
根据源码中的注释:
- capacity:指Buffer所包含的元素的数量,capacity的值永远不为负并且一旦声明不可更改;(实现时底层是数组,一旦申请空间空间大小就是固定的)。创建的Buffer的每一个元素的默认值都被初始化为0;
- limit指的是Buffer中第一个不能被访问(读或者写)的元素的下标,值不为负且不大于capacity。limit的初始值默认是0,或者取决于你在构造方法中的定义或者不同的Buffer类型,默认值不确定。
- posotion:是下一个可以被读或者写的元素的下标,不为负且不大于limit的值;在当前位置读或者写一个或者对个元素,position的值会根据传输的元素的个数进行增加,永远指向下一个可被传输的数据。新创建的Buffer的position默认值为0
-
mark:如果你调用了mark()方法,则把当前position的值赋给mark,调用reset()方法可以将position的值重置为mark的值;新创建的Buffer的mark默认是undefined
- 构造方法
指定mark、position、limit和capacity,创建一个Buffer,构造方法会对传递进来的属性值进行判断,不合理的话抛出异常;
- mark()
就是把当前的position保存在mark中
- reset()
把当前position的值还原为之前保存的position的值,也就是mark的值
- clear()
可见,clear()方法将Buffer的position和limit、mark进行重置,忘记掉了刚才操作之后的状态,但是并没有删除Buffer中的数据!数据依旧存在在Buffer中。并不是真正的删除操作。
可以在切换读写状态的时候使用,下面是注释中的例子:
- flp()
和clear()很相似,把position置0,但是limit的值为position,而不是clear中的capacity。限制了下次操作的数据的范围不能超过flp()之前的位置。多用于传输数据。
- remain与hasRemaining()
判断是否还有可操作的数据
- 最后还有一些判断是否可读,是否是直接缓存区等等的抽象方法:
以上就是Buffer类的基本情况;
接下来看一下它的具体实现类:
拿CharBuffer和ByteBuffer来看,底层都是存放对应数据类型的数组,可以通过传递数据直接构造一个自己类型的Buffer对象;
也通过各自Buffer实现类的静态的allocate方法创建缓冲区,创建了一个HeapxxxBufffer类型的缓冲区:
可以看到,allocate()方法本质上还是调用了CharBuffer自己的构造方法,在JVM的内存堆中创建了缓冲区;
使用warp()方法,可以自己创建一个char[],即自己指定缓冲区,但是还是创建的是HeapCharBuffer,即JVM堆中的非直接缓冲区;(关于直接缓冲区和非直接缓冲区的概念在上一篇基本概念的博客里写到)
还有一种分配缓冲区的方法,分配的是直接缓冲区,但是只支持ByteBuffer,在ByteBuffer类中的静态allocateDirect方法:
Unsafe:
可以看到分配内存的是naive方法,调用的是操作系统的指令,缓冲区分配到了物理内存上....这就是直接缓冲区。
比较重要的就是这些了吧,随后有机会再更新。。。