Most visited

Recently visited

Added in API level 1

CharBuffer

public abstract class CharBuffer
extends Buffer implements Comparable<CharBuffer>, Appendable, CharSequence, Readable

java.lang.Object
   ↳ java.nio.Buffer
     ↳ java.nio.CharBuffer


一个字符缓冲区。

这个类在char缓冲区中定义了四类操作:

Char缓冲区可以通过 allocation创建,该区域为缓冲区内容分配空间, wrapping将现有字符数组或字符串分配到缓冲区中,或通过创建现有字节缓冲区的 view创建。

像字节缓冲区一样,字符缓冲区是direct or non-direct 通过此类的wrap方法创建的字符缓冲区将是非直接的。 当且仅当字节缓冲区本身是直接的时,作为字节缓冲区的视图创建的字符缓冲区将是直接的。 通过调用isDirect方法可以确定字符缓冲区是否是直接的。

该类实现了 CharSequence接口,因此可以在接受字符序列的任何地方使用字符缓冲区,例如在正则表达式包 java.util.regex

指定此类中不具有返回值的方法以返回调用它们的缓冲区。 这允许方法调用被链接。 陈述的顺序

 cb.put("text/");
 cb.put(subtype);
 cb.put("; charset=");
 cb.put(enc);
can, for example, be replaced by the single statement
 cb.put("text/").put(subtype).put("; charset=").put(enc);

Summary

Public methods

static CharBuffer allocate(int capacity)

分配一个新的字符缓冲区。

CharBuffer append(char c)

将指定的char追加到此缓冲区 (可选操作)

CharBuffer append(CharSequence csq, int start, int end)

将指定字符序列的子序列附加到此缓冲区 (可选操作)

CharBuffer append(CharSequence csq)

将指定的字符序列追加到此缓冲区 (可选操作)

final char[] array()

返回支持此缓冲区的char数组 (可选操作)

final int arrayOffset()

返回缓冲区第一个元素 (可选操作)的此缓冲区的后备数组内的偏移量。

abstract CharBuffer asReadOnlyBuffer()

创建一个共享此缓冲区内容的新的只读字符缓冲区。

final char charAt(int index)

读取相对于当前位置的给定索引处的字符。

IntStream chars()

返回 int的数据流, int零延伸 char值。

abstract CharBuffer compact()

压缩此缓冲区 (可选操作)

int compareTo(CharBuffer that)

将此缓冲区与另一个进行比较。

abstract CharBuffer duplicate()

创建一个共享此缓冲区内容的新字符缓冲区。

boolean equals(Object ob)

告诉这个缓冲区是否等于另一个对象。

abstract char get()

相对 获得方法。

CharBuffer get(char[] dst)

相对批量 获取方法。

abstract char get(int index)

绝对 get方法。

CharBuffer get(char[] dst, int offset, int length)

相对批量 获取方法。

final boolean hasArray()

告诉这个缓冲区是否由可访问的char数组支持。

int hashCode()

返回此缓冲区的当前哈希码。

abstract boolean isDirect()

告诉这个字符缓冲区是否是直接的。

final int length()

返回此字符缓冲区的长度。

abstract ByteOrder order()

检索此缓冲区的字节顺序。

final CharBuffer put(char[] src)

相对批量 放置方法 (可选操作)

abstract CharBuffer put(int index, char c)

绝对 放置方法 (可选操作)

CharBuffer put(CharBuffer src)

相对批量 放置方法 (可选操作)

final CharBuffer put(String src)

相对批量 放置方法 (可选操作)

abstract CharBuffer put(char c)

相对 放置方法 (可选操作)

CharBuffer put(String src, int start, int end)

相对批量 放置方法 (可选操作)

CharBuffer put(char[] src, int offset, int length)

相对批量 放置方法 (可选操作)

int read(CharBuffer target)

尝试将字符读入指定的字符缓冲区。

abstract CharBuffer slice()

创建一个新的字符缓冲区,其内容是此缓冲区内容的共享子序列。

abstract CharBuffer subSequence(int start, int end)

