Top Description Fields Constructors Methods
java.nio

public abstract sealed Class CharBuffer

extends Buffer
implements Comparable<CharBuffer>, Appendable, CharSequence, Readable
permits StringCharBuffer, HeapCharBuffer, DirectCharBufferS, DirectCharBufferU, ByteBufferAsCharBufferB, ByteBufferAsCharBufferL
Class Inheritance
All Implemented Interfaces
java.lang.Readable, java.lang.CharSequence, java.lang.Appendable, java.lang.Comparable
Known Direct Subclasses
java.nio.StringCharBuffer, java.nio.ByteBufferAsCharBufferB, java.nio.ByteBufferAsCharBufferL, java.nio.HeapCharBuffer, java.nio.DirectCharBufferS, java.nio.DirectCharBufferU
Imports
java.io.IOException, java.lang.ref.Reference, java.util.Spliterator, .Objects, java.util.stream.StreamSupport, .IntStream, java.lang.foreign.MemorySegment, jdk.internal.util.ArraysSupport

A char buffer.

This class defines four categories of operations upon char buffers:

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. The methods defined by CharSequence operate relative to the current position of the buffer when they are invoked.

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);
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);
cb.put("text/").put(subtype).put("; charset=").put(enc);

Optional operations

Methods specified as optional operations throw a ReadOnlyBufferException when invoked on a read-only CharBuffer. The methods array and arrayOffset throw an UnsupportedOperationException if the CharBuffer is not backed by an accessible char array (irrespective of whether the CharBuffer is read-only).
Authors
Mark Reinhold, JSR-51 Expert Group
Since
1.4

Field Summary

Modifier and TypeField and Description
private static final long
pack-priv final char[]
pack-priv boolean
pack-priv final int
Inherited from java.nio.Buffer:
addressSCOPED_MEMORY_ACCESSsegmentSPLITERATOR_CHARACTERISTICSUNSAFE

Constructor Summary

AccessConstructor and Description
pack-priv
CharBuffer(int mark, int pos, int lim, int cap, char[] hb, int offset, MemorySegment segment)

pack-priv
CharBuffer(int mark, int pos, int lim, int cap, MemorySegment segment)

pack-priv
CharBuffer(char[] hb, long addr, int cap, MemorySegment segment)

Method Summary

Modifier and TypeMethod and Description
public static CharBuffer

Returns:

The new char buffer
allocate
(int
The new buffer's capacity, in chars
capacity
)

Allocates a new char buffer.

public CharBuffer

Returns:

This buffer
append
(CharSequence
The character sequence to append. If csq is null, then the four characters "null" are appended to this character buffer.
csq
)

Implements java.lang.Appendable.append.

Appends the specified character sequence to this buffer  (optional operation).

public CharBuffer

Returns:

This buffer
append
(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".
csq
,
int
The index of the first character in the subsequence
start
,
int
The index of the character following the last character in the subsequence
end
)

Implements java.lang.Appendable.append.

Appends a subsequence of the specified character sequence to this buffer  (optional operation).

public CharBuffer

Returns:

This buffer
append
(char
The 16-bit char to append
c
)

Implements java.lang.Appendable.append.

Appends the specified char to this buffer  (optional operation).

public final char[]

Returns:

The array that backs this buffer
array
()

Implements abstract java.nio.Buffer.array.

Returns the char array that backs this buffer  (optional operation).

public final int

Returns:

The offset within this buffer's array of the first element of the buffer
arrayOffset
()

Implements abstract java.nio.Buffer.arrayOffset.

Returns the offset within this buffer's backing array of the first element of the buffer  (optional operation).

public abstract CharBuffer

Returns:

The new, read-only char buffer
asReadOnlyBuffer
()

Creates a new, read-only char buffer that shares this buffer's content.

pack-priv Object
base()

Implements abstract java.nio.Buffer.base.

public final char

Returns:

The character at index position() + index
charAt
(int
The index of the character to be read, relative to the position; must be non-negative and smaller than remaining()
index
)

Implements java.lang.CharSequence.charAt.

Reads the character at the given index relative to the current position.

pack-priv abstract ByteOrder
public IntStream
chars()

Overrides default java.lang.CharSequence.chars.

Returns a stream of int zero-extending the char values from this sequence.

public final CharBuffer
clear()

Overrides java.nio.Buffer.clear.

Clears this buffer.

public abstract CharBuffer

Returns:

This buffer
compact
()

Compacts this buffer  (optional operation).

private static int
compare(char x, char y)

public int

Returns:

A negative integer, zero, or a positive integer as this buffer is less than, equal to, or greater than the given buffer
compareTo
(CharBuffer
the object to be compared.
that
)

