public abstract class Buffer extends Object
缓冲区是一个特定的原始类型的元素的线性的、有限的序列。除了它的内容,缓冲区的基本属性是它的容量,限制和位置:
A buffer's capacity is the number of elements it contains. The capacity of a buffer is never negative and never changes.
A buffer's limit 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.
A buffer's position 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.
这个类的一个子类为每个非布尔类型的原始类型。
每个子类,这个类定义了两类得到和放操作:
Relative 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 get operation throws a
BufferUnderflowException
and a relative put operation throws aBufferOverflowException
; in either case, no data is transferred.Absolute operations take an explicit element index and do not affect the position. Absolute get and put operations throw an
IndexOutOfBoundsException
if the index argument exceeds the limit.
当然,数据也可以通过一个合适的信道的I / O操作转移到或出一个缓冲区,而一个合适的信道总是相对于当前位置。
一个缓冲的作记号是指标,其位置将被重置时,reset
方法被调用。标记并不是总是定义的,但是当它被定义时,它永远不会是负面的,并且永远不会大于它的位置。如果标记被定义,那么当位置或限制被调整到比标记小的值时,它就被丢弃了。如果商标没有定义,然后调用reset
方法导致InvalidMarkException
被。
以下不变保持标记、位置、限制和容量值:
0 <= mark <= position <= limit <= capacity
一个新创建的缓冲区始终有一个零的位置和一个未定义的标记。最初的限制可能是零,或者它可能是一些其他的值取决于缓冲区的类型和它的方式,它是构造。一个新分配的缓冲区的每个元素都被初始化为零。
除了访问位置、极限和容量值的方法和标记和复位的方法外,该类还定义了缓冲区中的下列操作:
clear()
使缓冲准备一种新的通道读或相对放操作顺序:设置限制的能力和位置为零。
flip()
使缓冲准备一系列新的频道写或相对得到操作:设置限制的当前位置,然后设置位置为零。
rewind()
使缓冲准备重新阅读它已经包含数据:它留下的限制不变,设置为零的位置。
每个缓冲区是可读的,但不是每个缓冲区写入。每个缓冲区类突变方法被指定为可选操作,将ReadOnlyBufferException
当调用在只读缓冲区。只读缓冲区不允许改变其内容,但其标志,位置,和极限值是可变的。是否一个缓冲区是只读的可以通过调用其isReadOnly
法测定。
缓冲区是不安全的,使用多个并发线程。如果一个缓冲区被一个以上的线程使用,那么访问缓冲区应该通过适当的同步控制。
在这个类中,不以其他方式有一个返回的值被指定返回它们被调用的缓冲区。这允许方法调用链;例如,语句序列
可以由单取代,更紧凑的语句b.flip(); b.position(23); b.limit(42);
b.flip().position(23).limit(42);
Modifier and Type | Method and Description |
---|---|
abstract Object |
array()
返回数组,支持这个缓冲区 (可选操作)。
|
abstract int |
arrayOffset()
返回在缓冲 第一元这个缓冲区支持数组的偏移(可选操作)。
|
int |
capacity()
返回此缓冲区的容量。
|
Buffer |
clear()
清除此缓冲区。
|
Buffer |
flip()
翻转这个缓冲区。
|
abstract boolean |
hasArray()
告诉是否这个缓冲区是由一个可访问的数组支持的。
|
boolean |
hasRemaining()
告诉当前位置和极限之间是否有任何元素。
|
abstract boolean |
isDirect()
告诉这是否是
direct缓冲。
|
abstract boolean |
isReadOnly()
告诉是否该缓冲区是只读的。
|
int |
limit()
返回此缓冲区的限制。
|
Buffer |
limit(int newLimit)
设置此缓冲区的限制。
|
Buffer |
mark()
设置此缓冲区的标记位置。
|
int |
position()
返回此缓冲区的位置。
|
Buffer |
position(int newPosition)
设置此缓冲区的位置。
|
int |
remaining()
返回当前位置和极限之间的元素的数目。
|
Buffer |
reset()
重置此缓冲区的位置之前标记的位置。
|
Buffer |
rewind()
将此缓冲区。
|
public final int capacity()
public final int position()
public final Buffer position(int newPosition)
newPosition
-新的位置值;必须是非负的且不大于电流限制
IllegalArgumentException
如果
newPosition前提不住
public final int limit()
public final Buffer limit(int newLimit)
newLimit
-新的限值;必须是非负的,没有比这更大的缓冲容量
IllegalArgumentException
如果
newLimit前提不住
public final Buffer mark()
public final Buffer reset()
调用这个方法不改变也不会丢弃马克的价值。
InvalidMarkException
如果标记没有设置
public final Buffer clear()
调用此方法之前使用一个序列的信道读取或放操作来填补这个缓冲区。例如:
buf.clear(); // Prepare buffer for reading in.read(buf); // Read data
这种方法并没有真正删除缓冲区中的数据,但它被命名为,如果它做了,因为它会最经常被使用的情况下,这可能是这样的情况下。
public final Buffer flip()
一个序列的信道读取或放操作后,调用这个方法来准备一个序列的信道写或相对得到操作。例如:
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
这种方法经常结合使用compact
方法传送数据时,从一个地方到另一个。
public final Buffer rewind()
调用此方法之前,一个序列的信道或得到写操作,假设限制已经被适当的设置。例如:
out.write(buf); // Write remaining data buf.rewind(); // Rewind buffer buf.get(array); // Copy data into array
public final int remaining()
public final boolean hasRemaining()
public abstract boolean isReadOnly()
public abstract boolean hasArray()
如果此方法返回true然后array
和arrayOffset
方法可以被调用。
public abstract Object array()
此方法的目的是允许数组支持的缓冲区将更有效地传递给本地代码。具体的子类为该方法提供更强类型的返回值。
此缓冲区的内容的修改将导致返回的数组的内容进行修改,反之亦然。
调用hasArray
方法调用该方法以确保缓冲区有一个可访问的支持,前阵。
ReadOnlyBufferException
-如果这个缓冲区有一个数组不过是只读的
UnsupportedOperationException
-如果这个缓冲区不是由访问阵列支持
public abstract int arrayOffset()
如果这个缓冲区是一个数组缓冲区位置P对应的数组索引P + arrayOffset()支持。
调用hasArray
方法调用该方法以确保缓冲区有一个可访问的支持,前阵。
ReadOnlyBufferException
-如果这个缓冲区有一个数组不过是只读的
UnsupportedOperationException
-如果这个缓冲区不是由访问阵列支持
public abstract boolean isDirect()
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.