创建一个新的字符缓冲区,该缓冲区表示相对于当前位置的此缓冲区的指定子序列。

String toString()

返回包含此缓冲区中字符的字符串。

static CharBuffer wrap(CharSequence csq)

将字符序列包装到缓冲区中。

static CharBuffer wrap(char[] array, int offset, int length)

将char数组包装到缓冲区中。

static CharBuffer wrap(CharSequence csq, int start, int end)

将字符序列包装到缓冲区中。

static CharBuffer wrap(char[] array)

将char数组包装到缓冲区中。

Inherited methods

From class java.nio.Buffer
From class java.lang.Object
From interface java.lang.Comparable
From interface java.lang.Appendable
From interface java.lang.CharSequence
From interface java.lang.Readable

Public methods

allocate

Added in API level 1
CharBuffer allocate (int capacity)

分配一个新的字符缓冲区。

新的缓冲区的位置将为零,其限制将是其容量,其标记将是未定义的,并且其每个元素将被初始化为零。 它将有一个 backing array ,其 array offset将为零。

Parameters
capacity int: The new buffer's capacity, in chars
Returns
CharBuffer The new char buffer
Throws
IllegalArgumentException If the capacity is a negative integer

append

Added in API level 1
CharBuffer append (char c)

将指定的char追加到此缓冲区 (可选操作)

表单 dst.append(c)的这种方法的调用与调用完全相同

     dst.put(c) 

Parameters
c char: The 16-bit char to append
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer
ReadOnlyBufferException If this buffer is read-only

append

Added in API level 1
CharBuffer append (CharSequence csq, 
                int start, 
                int end)

将指定字符序列的子序列附加到此缓冲区 (可选操作)

形式的这种方法的调用时 dst.append(csq, start, end) csq不是 null,行为以完全相同的方式调用

     dst.put(csq.subSequence(start, end).toString()) 

Parameters
csq CharSequence: The character sequence from which a subsequence will be appended. If csq is null, then characters will be appended as if csq contained the four characters "null".
start int: The index of the first character in the subsequence
end int: The index of the character following the last character in the subsequence
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer
IndexOutOfBoundsException If start or end are negative, start is greater than end, or end is greater than csq.length()
ReadOnlyBufferException If this buffer is read-only

append

Added in API level 1
CharBuffer append (CharSequence csq)

将指定的字符序列追加到此缓冲区 (可选操作)

表单 dst.append(csq)的此方法的调用的行为与调用完全相同

     dst.put(csq.toString()) 

取决于toString字符序列csq本说明书中,整个序列可以不追加。 例如,调用字符缓冲区的toString方法将返回一个子序列,其内容取决于缓冲区的位置和限制。

Parameters
csq CharSequence: The character sequence to append. If csq is null, then the four characters "null" are appended to this character buffer.
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer
ReadOnlyBufferException If this buffer is read-only

array

Added in API level 1
char[] array ()

返回支持此缓冲区的char数组 (可选操作)

修改此缓冲区的内容将导致返回的数组内容被修改,反之亦然。

在调用此方法之前调用 hasArray方法,以确保此缓冲区具有可访问的后备数组。

Returns
char[] The array that backs this buffer
Throws
ReadOnlyBufferException If this buffer is backed by an array but is read-only
UnsupportedOperationException If this buffer is not backed by an accessible array

arrayOffset

Added in API level 1
int arrayOffset ()

返回缓冲区第一个元素 (可选操作)的此缓冲区的后备数组内的偏移量。

如果此缓冲区由数组支持,则缓冲区位置 p对应于数组索引 p + arrayOffset()

在调用此方法之前调用 hasArray方法以确保此缓冲区具有可访问的后备数组。

Returns
int 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
UnsupportedOperationException If this buffer is not backed by an accessible array

asReadOnlyBuffer

Added in API level 1
CharBuffer asReadOnlyBuffer ()

创建一个共享此缓冲区内容的新的只读字符缓冲区。

新缓冲区的内容将是该缓冲区的内容。 此缓冲区内容的更改将在新缓冲区中可见; 但是新的缓冲区本身将是只读的,并且不允许修改共享内容。 两个缓冲区的位置,限制和标记值将是独立的。

