内容一、基本概念
java.io.Buffer的相关定义:A container for data of a specific primitive type.
(这点上很类似于DataInputStream,DataOutputStream,不同的是一个提供的是基于流的操作,具有单向,不可逆性;Buffer不再是流了。)
/**
* A container for data of a specific primitive type.
*
* <p> A buffer is a linear, finite sequence of elements of a specific
* primitive type. Aside from its content, the essential properties of a
* buffer are its capacity, limit, and position: </p>
* (0<=position<=limit<=capacity 注:capacity一旦确定就不能更改)
*
* <blockquote>
*
* <p> A buffer's <i>capacity</i> is the number of elements it contains. The
* capacity of a buffer is never negative and never changes. </p>
*
* <p> A buffer's <i>limit</i> is the index of the first element that should
* not be read or written. A buffer's limit is never negative and is never
* greater than its capacity. </p>
*
* <p> A buffer's <i>position</i> is the index of the next element to be
* read or written. A buffer's position is never negative and is never
* greater than its limit. </p>
*
* </blockquote>
内容二、基本操作
* <h4> Transferring data </h4> 存取数据
*
* <p> Each subclass of this class defines two categories(绝对和相对存取) of <i>get</i> and
* <i>put</i> operations: </p>
* (相对以前的IoStream变化体现在操作更为灵活,不仅局限于单向,不可以逆的流操作。)
* <blockquote>
*
* <p> <i>Relative</i> operations read or write one or more elements starting
* at the current position and then increment the position by the number of
* elements transferred. If the requested transfer exceeds the limit then a
* relative <i>get</i> operation throws a {@link BufferUnderflowException}
* and a relative <i>put</i> operation throws a {@link
* BufferOverflowException}; in either case, no data is transferred. </p>
读写异常:
get (if: current+requested > limit) throw: BufferUnderflowException
write (if: current+requested > limit) throw: BufferOverflowException
*
* <p> <i>Absolute</i> operations take an explicit element index and do not
* affect the position. Absolute <i>get</i> and <i>put</i> operations throw
* an {@link IndexOutOfBoundsException} if the index argument exceeds the
* limit. </p>
内容三、标记
* <h4> Marking and resetting </h4>
*在IoStream中也有类似的操作,只是为了这种流操作的单向性。
* <p> A buffer's <i>mark</i> is the index to which its position will be reset
* when the {@link #reset reset} method is invoked. The mark is not always
* defined, but when it is defined it is never negative and is never greater
* than the position. If the mark is defined then it is discarded when the
* position or the limit is adjusted to a value smaller than the mark.(mark被隐含失效)
If the mark is not defined then invoking the {@link #reset reset} method causes an
* {@link InvalidMarkException} to be thrown.
内容四、控制
<h4> Clearing, flipping, and rewinding </h4>
*
* <p> In addition to methods for accessing the position, limit, and capacity
* values and for marking and resetting,
this class also defines the following operations upon buffers:
*
* <ul>
*
* <li><p> {@link #clear} makes a buffer ready for a new sequence of
* channel-read or relative <i>put</i> operations: It sets the limit to the
* capacity and the position to zero. </p></li>
*
* <li><p> {@link #flip} makes a buffer ready for a new sequence of
* channel-write or relative <i>get</i> operations: It sets the limit to the
* current position and then sets the position to zero. </p></li>
*
当我们创建完毕一个Buffer以后,我们应该养成这样的习惯:
(1)放数据前,clear下,确保position从0开始写;(2)取数据前,flip下,确保我们只取有效数据【一般(limit,capacity]间的数据全部是0】。
(3)如果我们要改变流操作的特性,需要对同一份数据进行多次读取的话,那么如果【全部复读】,则直接rewind下;如果【部分复读】则使用mark and reset。
* <li><p> {@link #rewind} makes a buffer ready for re-reading the data that
* it already contains: It leaves the limit unchanged and sets the position
* to zero. </p></li>
*
* </ul>
*
public class ByteBufferDemo {
public static void main(String[] args) {
ByteBuffer buf = ByteBuffer.allocateDirect(12);
buf.clear();//put前准备操作
buf.putInt(10);
buf.putInt(20);
//buf.putInt(30);//留4个字节没放东西,所以有效空间是[0,8)
buf.flip();//get前准备操作
System.out.println(buf.getInt());
System.out.println(buf.getInt());
System.out.println(buf.getInt());//读取时超过了有效范围
// 【全部复读】 buf.rewind(); System.out.println(buf.getInt()); System.out.println(buf.getInt()); // 【部分复读】 System.out.println("演示复读"); buf.rewind(); System.out.println(buf.getInt()); // 现在要对第2个int复读两次 buf.mark(); System.out.println(buf.getInt());//第一次读取 buf.reset();//准备第二次读取 System.out.println(buf.getInt()); /* 10 * 20 * Exception in thread "main" java.nio.BufferUnderflowException at java.nio.Buffer.nextGetIndex(Buffer.java:404) at java.nio.DirectByteBuffer.getInt(DirectByteBuffer.java:620) at com.umpay.construct.ByteBufferDemo.main(ByteBufferDemo.java:17) * */ }
}
内容五、注意事项
(1)非线程安全
* <h4> Thread safety </h4>
*
* <p> Buffers are not safe (非线程安全)for use by multiple concurrent threads. If a
* buffer is to be used by more than one thread then access to the buffer
* should be controlled by appropriate synchronization.
(2)只读
* <h4> Read-only buffers </h4>
*
* <p> Every buffer is readable, but not every buffer is writable. The
* mutation(指:会改变对象状态的方法) methods of each buffer class are specified as <i>optional
* operations</i> that will throw a {@link ReadOnlyBufferException} when
* invoked upon a read-only buffer. A read-only buffer does not allow its
* content to be changed, but its mark, position, and limit values are mutable. (因为:这些变量都属于内部维护的变量InVar)
* Whether or not a buffer is read-only may be determined by invoking its
* {@link #isReadOnly isReadOnly} method.
*