A buffer of chars.
Method from java.nio.CharBuffer Detail: |
public static CharBuffer allocate(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException();
}
return BufferFactory.newCharBuffer(capacity);
}
Creates a char buffer based on a newly allocated char array. |
public CharBuffer append(char c) {
return put(c);
}
Writes the given char to the current position and increases the position
by 1. |
public CharBuffer append(CharSequence csq) {
if (csq != null) {
return put(csq.toString());
}
return put("null"); //$NON-NLS-1$
}
Writes all chars of the given character sequence {@code csq} to the
current position of this buffer, and increases the position by the length
of the csq.
Calling this method has the same effect as {@code append(csq.toString())}.
If the {@code CharSequence} is {@code null} the string "null" will be
written to the buffer. |
public CharBuffer append(CharSequence csq,
int start,
int end) {
if (csq == null) {
csq = "null"; //$NON-NLS-1$
}
CharSequence cs = csq.subSequence(start, end);
if (cs.length() > 0) {
return put(cs.toString());
}
return this;
}
Writes chars of the given {@code CharSequence} to the current position of
this buffer, and increases the position by the number of chars written. |
public final char[] array() {
return protectedArray();
}
Returns the char array which this buffer is based on, if there is one. |
public final int arrayOffset() {
return protectedArrayOffset();
}
|
abstract public CharBuffer 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. |
public final char charAt(int index) {
if (index < 0 || index >= remaining()) {
throw new IndexOutOfBoundsException();
}
return get(position + index);
}
Returns the character located at the specified index in the buffer. The
index value is referenced from the current buffer position. |
abstract public CharBuffer 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
{@code remaining()}; the limit is set to capacity; the mark is cleared. |
public int compareTo(CharBuffer otherBuffer) {
int compareRemaining = (remaining() < otherBuffer.remaining()) ? remaining()
: otherBuffer.remaining();
int thisPos = position;
int otherPos = otherBuffer.position;
char thisByte, otherByte;
while (compareRemaining > 0) {
thisByte = get(thisPos);
otherByte = otherBuffer.get(otherPos);
if (thisByte != otherByte) {
return thisByte < otherByte ? -1 : 1;
}
thisPos++;
otherPos++;
compareRemaining--;
}
return remaining() - otherBuffer.remaining();
}
Compare the remaining chars of this buffer to another char
buffer's remaining chars. |
abstract public CharBuffer 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 buffer's
position, limit and mark are independent. |
public boolean equals(Object other) {
if (!(other instanceof CharBuffer)) {
return false;
}
CharBuffer otherBuffer = (CharBuffer) other;
if (remaining() != otherBuffer.remaining()) {
return false;
}
int myPosition = position;
int otherPosition = otherBuffer.position;
boolean equalSoFar = true;
while (equalSoFar && (myPosition < limit)) {
equalSoFar = get(myPosition++) == otherBuffer.get(otherPosition++);
}
return equalSoFar;
}
Checks whether this char buffer is equal to another object.
If {@code other} is not a char buffer then {@code false} is 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. |
abstract public char get()
Returns the char at the current position and increases the position by 1. |
public CharBuffer get(char[] dest) {
return get(dest, 0, dest.length);
}
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
{@code get(dest, 0, dest.length)}. |
abstract public char get(int index)
Returns a char at the specified index; the position is not changed. |
public CharBuffer get(char[] dest,
int off,
int len) {
int length = dest.length;
if ((off < 0) || (len < 0) || (long) off + (long) len > length) {
throw new IndexOutOfBoundsException();
}
if (len > remaining()) {
throw new BufferUnderflowException();
}
for (int i = off; i < off + len; i++) {
dest[i] = get();
}
return this;
}
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. |
public final boolean hasArray() {
return protectedHasArray();
}
Indicates whether this buffer is based on a char array and is read/write. |
public int hashCode() {
int myPosition = position;
int hash = 0;
while (myPosition < limit) {
hash = hash + get(myPosition++);
}
return hash;
}
Calculates this buffer's hash code from the remaining chars. The
position, limit, capacity and mark don't affect the hash code. |
abstract public boolean isDirect()
|
public final int length() {
return remaining();
}
Returns the number of remaining chars. |
abstract public ByteOrder 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. |
abstract char[] protectedArray()
Child class implements this method to realize {@code array()}. |
abstract int protectedArrayOffset()
Child class implements this method to realize {@code arrayOffset()}. |
abstract boolean protectedHasArray()
Child class implements this method to realize {@code hasArray()}. |
abstract public CharBuffer put(char c)
Writes the given char to the current position and increases the position
by 1. |
public final CharBuffer put(char[] src) {
return put(src, 0, src.length);
}
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
{@code put(src, 0, src.length)}. |
public CharBuffer put(CharBuffer src) {
if (src == this) {
throw new IllegalArgumentException();
}
if (src.remaining() > remaining()) {
throw new BufferOverflowException();
}
char[] contents = new char[src.remaining()];
src.get(contents);
put(contents);
return this;
}
Writes all the remaining chars of the {@code src} char buffer to this
buffer's current position, and increases both buffers' position by the
number of chars copied. |
public final CharBuffer put(String str) {
return put(str, 0, str.length());
}
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
{@code put(str, 0, str.length())}. |
abstract public CharBuffer put(int index,
char c)
Writes a char to the specified index of this buffer; the position is not
changed. |
public CharBuffer put(char[] src,
int off,
int len) {
int length = src.length;
if ((off < 0) || (len < 0) || (long) off + (long) len > length) {
throw new IndexOutOfBoundsException();
}
if (len > remaining()) {
throw new BufferOverflowException();
}
for (int i = off; i < off + len; i++) {
put(src[i]);
}
return this;
}
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. |
public CharBuffer put(String str,
int start,
int end) {
int length = str.length();
if (start < 0 || end < start || end > length) {
throw new IndexOutOfBoundsException();
}
if (end - start > remaining()) {
throw new BufferOverflowException();
}
for (int i = start; i < end; i++) {
put(str.charAt(i));
}
return this;
}
Writes chars of the given string to the current position of this buffer,
and increases the position by the number of chars written. |
public int read(CharBuffer target) throws IOException {
int remaining = remaining();
if (target == this) {
if (remaining == 0) {
return -1;
}
throw new IllegalArgumentException();
}
if (remaining == 0) {
return limit > 0 && target.remaining() == 0 ? 0 : -1;
}
remaining = Math.min(target.remaining(), remaining);
if (remaining > 0) {
char[] chars = new char[remaining];
get(chars);
target.put(chars);
}
return remaining;
}
Reads characters from this buffer and puts them into {@code target}. The
number of chars that are copied is either the number of remaining chars
in this buffer or the number of remaining chars in {@code target},
whichever is smaller. |
abstract public CharBuffer slice()
Returns a sliced buffer that shares its content with this buffer.
The sliced buffer's capacity will be this buffer's {@code 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 buffer's
position, limit and mark are independent. |
abstract public CharSequence subSequence(int start,
int end)
Returns a new char buffer representing a sub-sequence of this buffer's
current remaining content.
The new buffer's position will be {@code position() + start}, limit will
be {@code position() + 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 buffer's
position, limit and mark are independent. |
public String toString() {
StringBuilder strbuf = new StringBuilder();
for (int i = position; i < limit; i++) {
strbuf.append(get(i));
}
return strbuf.toString();
}
Returns a string representing the current remaining chars of this buffer. |
public static CharBuffer wrap(char[] array) {
return wrap(array, 0, array.length);
}
Creates a new char buffer by wrapping the given char array.
Calling this method has the same effect as
{@code wrap(array, 0, array.length)}. |
public static CharBuffer wrap(CharSequence chseq) {
return BufferFactory.newCharBuffer(chseq);
}
Creates a new char buffer by wrapping the given char sequence.
Calling this method has the same effect as
{@code wrap(chseq, 0, chseq.length())}. |
public static CharBuffer wrap(char[] array,
int start,
int len) {
int length = array.length;
if ((start < 0) || (len < 0) || (long) start + (long) len > length) {
throw new IndexOutOfBoundsException();
}
CharBuffer buf = BufferFactory.newCharBuffer(array);
buf.position = start;
buf.limit = start + len;
return buf;
}
Creates a new char buffer by wrapping the given char array.
The new buffer's position will be {@code start}, limit will be
{@code start + len}, capacity will be the length of the array. |
public static CharBuffer wrap(CharSequence chseq,
int start,
int end) {
if (chseq == null) {
throw new NullPointerException();
}
if (start < 0 || end < start || end > chseq.length()) {
throw new IndexOutOfBoundsException();
}
CharBuffer result = BufferFactory.newCharBuffer(chseq);
result.position = start;
result.limit = end;
return result;
}
Creates a new char buffer by wrapping the given char sequence.
The new buffer's position will be {@code start}, limit will be
{@code end}, capacity will be the length of the char sequence. The new
buffer is read-only. |