新缓冲区的容量,限制,位置和标记值将与该缓冲区的相同。

如果此缓冲区本身是只读的,则此方法的行为方式与 duplicate方法完全相同。

Returns
CharBuffer The new, read-only char buffer

charAt

Added in API level 1
char charAt (int index)

读取相对于当前位置的给定索引处的字符。

Parameters
index int: The index of the character to be read, relative to the position; must be non-negative and smaller than remaining()
Returns
char The character at index position() + index
Throws
IndexOutOfBoundsException If the preconditions on index do not hold

chars

Added in API level 24
IntStream chars ()

返回int的数据流,从这个序列中扩展char值。 映射到surrogate code point的任何字符都将通过未解释的字符传递。

如果在读取流时序列发生了变化,结果是未定义的。

Returns
IntStream an IntStream of char values from this sequence

compact

Added in API level 1
CharBuffer compact ()

压缩此缓冲区 (可选操作)

缓冲区当前位置与其限制之间的字符(如果有)将被复制到缓冲区的开头。 也就是说,索引p = position()处的字符被复制到索引零,索引p + 1处的字符被复制到索引1,依此类推,直到索引limit()-1处的字符被复制到索引n = limit() - 1 - p 然后将缓冲区的位置设置为n + 1,并将其限制设置为其容量。 标记(如果已定义)将被丢弃。

The buffer's position is set to the number of chars copied, rather than to zero, so that an invocation of this method can be followed immediately by an invocation of another relative put method.

Returns
CharBuffer This buffer
Throws
ReadOnlyBufferException If this buffer is read-only

compareTo

Added in API level 1
int compareTo (CharBuffer that)

将此缓冲区与另一个进行比较。

通过按照字典顺序比较剩余元素的序列来比较两个char缓冲区,而不考虑每个序列在其相应缓冲区内的开始位置。 char元素进行比较,就好像调用compare(char, char)

字符缓冲区不能与任何其他类型的对象进行比较。

Parameters
that CharBuffer
Returns
int A negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer

duplicate

Added in API level 1
CharBuffer duplicate ()

创建一个共享此缓冲区内容的新字符缓冲区。

新缓冲区的内容将是该缓冲区的内容。 这个缓冲区内容的改变将在新缓冲区中可见,反之亦然; 两个缓冲区的位置,限制和标记值将是独立的。

The new buffer's capacity, limit, position, and mark values will be identical to those of this buffer. The new buffer will be direct if, and only if, this buffer is direct, and it will be read-only if, and only if, this buffer is read-only.

Returns
CharBuffer The new char buffer

equals

Added in API level 1
boolean equals (Object ob)

告诉这个缓冲区是否等于另一个对象。

两个字符缓冲区是相等的,并且只有当,

  1. 它们具有相同的元素类型,

  2. 他们有相同数量的剩余元素,并且

  3. 独立于其起始位置考虑的剩余元素的两个序列是逐点相等的。

字符缓冲区不等于任何其他类型的对象。

Parameters
ob Object: The object to which this buffer is to be compared
Returns
boolean true if, and only if, this buffer is equal to the given object

get

Added in API level 1
char get ()

相对获得方法。 在此缓冲区的当前位置读取char,然后递增该位置。

Returns
char The char at the buffer's current position
Throws
BufferUnderflowException If the buffer's current position is not smaller than its limit

get

Added in API level 1
CharBuffer get (char[] dst)

相对批量 获取方法。

该方法将来自此缓冲区的字符传输到给定的目标数组中。 调用表单src.get(a)的此方法的行为与调用完全相同

     src.get(a, 0, a.length) 

Parameters
dst char
Returns
CharBuffer This buffer
Throws
BufferUnderflowException If there are fewer than length chars remaining in this buffer

get

Added in API level 1
char get (int index)

Absolute get method. Reads the char at the given index.

Parameters
index int: The index from which the char will be read
Returns
char The char at the given index
Throws
IndexOutOfBoundsException If index is negative or not smaller than the buffer's limit

