目录
-
源码翻译
package java.nio;
import jdk.internal.HotSpotIntrinsicCandidate;
import jdk.internal.access.JavaNioAccess;
import jdk.internal.access.SharedSecrets;
import jdk.internal.misc.Unsafe;
import java.util.Spliterator;
/**
* 一个特定基础类型的数据容器。
* 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>
*
* <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>
*
* 缓冲区的限制是第一个不应该读或写的元素的索引。缓冲区的限制从不为负,也从不大于其 capacity
* <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>
*
* 缓冲区的位置是要读取或写入的下一个元素的索引。缓冲区的位置从不为负,也不大于其 limit
* <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>
* 对于每个非布尔基元类型,该类有一个子类。
* <p> There is one subclass of this class for each non-boolean primitive type.
*
*
* <h2> Transferring data </h2>
* 种类
* <p> Each subclass of this class defines two categories of <i>get</i> and
* <i>put</i> operations: </p>
*
* <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
* get 会抛出 BufferUnderflowException
* relative <i>get</i> operation throws a {@link BufferUnderflowException}
* put 会抛出 BufferOverflowException
* and a relative <i>put</i> operation throws a {@link
* 无论哪种情况,都不会传输数据。
* BufferOverflowException}; in either case, no data is transferred. </p>
*
* 绝对操作采用显式元素索引,不影响位置。
* <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>
*
* </blockquote>
*
* 当然,数据也可以通过一个合适的通道的I/O操作传入或传出缓冲区,该通道始终与当前位置相关。
* <p> Data may also, of course, be transferred in to or out of a buffer by the
* I/O operations of an appropriate channel, which are always relative to the
* current position.
*
* 标记和重置
* <h2> Marking and resetting </h2>
* 缓冲区的标记是当调用 #reset 方法时,将重置其位置的索引。
* <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. If the
* 如果未定义标记,则调用 #reset 方法会引发@linkInvalidMarkException。
* mark is not defined then invoking the {@link #reset reset} method causes an
* {@link InvalidMarkException} to be thrown.
*
* 不变量
* <h2> Invariants </h2>
* 以下不变量用于标记、位置、限制和容量值
* <p> The following invariant holds for the mark, position, limit, and
* capacity values:
*
* <blockquote>
* {@code 0} {@code <=}
* <i>mark</i> {@code <=}
* <i>position</i> {@code <=}
* <i>limit</i> {@code <=}
* <i>capacity</i>
* </blockquote>
*
* 新创建的缓冲区始终具有零的位置和未定义的标记。
* <p> A newly-created buffer always has a position of zero and a mark that is
* 初始限制可以是零,也可以是其他一些值,这取决于缓冲区的类型和构造方式。
* undefined. The initial limit may be zero, or it may be some other value
* that depends upon the type of the buffer and the manner in which it is
* 新分配的缓冲区的每个元素都初始化为零。
* constructed. Each element of a newly-allocated buffer is initialized
* to zero.
*
* 附加操作
* <h2> Additional operations </h2>
* 除了访问位置、限制和容量值以及标记和重置的方法外,此类还定义了对缓冲区的以下操作
* <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
* 它将设置 limit=capacity ,position=0 .
* 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
* 他将设置 limit=position ,position=0 .
* current position and then sets the position to zero. </p></li>
*
* 使缓冲区准备好重新读取它已经包含的数据:
* <li><p> {@link #rewind} makes a buffer ready for re-reading the data that
* 它保持 limit 不变,position=0 .
* it already contains: It leaves the limit unchanged and sets the position
* to zero. </p></li>
*
* 创建缓冲区的子序列:limit 和 position 都不变 .
* <li><p> {@link #slice} creates a subsequence of a buffer: It leaves the
* limit and the position unchanged. </p></li>
*
* 创建缓冲区的浅副本:limit 和 position 都不变 .
* <li><p> {@link #duplicate} creates a shallow copy of a buffer: It leaves
* the limit and the position unchanged. </p></li>
*
* </ul>
*
* 只读缓冲区
* <h2> Read-only buffers </h2>
* 每个缓冲区都是可读的,但不是每个缓冲区都是可写的。
* <p> Every buffer is readable, but not every buffer is writable. The
* 每个缓冲区类的突变方法被指定为可选操作,
* mutation methods of each buffer class are specified as <I>optional
* 当对只读缓冲区调用时,这些操作将引发@LinkReadOnlyBufferException。
* operations</i> that will throw a {@link ReadOnlyBufferException} when
* i