Class CharChunk
java.lang.Object
org.apache.tomcat.util.buf.AbstractChunk
org.apache.tomcat.util.buf.CharChunk
- All Implemented Interfaces:
Serializable, CharSequence, Cloneable
Utilities to manipulate char chunks. While String is the easiest way to manipulate chars ( search, substrings, etc),
it is known to not be the most efficient solution - Strings are designed as immutable and secure objects.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInput interface, used when the buffer is empty.static interfaceWhen we need more space we'll either grow the buffer ( up to the limit ) or send it to a channel. -
Field Summary
Fields inherited from class AbstractChunk
ARRAY_MAX_SIZE, end, hasHashCode, isSet, sm, start -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidallocate(int initial, int limit) Allocates a buffer of the specified size.voidappend(char c) Appends a single character to the buffer.voidappend(char[] src, int off, int len) Add data to the buffer.voidAppend a string to the buffer.voidAppend a string to the buffer.voidAppends the contents of the source CharChunk to this buffer.charcharAt(int index) clone()booleanChecks if the buffer ends with the specified string.booleanequals(char[] b2, int off2, int len2) Compares this CharChunk to a character array.booleanbooleanCompares the message bytes to the specified String object.booleanCompares this CharChunk to another CharChunk.booleanCompares the message bytes to the specified String object.voidSend the buffer to the sink.char[]Returns the underlying character buffer.protected intgetBufferElement(int index) Returns the element at the specified index in the buffer.char[]getChars()Returns the character buffer.intindexOf(char c) Returns the index of the first occurrence of the given character.static intindexOf(char[] chars, int start, int end, char s) Returns the first instance of the given character in the given char array between the specified start and end.intindexOf(char c, int starting) Returns the first instance of the given character in this CharChunk starting at the specified char.intlength()voidmakeSpace(int count) Make space for len chars.voidWhen the buffer is empty, read the data from the input channel.voidWhen the buffer is full, write the data to the output channel.voidsetChars(char[] c, int off, int len) Sets the buffer to the specified sub array of characters.booleanstartsWith(String s) Checks if the buffer starts with the specified string.booleanstartsWithIgnoreCase(String s, int pos) Returns true if the buffer starts with the specified string.subSequence(int start, int end) intsubtract()Subtracts a single character from the buffer.intsubtract(char[] dest, int off, int len) Subtracts characters from the buffer into the destination array.toString()Returns the string representation of the chunk contents.Methods inherited from class AbstractChunk
getEnd, getLength, getLimit, getLimitInternal, getOffset, getStart, hash, hashCode, indexOf, isNull, recycle, setEnd, setLimit, setOffset, setStartMethods inherited from interface CharSequence
chars, codePoints, getChars, isEmpty
-
Constructor Details
-
CharChunk
public CharChunk()Creates a new, uninitialized CharChunk object. -
CharChunk
public CharChunk(int initial) Creates a new CharChunk with the specified initial buffer size.- Parameters:
initial- the initial buffer size
-
-
Method Details
-
clone
- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-
allocate
public void allocate(int initial, int limit) Allocates a buffer of the specified size.- Parameters:
initial- the initial buffer sizelimit- the maximum buffer size
-
setChars
public void setChars(char[] c, int off, int len) Sets the buffer to the specified sub array of characters.- Parameters:
c- the charactersoff- the start offset of the characterslen- the length of the characters
-
getChars
public char[] getChars()Returns the character buffer.- Returns:
- the character buffer
-
getBuffer
public char[] getBuffer()Returns the underlying character buffer.- Returns:
- the underlying character buffer
-
setCharInputChannel
When the buffer is empty, read the data from the input channel.- Parameters:
in- The input channel
-
setCharOutputChannel
When the buffer is full, write the data to the output channel. Also used when large amount of data is appended. If not set, the buffer will grow to the limit.- Parameters:
out- The output channel
-
append
Appends a single character to the buffer.- Parameters:
c- the character to append- Throws:
IOException- Writing overflow data to the output channel failed
-
append
Appends the contents of the source CharChunk to this buffer.- Parameters:
src- the source CharChunk- Throws:
IOException- Writing overflow data to the output channel failed
-
append
Add data to the buffer.- Parameters:
src- Char arrayoff- Offsetlen- Length- Throws:
IOException- Writing overflow data to the output channel failed
-
append
Append a string to the buffer.- Parameters:
s- The string- Throws:
IOException- Writing overflow data to the output channel failed
-
append
Append a string to the buffer.- Parameters:
s- The stringoff- Offsetlen- Length- Throws:
IOException- Writing overflow data to the output channel failed
-
subtract
Subtracts a single character from the buffer.- Returns:
- the character or -1 if end of stream
- Throws:
IOException- If an I/O error occurs
-
subtract
Subtracts characters from the buffer into the destination array.- Parameters:
dest- the destination arrayoff- the offset in the destination arraylen- the maximum number of characters to read- Returns:
- the number of characters read or -1 if end of stream
- Throws:
IOException- If an I/O error occurs
-
flushBuffer
Send the buffer to the sink. Called by append() when the limit is reached. You can also call it explicitly to force the data to be written.- Throws:
IOException- Writing overflow data to the output channel failed
-
makeSpace
public void makeSpace(int count) Make space for len chars. If len is small, allocate a reserve space too. Never grow bigger than the limit orAbstractChunk.ARRAY_MAX_SIZE.- Parameters:
count- The size
-
toString
- Specified by:
toStringin interfaceCharSequence- Overrides:
toStringin classObject
-
toStringInternal
Returns the string representation of the chunk contents.- Returns:
- the string representation
-
equals
Description copied from class:AbstractChunkSub-classes must provide an equals implementation that is consistent with
AbstractChunk.hashCode().- Specified by:
equalsin classAbstractChunk
-
equals
Compares the message bytes to the specified String object.- Parameters:
s- the String to compare- Returns:
trueif the comparison succeeded,falseotherwise
-
equalsIgnoreCase
Compares the message bytes to the specified String object.- Parameters:
s- the String to compare- Returns:
trueif the comparison succeeded,falseotherwise
-
equals
Compares this CharChunk to another CharChunk.- Parameters:
cc- the CharChunk to compare to- Returns:
- true if the CharChunks are equal
-
equals
public boolean equals(char[] b2, int off2, int len2) Compares this CharChunk to a character array.- Parameters:
b2- the character array to compare tooff2- the offset in the character arraylen2- the length of the character array- Returns:
- true if the contents are equal
-
startsWith
Checks if the buffer starts with the specified string.- Parameters:
s- the string to check- Returns:
- true if the buffer starts with the specified string
-
startsWithIgnoreCase
Returns true if the buffer starts with the specified string.- Parameters:
s- the stringpos- The position- Returns:
trueif the start matches
-
endsWith
Checks if the buffer ends with the specified string.- Parameters:
s- the string to check- Returns:
- true if the buffer ends with the specified string
-
getBufferElement
protected int getBufferElement(int index) Description copied from class:AbstractChunkReturns the element at the specified index in the buffer.- Specified by:
getBufferElementin classAbstractChunk- Parameters:
index- the element location in the buffer- Returns:
- the element
-
indexOf
public int indexOf(char c) Returns the index of the first occurrence of the given character.- Parameters:
c- the character to find- Returns:
- the index of the character, or -1 if not found
-
indexOf
public int indexOf(char c, int starting) Returns the first instance of the given character in this CharChunk starting at the specified char. If the character is not found, -1 is returned.- Parameters:
c- The characterstarting- The start position- Returns:
- The position of the first instance of the character or -1 if the character is not found.
-
indexOf
public static int indexOf(char[] chars, int start, int end, char s) Returns the first instance of the given character in the given char array between the specified start and end.- Parameters:
chars- The array to searchstart- The point to start searching from in the arrayend- The point to stop searching in the arrays- The character to search for- Returns:
- The position of the first instance of the character or -1 if the character is not found.
-
charAt
public char charAt(int index) - Specified by:
charAtin interfaceCharSequence
-
subSequence
- Specified by:
subSequencein interfaceCharSequence
-
length
public int length()- Specified by:
lengthin interfaceCharSequence
-