get

Added in API level 1
CharBuffer get (char[] dst, 
                int offset, 
                int length)

相对批量 获取方法。

该方法将来自此缓冲区的字符传输到给定的目标数组中。 如果缓冲区中剩余的字符数少于满足请求所需的字符数,也就是说,如果length > remaining() ,则不会传输字符并引发BufferUnderflowException

否则,此方法将length字符从此缓冲区复制到给定数组中,从此缓冲区的当前位置开始,并在数组中给定的偏移量处开始。 此缓冲区的位置然后递增length

In other words, an invocation of this method of the form src.get(dst, off, len) has exactly the same effect as the loop

     for (int i = off; i < off + len; i++)
         dst[i] = src.get(); 
except that it first checks that there are sufficient chars in this buffer and it is potentially much more efficient.

Parameters
dst char: The array into which chars are to be written
offset int: The offset within the array of the first char to be written; must be non-negative and no larger than dst.length
length int: The maximum number of chars to be written to the given array; must be non-negative and no larger than dst.length - offset
Returns
CharBuffer This buffer
Throws
BufferUnderflowException If there are fewer than length chars remaining in this buffer
IndexOutOfBoundsException If the preconditions on the offset and length parameters do not hold

hasArray

Added in API level 1
boolean hasArray ()

告诉这个缓冲区是否由可访问的char数组支持。

如果此方法返回 true ,则可安全地调用 arrayarrayOffset方法。

Returns
boolean true if, and only if, this buffer is backed by an array and is not read-only

hashCode

Added in API level 1
int hashCode ()

返回此缓冲区的当前哈希码。

char缓冲区的哈希码仅依赖于其余的元素; 即从position()到元素limit() - 1的元素。

由于缓冲区散列码与内容相关,因此除非知道其内容不会改变,否则在散列映射或类似数据结构中使用缓冲区作为键是不可取的。

Returns
int The current hash code of this buffer

isDirect

Added in API level 1
boolean isDirect ()

告诉这个字符缓冲区是否是直接的。

Returns
boolean true if, and only if, this buffer is direct

length

Added in API level 1
int length ()

返回此字符缓冲区的长度。

当作为字符序列查看时,字符缓冲区的长度只是位置(包括)和限制(不包括)之间的字符数; 也就是说,它相当于remaining()

Returns
int The length of this character buffer

order

Added in API level 1
ByteOrder order ()

检索此缓冲区的字节顺序。

通过分配或包装现有的char阵列创建的字符缓冲区的字节顺序是底层硬件的 native order 创建为字节缓冲区view的字符缓冲区的字节顺序是创建视图时字节缓冲区的字节顺序。

Returns
ByteOrder This buffer's byte order

put

Added in API level 1
CharBuffer put (char[] src)

相对批量 放置方法 (可选操作)

此方法将给定源char数组的全部内容传输到此缓冲区中。 表单dst.put(a)的这种方法的调用方式与调用完全相同

     dst.put(a, 0, a.length) 

Parameters
src char
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer
ReadOnlyBufferException If this buffer is read-only

put

Added in API level 1
CharBuffer put (int index, 
                char c)

绝对 放置方法 (可选操作)

在指定的索引处将给定的char写入此缓冲区。

Parameters
index int: The index at which the char will be written
c char: The char value to be written
Returns
CharBuffer This buffer
Throws
IndexOutOfBoundsException If index is negative or not smaller than the buffer's limit
ReadOnlyBufferException If this buffer is read-only

put

Added in API level 1
CharBuffer put (CharBuffer src)

相对批量 放置方法 (可选操作)

此方法将剩余在给定源缓冲区中的字符传送到此缓冲区。 如果源缓冲区中剩余的字符多于此缓冲区中的字符数,也就是说,如果src.remaining() > remaining() ,则不传送字符并引发BufferOverflowException

否则,此方法将n = src.remaining()字符从给定缓冲区复制到此缓冲区,从每个缓冲区的当前位置开始。 然后两个缓冲器的位置增加n

