CharBuffer

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

A char buffer.

This class defines four categories of operations upon char buffers:

  • Absolute and relative get and put methods that read and write single chars;

  • Relative bulk get methods that transfer contiguous sequences of chars from this buffer into an array; and

  • Relative bulk put methods that transfer contiguous sequences of chars from a char array, a string, or some other char buffer into this buffer; and

  • Methods for compacting, duplicating, and slicing a char buffer.

Char buffers can be created either by allocation, which allocates space for the buffer's content, by wrapping an existing char array or string into a buffer, or by creating a view of an existing byte buffer.

Like a byte buffer, a char buffer is either direct or non-direct. A char buffer created via the wrap methods of this class will be non-direct. A char buffer created as a view of a byte buffer will be direct if, and only if, the byte buffer itself is direct. Whether or not a char buffer is direct may be determined by invoking the isDirect method.

This class implements the CharSequence interface so that character buffers may be used wherever character sequences are accepted, for example in the regular-expression package java.util.regex.

Methods in this class that do not otherwise have a value to return are specified to return the buffer upon which they are invoked. This allows method invocations to be chained. The sequence of statements

 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);

Public Method Summary

<
static CharBuffer
allocate(int capacity)
Allocates a new char buffer.
CharBuffer
append(char c)
Appends the specified char to this buffer  (optional operation).
CharBuffer
append(CharSequence csq, int start, int end)
Appends a subsequence of the specified character sequence to this buffer  (optional operation).
CharBuffer
append(CharSequence csq)
Appends the specified character sequence to this buffer  (optional operation).
final char[]
array()
Returns the char array that backs this buffer  (optional operation).
final int
arrayOffset()
Returns the offset within this buffer's backing array of the first element of the buffer  (optional operation).
abstract CharBuffer
asReadOnlyBuffer()
Creates a new, read-only char buffer that shares this buffer's content.
final char
charAt(int index)
Reads the character at the given index relative to the current position.
IntStream
chars()
Returns a stream of int zero-extending the char values from this sequence.
abstract CharBuffer
compact()
Compacts this buffer  (optional operation).
int
compareTo(CharBuffer that)
Compares this buffer to another.
abstract CharBuffer
duplicate()
Creates a new char buffer that shares this buffer's content.
boolean
equals(Object ob)
Tells whether or not this buffer is equal to another object.
abstract char
get()
Relative get method.
CharBuffer
get(char[] dst)
Relative bulk get method.
abstract char
get(int index)
Absolute get method.
CharBuffer
get(char[] dst, int offset, int length)
Relative bulk get method.
final boolean
hasArray()
Tells whether or not this buffer is backed by an accessible char array.
int
hashCode()
Returns the current hash code of this buffer.
abstract boolean
isDirect()
Tells whether or not this char buffer is direct.
final int
length()
Returns the length of this character buffer.
abstract ByteOrder
order()
Retrieves this buffer's byte order.
final CharBuffer
put(char[] src)
Relative bulk put method  (optional operation).
abstract CharBuffer
put(int index, char c)
Absolute put method  (optional operation).
CharBuffer
put(CharBuffer src)
Relative bulk put method  (optional operation).
final CharBuffer
put(String src)
Relative bulk put method  (optional operation).
abstract CharBuffer
put(char c)
Relative put method  (optional operation).
CharBuffer
put(String src, int start, int end)
Relative bulk put method  (optional operation).
CharBuffer
put(char[] src, int offset, int length)
Relative bulk put method  (optional operation).
int
read(CharBuffer target)
Attempts to read characters into the specified character buffer.
abstract CharBuffer
slice()
Creates a new char buffer whose content is a shared subsequence of this buffer's content.
abstract CharBuffer
subSequence(int start, int end)
Creates a new character buffer that represents the specified subsequence of this buffer, relative to the current position.
String
toString()
Returns a string containing the characters in this buffer.
static CharBuffer
wrap(CharSequence csq)
Wraps a character sequence into a buffer.
static CharBuffer
wrap(char[] array, int offset, int length)
Wraps a char array into a buffer.
static CharBuffer