Class ByteChunk
- All Implemented Interfaces:
Serializable,Cloneable
The buffer can be modified and used for both input and output.
There are 2 modes: The chunk can be associated with a sink - ByteInputChannel or ByteOutputChannel, which will be used when the buffer is empty (on input) or filled (on output). For output, it can also grow. This operating mode is selected by calling setLimit() or allocate(initial, limit) with limit != -1.
Various search and append method are defined - similar with String and StringBuffer, but operating on bytes.
This is important because it allows processing the http headers directly on the received bytes, without converting to chars and Strings until the strings are needed. In addition, the charset is determined later, from headers or user code.
In a server it is very important to be able to operate on the original byte[] without converting everything to chars. Some protocols are ASCII only, and some allow different non-UNICODE encodings. The encoding is not known beforehand, and can even change during the execution of the protocol. ( for example a multipart message may have parts with different encoding )
For HTTP it is not very clear how the encoding of RequestURI and mime values can be determined, but it is a great advantage to be able to parse the request without converting to string.
- Author:
- dac@sun.com, James Todd [gonzo@sun.com], Costin Manolache, Remy Maucherat
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic 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
FieldsModifier and TypeFieldDescriptionstatic final CharsetDefault encoding used to convert to strings.Fields inherited from class org.apache.tomcat.util.buf.AbstractChunk
ARRAY_MAX_SIZE, end, hasHashCode, isSet, sm, start -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidallocate(int initial, int limit) voidappend(byte b) voidappend(byte[] src, int off, int len) Add data to the buffer.voidappend(ByteBuffer from) Add data to the buffer.voidclone()static byte[]convertToBytes(String value) Convert specified String to a byte array.booleanequals(byte[] b2, int off2, int len2) booleanequals(char[] c2, int off2, int len2) Compares the message bytes to the specified char array.booleanbooleanCompares the message bytes to the specified String object.booleanbooleanbooleanequalsIgnoreCase(byte[] b2, int off2, int len2) booleanCompares the message bytes to the specified String object.static intfindByte(byte[] bytes, int start, int end, byte b) Returns the first instance of the given byte in the byte array between the specified start and end.static intfindBytes(byte[] bytes, int start, int end, byte[] b) Returns the first instance of any of the given bytes in the byte array between the specified start and end.voidSend the buffer to the sink.byte[]protected intgetBufferElement(int index) byte[]getBytes()longgetLong()static intindexOf(byte[] bytes, int start, int end, char s) Returns the first instance of the given character in the given byte array between the specified start and end.intindexOf(char c, int starting) Returns the first instance of the given character in this ByteChunk starting at the specified byte.voidmakeSpace(int count) Make space for len bytes.voidrecycle()Resets the chunk to an uninitialized state.voidWhen the buffer is empty, read the data from the input channel.voidWhen the buffer is full, write the data to the output channel.voidsetBytes(byte[] b, int off, int len) Sets the buffer to the specified subarray of bytes.voidsetCharset(Charset charset) booleanstartsWith(String s, int pos) Returns true if the buffer starts with the specified string when tested in a case sensitive manner.booleanstartsWithIgnoreCase(String s, int pos) Returns true if the buffer starts with the specified string when tested in a case insensitive manner.intDeprecated.intsubstract(byte[] dest, int off, int len) Deprecated.intsubstract(ByteBuffer to) Deprecated.byteDeprecated.intsubtract()intsubtract(byte[] dest, int off, int len) intsubtract(ByteBuffer to) Transfers bytes from the buffer to the specified ByteBuffer.bytetoString()toString(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) Deprecated.Unused.toStringInternal(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) Converts the current content of the byte buffer to a String using the configured character set.
-
Field Details
-
DEFAULT_CHARSET
Default encoding used to convert to strings. It should be UTF8, as most standards seem to converge, but the servlet API requires 8859_1, and this object is used mostly for servlets.
-
-
Constructor Details
-
ByteChunk
public ByteChunk()Creates a new, uninitialized ByteChunk object. -
ByteChunk
public ByteChunk(int initial)
-
-
Method Details
-
clone
- Overrides:
clonein classObject- Throws:
CloneNotSupportedException
-
recycle
public void recycle()Description copied from class:AbstractChunkResets the chunk to an uninitialized state.- Overrides:
recyclein classAbstractChunk
-
allocate
public void allocate(int initial, int limit) -
setBytes
public void setBytes(byte[] b, int off, int len) Sets the buffer to the specified subarray of bytes.- Parameters:
b- the ascii bytesoff- the start offset of the byteslen- the length of the bytes
-
setCharset
-
getCharset
-
getBytes
public byte[] getBytes()- Returns:
- the buffer.
-
getBuffer
public byte[] getBuffer()- Returns:
- the buffer.
-
setByteInputChannel
When the buffer is empty, read the data from the input channel.- Parameters:
in- The input channel
-
setByteOutputChannel
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
- Throws:
IOException
-
append
- Throws:
IOException
-
append
Add data to the buffer.- Parameters:
src- Bytes arrayoff- Offsetlen- Length- Throws:
IOException- Writing overflow data to the output channel failed
-
append
Add data to the buffer.- Parameters:
from- the ByteBuffer with the data- Throws:
IOException- Writing overflow data to the output channel failed
-
substract
Deprecated.- Throws:
IOException
-
subtract
- Throws:
IOException
-
substractB
Deprecated.- Throws:
IOException
-
subtractB
- Throws:
IOException
-
substract
Deprecated.- Throws:
IOException
-
subtract
- Throws:
IOException
-
substract
Deprecated.Usesubtract(ByteBuffer). This method will be removed in Tomcat 10Transfers bytes from the buffer to the specified ByteBuffer. After the operation the position of the ByteBuffer will be returned to the one before the operation, the limit will be the position incremented by the number of the transferred bytes.- Parameters:
to- the ByteBuffer into which bytes are to be written.- Returns:
- an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached
- Throws:
IOException- if an input or output exception has occurred
-
subtract
Transfers bytes from the buffer to the specified ByteBuffer. After the operation the position of the ByteBuffer will be returned to the one before the operation, the limit will be the position incremented by the number of the transfered bytes.- Parameters:
to- the ByteBuffer into which bytes are to be written.- Returns:
- an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached
- Throws:
IOException- if an input or output exception has occurred
-
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 bytes. If len is small, allocate a reserve space too. Never grow bigger than the limit orAbstractChunk.ARRAY_MAX_SIZE.- Parameters:
count- The size
-
toString
-
toString
public String toString(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) throws CharacterCodingException - Throws:
CharacterCodingException
-
toStringInternal
Deprecated.Unused. This method will be removed in Tomcat 11 onwards.Converts the current content of the byte buffer to a String using the configured character set.- Returns:
- The result of converting the bytes to a String
-
toStringInternal
public String toStringInternal(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) throws CharacterCodingException Converts the current content of the byte buffer to a String using the configured character set.- Parameters:
malformedInputAction- Action to take if the input is malformedunmappableCharacterAction- Action to take if a byte sequence can't be mapped to a character- Returns:
- The result of converting the bytes to a String
- Throws:
CharacterCodingException- If an error occurs during the conversion
-
getLong
public long getLong() -
equals
-
equals
Compares the message bytes to the specified String object.NOTE: This only works for characters in the range 0-127.
- Parameters:
s- the String to compare- Returns:
trueif the comparison succeeded,falseotherwise
-
equalsIgnoreCase
Compares the message bytes to the specified String object.NOTE: This only works for characters in the range 0-127.
- Parameters:
s- the String to compare- Returns:
trueif the comparison succeeded,falseotherwise
-
equals
-
equals
public boolean equals(byte[] b2, int off2, int len2) -
equalsIgnoreCase
public boolean equalsIgnoreCase(byte[] b2, int off2, int len2) -
equals
-
equals
public boolean equals(char[] c2, int off2, int len2) Compares the message bytes to the specified char array.NOTE: This only works for characters in the range 0-127.
- Parameters:
c2- the array to compare tooff2- offsetlen2- length- Returns:
trueif the comparison succeeded,falseotherwise
-
startsWith
Returns true if the buffer starts with the specified string when tested in a case sensitive manner.NOTE: This only works for characters in the range 0-127.
- Parameters:
s- the stringpos- The position- Returns:
trueif the start matches
-
startsWithIgnoreCase
Returns true if the buffer starts with the specified string when tested in a case insensitive manner.NOTE: This only works for characters in the range 0-127.
- Parameters:
s- the stringpos- The position- Returns:
trueif the start matches
-
getBufferElement
protected int getBufferElement(int index) - Specified by:
getBufferElementin classAbstractChunk
-
indexOf
public int indexOf(char c, int starting) Returns the first instance of the given character in this ByteChunk starting at the specified byte. If the character is not found, -1 is returned.NOTE: This only works for characters in the range 0-127.
- 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(byte[] bytes, int start, int end, char s) Returns the first instance of the given character in the given byte array between the specified start and end.NOTE: This only works for characters in the range 0-127.
- Parameters:
bytes- 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.
-
findByte
public static int findByte(byte[] bytes, int start, int end, byte b) Returns the first instance of the given byte in the byte array between the specified start and end.- Parameters:
bytes- The byte array to searchstart- The point to start searching from in the byte arrayend- The point to stop searching in the byte arrayb- The byte to search for- Returns:
- The position of the first instance of the byte or -1 if the byte is not found.
-
findBytes
public static int findBytes(byte[] bytes, int start, int end, byte[] b) Returns the first instance of any of the given bytes in the byte array between the specified start and end.- Parameters:
bytes- The byte array to searchstart- The point to start searching from in the byte arrayend- The point to stop searching in the byte arrayb- The array of bytes to search for- Returns:
- The position of the first instance of the byte or -1 if the byte is not found.
-
convertToBytes
Convert specified String to a byte array. This ONLY WORKS for ascii, UTF chars will be truncated.- Parameters:
value- to convert to byte array- Returns:
- the byte array value
-
subtract(ByteBuffer).