Class CharBuffer
- All Implemented Interfaces:
Appendable,CharSequence,Comparable<CharBuffer>,Readable
public abstract class CharBuffer extends Buffer implements Comparable<CharBuffer>, CharSequence, Appendable, Readable
A char buffer can be created in either one of the following ways:
Allocatea new char array and create a buffer based on it;Wrapan existing char array to create a new buffer;Wrapan existing char sequence to create a new buffer;- Use
ByteBuffer.asCharBufferto create a char buffer based on a byte buffer.
-
Method Summary
Modifier and Type Method Description static CharBufferallocate(int capacity)Creates a char buffer based on a newly allocated char array.CharBufferappend(char c)Writes the given char to the current position and increases the position by 1.CharBufferappend(CharSequence csq)Writes all chars of the given character sequencecsqto the current position of this buffer, and increases the position by the length of the csq.CharBufferappend(CharSequence csq, int start, int end)Writes chars of the givenCharSequenceto the current position of this buffer, and increases the position by the number of chars written.char[]array()Returns the array that backs this buffer (optional operation).intarrayOffset()Returns the offset into the array returned byarrayof the first element of the buffer (optional operation).abstract CharBufferasReadOnlyBuffer()Returns a read-only buffer that shares its content with this buffer.charcharAt(int index)Returns the character located at the specified index in the buffer.abstract CharBuffercompact()Compacts this char buffer.intcompareTo(CharBuffer otherBuffer)Compare the remaining chars of this buffer to another char buffer's remaining chars.abstract CharBufferduplicate()Returns a duplicated buffer that shares its content with this buffer.booleanequals(Object other)Checks whether this char buffer is equal to another object.abstract charget()Returns the char at the current position and increases the position by 1.CharBufferget(char[] dst)Reads chars from the current position into the specified char array and increases the position by the number of chars read.CharBufferget(char[] dst, int dstOffset, int charCount)Reads chars from the current position into the specified char array, starting from the specified offset, and increases the position by the number of chars read.abstract charget(int index)Returns a char at the specified index; the position is not changed.booleanhasArray()Returns true ifarrayandarrayOffsetwon't throw.inthashCode()Calculates this buffer's hash code from the remaining chars.abstract booleanisDirect()Indicates whether this buffer is direct.intlength()Returns the number of remaining chars.abstract ByteOrderorder()Returns the byte order used by this buffer when converting chars from/to bytes.abstract CharBufferput(char c)Writes the given char to the current position and increases the position by 1.CharBufferput(char[] src)Writes chars from the given char array to the current position and increases the position by the number of chars written.CharBufferput(char[] src, int srcOffset, int charCount)Writes chars from the given char array, starting from the specified offset, to the current position and increases the position by the number of chars written.abstract CharBufferput(int index, char c)Writes a char to the specified index of this buffer; the position is not changed.CharBufferput(String str)Writes all chars of the given string to the current position of this buffer, and increases the position by the length of string.CharBufferput(String str, int start, int end)Writes chars of the given string to the current position of this buffer, and increases the position by the number of chars written.CharBufferput(CharBuffer src)Writes all the remaining chars of thesrcchar buffer to this buffer's current position, and increases both buffers' position by the number of chars copied.intread(CharBuffer target)Reads characters from this buffer and puts them intotarget.abstract CharBufferslice()Returns a sliced buffer that shares its content with this buffer.abstract CharBuffersubSequence(int start, int end)Returns a new char buffer representing a sub-sequence of this buffer's current remaining content.StringtoString()Returns a string representing the current remaining chars of this buffer.static CharBufferwrap(char[] array)Creates a new char buffer by wrapping the given char array.static CharBufferwrap(char[] array, int start, int charCount)Creates a new char buffer by wrapping the given char array.static CharBufferwrap(CharSequence chseq)Creates a new char buffer by wrapping the given char sequence.static CharBufferwrap(CharSequence cs, int start, int end)Creates a new char buffer by wrapping the given char sequence.
-
Method Details
-
allocate
Creates a char buffer based on a newly allocated char array.- Parameters:
capacity- the capacity of the new buffer.- Returns:
- the created char buffer.
- Throws:
IllegalArgumentException- ifcapacityis less than zero.
-
wrap
Creates a new char buffer by wrapping the given char array.Calling this method has the same effect as
wrap(array, 0, array.length).- Parameters:
array- the char array which the new buffer will be based on.- Returns:
- the created char buffer.
-
wrap
Creates a new char buffer by wrapping the given char array.The new buffer's position will be
start, limit will bestart + charCount, capacity will be the length of the array.- Parameters:
array- the char array which the new buffer will be based on.start- the start index, must not be negative and not greater thanarray.length.charCount- the length, must not be negative and not greater thanarray.length - start.- Returns:
- the created char buffer.
- Throws:
IndexOutOfBoundsException- if eitherstartorcharCountis invalid.
-
wrap
Creates a new char buffer by wrapping the given char sequence.Calling this method has the same effect as
wrap(chseq, 0, chseq.length()).- Parameters:
chseq- the char sequence which the new buffer will be based on.- Returns:
- the created char buffer.
-
wrap
Creates a new char buffer by wrapping the given char sequence.The new buffer's position will be
start, limit will beend, capacity will be the length of the char sequence. The new buffer is read-only.- Parameters:
cs- the char sequence which the new buffer will be based on.start- the start index, must not be negative and not greater thancs.length().end- the end index, must be no less thanstartand no greater thancs.length().- Returns:
- the created char buffer.
- Throws:
IndexOutOfBoundsException- if eitherstartorendis invalid.
-
array
public final char[] array()Description copied from class:BufferReturns the array that backs this buffer (optional operation). The returned value is the actual array, not a copy, so modifications to the array write through to the buffer.Subclasses should override this method with a covariant return type to provide the exact type of the array.
Use
hasArrayto ensure this method won't throw. (A separate call toisReadOnlyis not necessary.) -
arrayOffset
public final int arrayOffset()Description copied from class:BufferReturns the offset into the array returned byarrayof the first element of the buffer (optional operation). The backing array (if there is one) is not necessarily the same size as the buffer, and position 0 in the buffer is not necessarily the 0th element in the array. Usebuffer.array()[offset + buffer.arrayOffset()to access elementoffsetinbuffer.Use
hasArrayto ensure this method won't throw. (A separate call toisReadOnlyis not necessary.)- Specified by:
arrayOffsetin classBuffer- Returns:
- the offset
-
asReadOnlyBuffer
Returns a read-only buffer that shares its content with this buffer.The returned buffer is guaranteed to be a new instance, even if this buffer is read-only itself. The new buffer's position, limit, capacity and mark are the same as this buffer's.
The new buffer shares its content with this buffer, which means this buffer's change of content will be visible to the new buffer. The two buffer's position, limit and mark are independent.
- Returns:
- a read-only version of this buffer.
-
charAt
public final char charAt(int index)Returns the character located at the specified index in the buffer. The index value is referenced from the current buffer position.- Specified by:
charAtin interfaceCharSequence- Parameters:
index- the index referenced from the current buffer position. It must not be less than zero but less than the value obtained from a call toremaining().- Returns:
- the character located at the specified index (referenced from the current position) in the buffer.
- Throws:
IndexOutOfBoundsException- if the index is invalid.
-
compact
Compacts this char buffer.The remaining chars will be moved to the head of the buffer, starting from position zero. Then the position is set to
remaining(); the limit is set to capacity; the mark is cleared.- Returns:
- this buffer.
- Throws:
ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
compareTo
Compare the remaining chars of this buffer to another char buffer's remaining chars.- Specified by:
compareToin interfaceComparable<CharBuffer>- Parameters:
otherBuffer- another char buffer.- Returns:
- a negative value if this is less than
otherBuffer; 0 if this equals tootherBuffer; a positive value if this is greater thanotherBuffer. - Throws:
ClassCastException- ifotherBufferis not a char buffer.
-
duplicate
Returns a duplicated buffer that shares its content with this buffer.The duplicated buffer's initial position, limit, capacity and mark are the same as this buffer's. The duplicated buffer's read-only property and byte order are the same as this buffer's, too.
The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffers' position, limit and mark are independent.
-
equals
Checks whether this char buffer is equal to another object.If
otheris not a char buffer thenfalseis returned. Two char buffers are equal if and only if their remaining chars are exactly the same. Position, limit, capacity and mark are not considered.- Overrides:
equalsin classObject- Parameters:
other- the object to compare with this char buffer.- Returns:
trueif this char buffer is equal toother,falseotherwise.- See Also:
Object.hashCode()
-
get
public abstract char get()Returns the char at the current position and increases the position by 1.- Returns:
- the char at the current position.
- Throws:
BufferUnderflowException- if the position is equal or greater than limit.
-
get
Reads chars from the current position into the specified char array and increases the position by the number of chars read.Calling this method has the same effect as
get(dst, 0, dst.length).- Parameters:
dst- the destination char array.- Returns:
- this buffer.
- Throws:
BufferUnderflowException- ifdst.lengthis greater thanremaining().
-
get
Reads chars from the current position into the specified char array, starting from the specified offset, and increases the position by the number of chars read.- Parameters:
dst- the target char array.dstOffset- the offset of the char array, must not be negative and not greater thandst.length.charCount- The number of chars to read, must be no less than zero and no greater thandst.length - dstOffset.- Returns:
- this buffer.
- Throws:
IndexOutOfBoundsException- if eitherdstOffsetorcharCountis invalid.BufferUnderflowException- ifcharCountis greater thanremaining().
-
get
public abstract char get(int index)Returns a char at the specified index; the position is not changed.- Parameters:
index- the index, must not be negative and less than limit.- Returns:
- a char at the specified index.
- Throws:
IndexOutOfBoundsException- if index is invalid.
-
hasArray
public final boolean hasArray()Description copied from class:BufferReturns true ifarrayandarrayOffsetwon't throw. This method does not return true for buffers not backed by arrays because the other methods would throwUnsupportedOperationException, nor does it return true for buffers backed by read-only arrays, because the other methods would throwReadOnlyBufferException. -
hashCode
public int hashCode()Calculates this buffer's hash code from the remaining chars. The position, limit, capacity and mark don't affect the hash code.- Overrides:
hashCodein classObject- Returns:
- the hash code calculated from the remaining chars.
- See Also:
Object.equals(java.lang.Object)
-
isDirect
public abstract boolean isDirect()Indicates whether this buffer is direct. A direct buffer will try its best to take advantage of native memory APIs and it may not stay in the Java heap, so it is not affected by garbage collection.A char buffer is direct if it is based on a byte buffer and the byte buffer is direct.
-
length
public final int length()Returns the number of remaining chars.- Specified by:
lengthin interfaceCharSequence- Returns:
- the number of remaining chars.
-
order
Returns the byte order used by this buffer when converting chars from/to bytes.If this buffer is not based on a byte buffer, then this always returns the platform's native byte order.
- Returns:
- the byte order used by this buffer when converting chars from/to bytes.
-
put
Writes the given char to the current position and increases the position by 1.- Parameters:
c- the char to write.- Returns:
- this buffer.
- Throws:
BufferOverflowException- if position is equal or greater than limit.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes chars from the given char array to the current position and increases the position by the number of chars written.Calling this method has the same effect as
put(src, 0, src.length).- Parameters:
src- the source char array.- Returns:
- this buffer.
- Throws:
BufferOverflowException- ifremaining()is less thansrc.length.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes chars from the given char array, starting from the specified offset, to the current position and increases the position by the number of chars written.- Parameters:
src- the source char array.srcOffset- the offset of char array, must not be negative and not greater thansrc.length.charCount- the number of chars to write, must be no less than zero and no greater thansrc.length - srcOffset.- Returns:
- this buffer.
- Throws:
BufferOverflowException- ifremaining()is less thancharCount.IndexOutOfBoundsException- if eithersrcOffsetorcharCountis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes all the remaining chars of thesrcchar buffer to this buffer's current position, and increases both buffers' position by the number of chars copied.- Parameters:
src- the source char buffer.- Returns:
- this buffer.
- Throws:
BufferOverflowException- ifsrc.remaining()is greater than this buffer'sremaining().IllegalArgumentException- ifsrcis this buffer.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes a char to the specified index of this buffer; the position is not changed.- Parameters:
index- the index, must be no less than zero and less than the limit.c- the char to write.- Returns:
- this buffer.
- Throws:
IndexOutOfBoundsException- if index is invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes all chars of the given string to the current position of this buffer, and increases the position by the length of string.Calling this method has the same effect as
put(str, 0, str.length()).- Parameters:
str- the string to write.- Returns:
- this buffer.
- Throws:
BufferOverflowException- ifremaining()is less than the length of string.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
put
Writes chars of the given string to the current position of this buffer, and increases the position by the number of chars written.- Parameters:
str- the string to write.start- the first char to write, must not be negative and not greater thanstr.length().end- the last char to write (excluding), must be less thanstartand not greater thanstr.length().- Returns:
- this buffer.
- Throws:
BufferOverflowException- ifremaining()is less thanend - start.IndexOutOfBoundsException- if eitherstartorendis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
slice
Returns a sliced buffer that shares its content with this buffer.The sliced buffer's capacity will be this buffer's
remaining(), and its zero position will correspond to this buffer's current position. The new buffer's position will be 0, limit will be its capacity, and its mark is cleared. The new buffer's read-only property and byte order are same as this buffer.The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffers' position, limit and mark are independent.
-
subSequence
Returns a new char buffer representing a sub-sequence of this buffer's current remaining content.The new buffer's position will be
position() + start, limit will beposition() + end, capacity will be the same as this buffer. The new buffer's read-only property and byte order are the same as this buffer.The new buffer shares its content with this buffer, which means either buffer's change of content will be visible to the other. The two buffers' position, limit and mark are independent.
- Specified by:
subSequencein interfaceCharSequence- Parameters:
start- the start index of the sub-sequence, referenced from the current buffer position. Must not be less than zero and not greater than the value obtained from a call toremaining().end- the end index of the sub-sequence, referenced from the current buffer position. Must not be less thanstartand not be greater than the value obtained from a call toremaining().- Returns:
- a new char buffer represents a sub-sequence of this buffer's current remaining content.
- Throws:
IndexOutOfBoundsException- if eitherstartorendis invalid.
-
toString
Returns a string representing the current remaining chars of this buffer.- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classBuffer- Returns:
- a printable representation of this object.
-
append
Writes the given char to the current position and increases the position by 1.- Specified by:
appendin interfaceAppendable- Parameters:
c- the char to write.- Returns:
- this buffer.
- Throws:
BufferOverflowException- if position is equal or greater than limit.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
append
Writes all chars of the given character sequencecsqto the current position of this buffer, and increases the position by the length of the csq.Calling this method has the same effect as
append(csq.toString()). If theCharSequenceisnullthe string "null" will be written to the buffer.- Specified by:
appendin interfaceAppendable- Parameters:
csq- theCharSequenceto write.- Returns:
- this buffer.
- Throws:
BufferOverflowException- ifremaining()is less than the length of csq.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
append
Writes chars of the givenCharSequenceto the current position of this buffer, and increases the position by the number of chars written.- Specified by:
appendin interfaceAppendable- Parameters:
csq- theCharSequenceto write.start- the first char to write, must not be negative and not greater thancsq.length().end- the last char to write (excluding), must be less thanstartand not greater thancsq.length().- Returns:
- this buffer.
- Throws:
BufferOverflowException- ifremaining()is less thanend - start.IndexOutOfBoundsException- if eitherstartorendis invalid.ReadOnlyBufferException- if no changes may be made to the contents of this buffer.
-
read
Reads characters from this buffer and puts them intotarget. The number of chars that are copied is either the number of remaining chars in this buffer or the number of remaining chars intarget, whichever is smaller.- Specified by:
readin interfaceReadable- Parameters:
target- the target char buffer.- Returns:
- the number of chars copied or -1 if there are no chars left to be read from this buffer.
- Throws:
IllegalArgumentException- iftargetis this buffer.IOException- if an I/O error occurs.ReadOnlyBufferException- if no changes may be made to the contents oftarget.
-