CharBuffers

A CharBuffer works pretty much the same as an IntBuffer, a DoubleBuffer, a LongBuffer, and any of the other multibyte buffer types. That is, it has absolute and relative put methods that put chars; absolute and relative get methods that get chars; bulk put and get methods that put and get arrays of chars; and methods to flip, clear, rewind, and so forth. CharBuffer also has some convenience methods that operate on CharSequences (usually Strings, but also StringBuffers, StringBuilders, and so on). Furthermore, the CharBuffer class itself implements CharSequence, so you can pass it to methods such as Java 6's Normalizer.isNormalized( ), query it with regular expressions, and generally use it as you would any string.

A CharBuffer can be created in the three usual ways: by allocation, by wrapping a preexisting char[] array, or as a view of a ByteBuffer. A CharBuffer can also be created by wrapping a CharSequence:

public static CharBuffer wrap(CharSequence sequence)

or from a subsequence beginning at a certain offset and continuing to a certain point:

public static CharBuffer wrap(CharSequence sequence, int start, int end)

Because CharSequence is a read-only interface (i.e., it has no setter methods), the buffers created in this way are also read-only. You can get from them, but you can't put into them. Attempting to do so throws a ReadOnlyBufferException.

Категории