换句话说,调用这种形式为 dst.put(src)的方法与 循环具有完全相同的效果

     while (src.hasRemaining())
         dst.put(src.get()); 
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

Parameters
src CharBuffer: The source buffer from which chars are to be read; must not be this buffer
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer for the remaining chars in the source buffer
IllegalArgumentException If the source buffer is this buffer
ReadOnlyBufferException If this buffer is read-only

put

Added in API level 1
CharBuffer put (String src)

相对批量 放置方法 (可选操作)

This method transfers the entire content of the given source string into this buffer. An invocation of this method of the form dst.put(s) behaves in exactly the same way as the invocation

     dst.put(s, 0, s.length()) 

Parameters
src String
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer
ReadOnlyBufferException If this buffer is read-only

put

Added in API level 1
CharBuffer put (char c)

相对 放置方法 (可选操作)

将给定的char写入当前位置的缓冲区,然后递增该位置。

Parameters
c char: The char to be written
Returns
CharBuffer This buffer
Throws
BufferOverflowException If this buffer's current position is not smaller than its limit
ReadOnlyBufferException If this buffer is read-only

put

Added in API level 1
CharBuffer put (String src, 
                int start, 
                int end)

相对批量 放置方法 (可选操作)

该方法将chars从给定的字符串传输到此缓冲区。 如果从字符串复制的字符数多于保留在此缓冲区中的字符数,也就是说,如果end - start > remaining() ,则不传送字符并引发BufferOverflowException

否则,此方法将n = end - start字符从给定字符串复制到此缓冲区,从给定的start索引开始并在此缓冲区的当前位置开始。 这个缓冲区的位置然后增加n

换句话说,调用表单 dst.put(src, start, end)的这种方法与 循环具有完全相同的效果

     for (int i = start; i < end; i++)
         dst.put(src.charAt(i)); 
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

Parameters
src String: The string from which chars are to be read
start int: The offset within the string of the first char to be read; must be non-negative and no larger than string.length()
end int: The offset within the string of the last char to be read, plus one; must be non-negative and no larger than string.length()
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer
IndexOutOfBoundsException If the preconditions on the start and end parameters do not hold
ReadOnlyBufferException If this buffer is read-only

put

Added in API level 1
CharBuffer put (char[] src, 
                int offset, 
                int length)

相对批量 放置方法 (可选操作)

该方法将chars从给定的源数组传递到此缓冲区。 如果从阵列中复制的字符多于保留在此缓冲区中的字符数,也就是说,如果length > remaining() ,则不传送字符并引发BufferOverflowException

否则,该方法会将length字符从给定数组复制到此缓冲区,从数组中给定的偏移量和此缓冲区的当前位置开始。 此缓冲区的位置然后递增length

换句话说,调用这种形式为 dst.put(src, off, len)的方法与 循环具有完全相同的效果

     for (int i = off; i < off + len; i++)
         dst.put(a[i]); 
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

Parameters
src char: The array from which chars are to be read
offset int: The offset within the array of the first char to be read; must be non-negative and no larger than array.length
length int: The number of chars to be read from the given array; must be non-negative and no larger than array.length - offset
Returns
CharBuffer This buffer
Throws
BufferOverflowException If there is insufficient space in this buffer
IndexOutOfBoundsException If the preconditions on the offset and length parameters do not hold
ReadOnlyBufferException If this buffer is read-only

read

Added in API level 1
int read (CharBuffer target)

尝试将字符读入指定的字符缓冲区。 该缓冲区原样用作字符的存储库:唯一的更改是放置操作的结果。 不执行翻转或倒带缓冲器。

Parameters
target CharBuffer: the buffer to read characters into
Returns
int The number of characters added to the buffer, or -1 if this source of characters is at its end
Throws
IOException if an I/O error occurs
NullPointerException if target is null
ReadOnlyBufferException if target is a read only buffer

slice

Added in API level 1
CharBuffer slice ()

创建一个新的字符缓冲区,其内容是此缓冲区内容的共享子序列。

新缓冲区的内容将从此缓冲区的当前位置开始。 这个缓冲区内容的改变将在新缓冲区中可见,反之亦然; 两个缓冲区的位置,限制和标记值将是独立的。