Implements java.lang.Comparable.compareTo.

Compares this buffer to another.

public abstract CharBuffer

Returns:

The new char buffer
duplicate
()

Implements abstract java.nio.Buffer.duplicate.

Creates a new char buffer that shares this buffer's content.

public boolean

Returns:

true if, and only if, this buffer is equal to the given object
equals
(Object
The object to which this buffer is to be compared
ob
)

Overrides java.lang.Object.equals.

Tells whether or not this buffer is equal to another object.

public final CharBuffer
flip()

Overrides java.nio.Buffer.flip.

Flips this buffer.

public abstract char

Returns:

The char at the buffer's current position
get
()

Relative get method.

public abstract char

Returns:

The char at the given index
get
(int
The index from which the char will be read
index
)

Absolute get method.

public CharBuffer

Returns:

This buffer
get
(char[]
The array into which chars are to be written
dst
,
int
The offset within the array of the first char to be written; must be non-negative and no larger than dst.length
offset
,
int
The maximum number of chars to be written to the given array; must be non-negative and no larger than dst.length - offset
length
)

Relative bulk get method.

public CharBuffer

Returns:

This buffer
get
(char[]
The destination array
dst
)

Relative bulk get method.

public CharBuffer

Returns:

This buffer
get
(int
The index in this buffer from which the first char will be read; must be non-negative and less than limit()
index
,
char[]
The destination array
dst
,
int
The offset within the array of the first char to be written; must be non-negative and less than dst.length
offset
,
int
The number of chars to be written to the given array; must be non-negative and no larger than the smaller of limit() - index and dst.length - offset
length
)

Absolute bulk get method.

public CharBuffer

Returns:

This buffer
get
(int
The index in this buffer from which the first char will be read; must be non-negative and less than limit()
index
,
char[]
The destination array
dst
)

Absolute bulk get method.

private CharBuffer
getArray(int index, char[] dst, int offset, int length)

pack-priv abstract char

Returns:

The char at the given index
getUnchecked
(int
The index from which the char will be read
index
)

Absolute get method.

public final boolean

Returns:

true if, and only if, this buffer is backed by an array and is not read-only
hasArray
()

Implements abstract java.nio.Buffer.hasArray.

Tells whether or not this buffer is backed by an accessible char array.

public int

Returns:

The current hash code of this buffer
hashCode
()

Overrides java.lang.Object.hashCode.

Returns the current hash code of this buffer.

pack-priv boolean

Returns:

true if, and only, this buffer has addressable memory
isAddressable
()

Tells whether this buffer has addressable memory, e.g., a Java array or a native address.

public abstract boolean

Returns:

true if, and only if, this buffer is direct
isDirect
()

Implements abstract java.nio.Buffer.isDirect.

Tells whether or not this char buffer is direct.

public final boolean

Returns:

true if there are 0 remaining characters, otherwise false
isEmpty
()

Overrides default java.lang.CharSequence.isEmpty.

Returns true if this character buffer is empty.

public final int

Returns:

The length of this character buffer
length
()

Implements java.lang.CharSequence.length.

Returns the length of this character buffer.

public final CharBuffer
limit(int
The new limit value; must be non-negative and no larger than this buffer's capacity
newLimit
)

Overrides java.nio.Buffer.limit.

Sets this buffer's limit.

public final CharBuffer
mark()

Overrides java.nio.Buffer.mark.

Sets this buffer's mark at its position.

public int

Returns:

The relative index of the first mismatch between this and the given buffer, otherwise -1 if no mismatch.
mismatch
(CharBuffer
The byte buffer to be tested for a mismatch with this buffer
that
)

Finds and returns the relative index of the first mismatch between this buffer and a given buffer.

public abstract ByteOrder

Returns:

This buffer's byte order
order
()

Retrieves this buffer's byte order.

public final CharBuffer
position(int
The new position value; must be non-negative and no larger than the current limit
newPosition
)

Overrides java.nio.Buffer.position.

Sets this buffer's position.

public abstract CharBuffer

Returns:

This buffer
put
(char
The char to be written
c
)

Relative put method  (optional operation).

public abstract CharBuffer

Returns:

This buffer
put
(int
The index at which the char will be written
index
,
char
The char value to be written
c
)

Absolute put method  (optional operation).

public CharBuffer

Returns:

This buffer
put
(CharBuffer
The source buffer from which chars are to be read; must not be this buffer
src
)

Relative bulk put method  (optional operation).

public CharBuffer

Returns:

This buffer
put
(int
The index in this buffer at which the first char will be written; must be non-negative and less than limit()
index
,
CharBuffer
The buffer from which chars are to be read
src
,
int
The index within the source buffer of the first char to be read; must be non-negative and less than src.limit()
offset
,
int
The number of chars to be read from the given buffer; must be non-negative and no larger than the smaller of limit() - index and src.limit() - offset
length
)

Absolute bulk put method  (optional operation).

public CharBuffer

Returns:

This buffer
put
(char[]
The array from which chars are to be read
src
,
int
The offset within the array of the first char to be read; must be non-negative and no larger than src.length
offset
,
int
The number of chars to be read from the given array; must be non-negative and no larger than src.length - offset
length
)

Relative bulk put method  (optional operation).

public final CharBuffer

Returns:

This buffer
put
(char[]
The source array
src
)

Relative bulk put method  (optional operation).

public CharBuffer

Returns:

This buffer
put
(int
The index in this buffer at which the first char will be written; must be non-negative and less than limit()
index
,
char[]
The array from which chars are to be read
src
,
int
The offset within the array of the first char to be read; must be non-negative and less than src.length
offset
,
int
The number of chars to be read from the given array; must be non-negative and no larger than the smaller of limit() - index and src.length - offset
length
)

Absolute bulk put method  (optional operation).

public CharBuffer

Returns:

This buffer
put
(int
The index in this buffer at which the first char will be written; must be non-negative and less than limit()
index
,
char[]
The array from which chars are to be read
src
)

Absolute bulk put method  (optional operation).

public CharBuffer

Returns:

This buffer
put
(String
The string from which chars are to be read
src
,
int
The offset within the string of the first char to be read; must be non-negative and no larger than string.length()
start
,
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()
end
)

Relative bulk put method  (optional operation).

public final CharBuffer

Returns:

This buffer
put
(String
The source string
src
)

Relative bulk put method  (optional operation).

pack-priv CharBuffer
putArray(int index, char[] src, int offset, int length)

pack-priv void
putBuffer(int pos, CharBuffer src, int srcPos, int n)

public int

Returns:

The number of characters added to the buffer, possibly zero, or -1 if this source of characters is at its end
read
(CharBuffer
the buffer to read characters into
target
)

Implements java.lang.Readable.read.

Attempts to read characters into the specified character buffer.

public final CharBuffer
reset()

Overrides java.nio.Buffer.reset.

Resets this buffer's position to the previously-marked position.

public final CharBuffer
rewind()

Overrides java.nio.Buffer.rewind.

Rewinds this buffer.

public abstract CharBuffer

Returns:

The new char buffer
slice
()

Implements abstract java.nio.Buffer.slice.

Creates a new char buffer whose content is a shared subsequence of this buffer's content.

public abstract CharBuffer

Returns:

The new buffer
slice
(int
The position in this buffer at which the content of the new buffer will start; must be non-negative and no larger than limit()
index
,
int
The number of elements the new buffer will contain; must be non-negative and no larger than limit() - index
length
)

Implements abstract java.nio.Buffer.slice.

Creates a new char buffer whose content is a shared subsequence of this buffer's content.

public abstract CharBuffer

Returns:

The new character buffer
subSequence
(int
The index, relative to the current position, of the first character in the subsequence; must be non-negative and no larger than remaining()
start
,
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()
end
)

Redeclares java.lang.CharSequence.subSequence.

Creates a new character buffer that represents the specified subsequence of this buffer, relative to the current position.

public String

Returns:

The specified string
toString
()

Overrides java.lang.Object.toString.

Implements java.lang.CharSequence.toString.

Returns a string containing the characters in this buffer.

pack-priv abstract String
toString(int start, int end)

public static CharBuffer

Returns:

The new char buffer
wrap
(char[]
The array that will back the new buffer
array
,
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.
offset
,
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.
length
)

Wraps a char array into a buffer.

public static CharBuffer

Returns:

The new char buffer
wrap
(char[]
The array that will back this buffer
array
)

Wraps a char array into a buffer.

public static CharBuffer

Returns:

The new character buffer
wrap
(CharSequence
The character sequence from which the new character buffer is to be created
csq
,
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.
start
,
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.
end
)

Wraps a character sequence into a buffer.

public static CharBuffer

Returns:

The new character buffer
wrap
(CharSequence
The character sequence from which the new character buffer is to be created
csq
)

Wraps a character sequence into a buffer.

Inherited from java.nio.Buffer:
capacitycheckIndexcheckIndexcheckSessioncreateCapacityExceptioncreateSameBufferExceptiondiscardMarkhasRemainingisReadOnlylimitmarkValuenextGetIndexnextGetIndexnextPutIndexnextPutIndexpositionremainingsession