新缓冲区的位置将为零,其容量和限制将是此缓冲区中剩余字符的数量,其标记将不确定。 当且仅当此缓冲区是直接的时,新缓冲区才是直接的,并且只有在此缓冲区是只读的情况下,它才会是只读的。

Returns
CharBuffer The new char buffer

subSequence

Added in API level 1
CharBuffer subSequence (int start, 
                int end)

创建一个新的字符缓冲区,该缓冲区表示相对于当前位置的此缓冲区的指定子序列。

新缓冲区将共享此缓冲区的内容; 也就是说,如果这个缓冲区的内容是可变的,那么对一个缓冲区的修改会导致另一个缓冲区被修改。 新缓冲区的容量将为该缓冲区的容量,其位置将为position() + start ,其限制将为position() + end 当且仅当此缓冲区是直接的时,新缓冲区才是直接的,并且只有在此缓冲区是只读的情况下,它才会是只读的。

Parameters
start int: The index, relative to the current position, of the first character in the subsequence; must be non-negative and no larger than remaining()
end int: The index, relative to the current position, of the character following the last character in the subsequence; must be no smaller than start and no larger than remaining()
Returns
CharBuffer The new character buffer
Throws
IndexOutOfBoundsException If the preconditions on start and end do not hold

toString

Added in API level 1
String toString ()

返回包含此缓冲区中字符的字符串。

The first character of the resulting string will be the character at this buffer's position, while the last character will be the character at index limit() - 1. Invoking this method does not change the buffer's position.

Returns
String The specified string

wrap

Added in API level 1
CharBuffer wrap (CharSequence csq)

将字符序列包装到缓冲区中。

新的只读缓冲区的内容将是给定字符序列的内容。 新缓冲区的容量和限制将为csq.length() ,其位置将为零,其标记将不确定。

Parameters
csq CharSequence: The character sequence from which the new character buffer is to be created
Returns
CharBuffer The new character buffer

wrap

Added in API level 1
CharBuffer wrap (char[] array, 
                int offset, 
                int length)

将char数组包装到缓冲区中。

新的缓冲区将由给定的char数组支持; 也就是说,修改缓冲区将导致数组被修改,反之亦然。 新缓冲区容量为array.length ,位置为offset ,限制为offset + length ,其标记为未定义。 它的 backing array将是给定的数组,其 array offset将为零。

Parameters
array char: The array that will back the new buffer
offset int: The offset of the subarray to be used; must be non-negative and no larger than array.length. The new buffer's position will be set to this value.
length int: The length of the subarray to be used; must be non-negative and no larger than array.length - offset. The new buffer's limit will be set to offset + length.
Returns
CharBuffer The new char buffer
Throws
IndexOutOfBoundsException If the preconditions on the offset and length parameters do not hold

wrap

Added in API level 1
CharBuffer wrap (CharSequence csq, 
                int start, 
                int end)

将字符序列包装到缓冲区中。

新的只读缓冲区的内容将是给定字符序列的内容。 缓冲区容量为csq.length() ,位置为start ,限制为end ,其标记未定义。

Parameters
csq CharSequence: The character sequence from which the new character buffer is to be created
start int: The index of the first character to be used; must be non-negative and no larger than csq.length(). The new buffer's position will be set to this value.
end int: The index of the character following the last character to be used; must be no smaller than start and no larger than csq.length(). The new buffer's limit will be set to this value.
Returns
CharBuffer The new character buffer
Throws
IndexOutOfBoundsException If the preconditions on the start and end parameters do not hold

wrap

Added in API level 1
CharBuffer wrap (char[] array)

将char数组包装到缓冲区中。

新的缓冲区将由给定的char数组支持; 也就是说,修改缓冲区将导致数组被修改,反之亦然。 新缓冲区的容量和限制将为array.length ,其位置将为零,并且其标记将不确定。 backing array将是给定的数组,其 array offset将为零。

Parameters
array char: The array that will back this buffer
Returns
CharBuffer The new char buffer

Hooray!