public final class ByteChunk extends AbstractChunk
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.
| Modifier and Type | Class and Description |
|---|---|
static class |
ByteChunk.BufferOverflowException |
static interface |
ByteChunk.ByteInputChannel
Input interface, used when the buffer is empty.
|
static interface |
ByteChunk.ByteOutputChannel
When we need more space we'll either grow the buffer ( up to the limit ) or send it to a channel.
|
| Modifier and Type | Field and Description |
|---|---|
static java.nio.charset.Charset |
DEFAULT_CHARSET
Default encoding used to convert to strings.
|
ARRAY_MAX_SIZE, end, hasHashCode, isSet, sm, start| Constructor and Description |
|---|
ByteChunk()
Creates a new, uninitialized ByteChunk object.
|
ByteChunk(int initial) |
| Modifier and Type | Method and Description |
|---|---|
void |
allocate(int initial,
int limit) |
void |
append(byte b) |
void |
append(byte[] src,
int off,
int len)
Add data to the buffer.
|
void |
append(java.nio.ByteBuffer from)
Add data to the buffer.
|
void |
append(ByteChunk src) |
java.lang.Object |
clone() |
static byte[] |
convertToBytes(java.lang.String value)
Convert specified String to a byte array.
|
boolean |
equals(byte[] b2,
int off2,
int len2) |
boolean |
equals(ByteChunk bb) |
boolean |
equals(char[] c2,
int off2,
int len2)
Compares the message bytes to the specified char array.
|
boolean |
equals(CharChunk cc) |
boolean |
equals(java.lang.Object obj) |
boolean |
equals(java.lang.String s)
Compares the message bytes to the specified String object.
|
boolean |
equalsIgnoreCase(byte[] b2,
int off2,
int len2) |
boolean |
equalsIgnoreCase(java.lang.String s)
Compares the message bytes to the specified String object.
|
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.
|
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.
|
void |
flushBuffer()
Send the buffer to the sink.
|
byte[] |
getBuffer() |
protected int |
getBufferElement(int index) |
byte[] |
getBytes() |
java.nio.charset.Charset |
getCharset() |
long |
getLong() |
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.
|
int |
indexOf(char c,
int starting)
Returns the first instance of the given character in this ByteChunk starting at the specified byte.
|
void |
makeSpace(int count)
Make space for len bytes.
|
void |
recycle()
Resets the chunk to an uninitialized state.
|
void |
setByteInputChannel(ByteChunk.ByteInputChannel in)
When the buffer is empty, read the data from the input channel.
|
void |
setByteOutputChannel(ByteChunk.ByteOutputChannel out)
When the buffer is full, write the data to the output channel.
|
void |
setBytes(byte[] b,
int off,
int len)
Sets the buffer to the specified subarray of bytes.
|
void |
setCharset(java.nio.charset.Charset charset) |
boolean |
startsWith(java.lang.String s,
int pos)
Returns true if the buffer starts with the specified string when tested in a case sensitive manner.
|
boolean |
startsWithIgnoreCase(java.lang.String s,
int pos)
Returns true if the buffer starts with the specified string when tested in a case insensitive manner.
|
int |
substract()
Deprecated.
|
int |
substract(byte[] dest,
int off,
int len)
Deprecated.
|
int |
substract(java.nio.ByteBuffer to)
Deprecated.
Use
subtract(ByteBuffer). This method will be removed in Tomcat 10 |
byte |
substractB()
Deprecated.
|
int |
subtract() |
int |
subtract(byte[] dest,
int off,
int len) |
int |
subtract(java.nio.ByteBuffer to)
Transfers bytes from the buffer to the specified ByteBuffer.
|
byte |
subtractB() |
java.lang.String |
toString() |
java.lang.String |
toString(java.nio.charset.CodingErrorAction malformedInputAction,
java.nio.charset.CodingErrorAction unmappableCharacterAction) |
java.lang.String |
toStringInternal()
Deprecated.
Unused. This method will be removed in Tomcat 11 onwards.
|
java.lang.String |
toStringInternal(java.nio.charset.CodingErrorAction malformedInputAction,
java.nio.charset.CodingErrorAction unmappableCharacterAction)
Converts the current content of the byte buffer to a String using the configured character set.
|
public static final java.nio.charset.Charset DEFAULT_CHARSET
public ByteChunk()
public ByteChunk(int initial)
public java.lang.Object clone()
throws java.lang.CloneNotSupportedException
clone in class java.lang.Objectjava.lang.CloneNotSupportedExceptionpublic void recycle()
AbstractChunkrecycle in class AbstractChunkpublic void allocate(int initial,
int limit)
public void setBytes(byte[] b,
int off,
int len)
b - the ascii bytesoff - the start offset of the byteslen - the length of the bytespublic void setCharset(java.nio.charset.Charset charset)
public java.nio.charset.Charset getCharset()
public byte[] getBytes()
public byte[] getBuffer()
public void setByteInputChannel(ByteChunk.ByteInputChannel in)
in - The input channelpublic void setByteOutputChannel(ByteChunk.ByteOutputChannel out)
out - The output channelpublic void append(byte b)
throws java.io.IOException
java.io.IOExceptionpublic void append(ByteChunk src) throws java.io.IOException
java.io.IOExceptionpublic void append(byte[] src,
int off,
int len)
throws java.io.IOException
src - Bytes arrayoff - Offsetlen - Lengthjava.io.IOException - Writing overflow data to the output channel failedpublic void append(java.nio.ByteBuffer from)
throws java.io.IOException
from - the ByteBuffer with the datajava.io.IOException - Writing overflow data to the output channel failed@Deprecated
public int substract()
throws java.io.IOException
java.io.IOExceptionpublic int subtract()
throws java.io.IOException
java.io.IOException@Deprecated
public byte substractB()
throws java.io.IOException
java.io.IOExceptionpublic byte subtractB()
throws java.io.IOException
java.io.IOException@Deprecated
public int substract(byte[] dest,
int off,
int len)
throws java.io.IOException
java.io.IOExceptionpublic int subtract(byte[] dest,
int off,
int len)
throws java.io.IOException
java.io.IOException@Deprecated
public int substract(java.nio.ByteBuffer to)
throws java.io.IOException
subtract(ByteBuffer). This method will be removed in Tomcat 10to - the ByteBuffer into which bytes are to be written.java.io.IOException - if an input or output exception has occurredpublic int subtract(java.nio.ByteBuffer to)
throws java.io.IOException
to - the ByteBuffer into which bytes are to be written.java.io.IOException - if an input or output exception has occurredpublic void flushBuffer()
throws java.io.IOException
java.io.IOException - Writing overflow data to the output channel failedpublic void makeSpace(int count)
AbstractChunk.ARRAY_MAX_SIZE.count - The sizepublic java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.String toString(java.nio.charset.CodingErrorAction malformedInputAction,
java.nio.charset.CodingErrorAction unmappableCharacterAction)
throws java.nio.charset.CharacterCodingException
java.nio.charset.CharacterCodingException@Deprecated public java.lang.String toStringInternal()
public java.lang.String toStringInternal(java.nio.charset.CodingErrorAction malformedInputAction,
java.nio.charset.CodingErrorAction unmappableCharacterAction)
throws java.nio.charset.CharacterCodingException
malformedInputAction - Action to take if the input is malformedunmappableCharacterAction - Action to take if a byte sequence can't be mapped to a characterjava.nio.charset.CharacterCodingException - If an error occurs during the conversionpublic long getLong()
public boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic boolean equals(java.lang.String s)
NOTE: This only works for characters in the range 0-127.
s - the String to comparetrue if the comparison succeeded, false otherwisepublic boolean equalsIgnoreCase(java.lang.String s)
NOTE: This only works for characters in the range 0-127.
s - the String to comparetrue if the comparison succeeded, false otherwisepublic boolean equals(ByteChunk bb)
public boolean equals(byte[] b2,
int off2,
int len2)
public boolean equalsIgnoreCase(byte[] b2,
int off2,
int len2)
public boolean equals(CharChunk cc)
public boolean equals(char[] c2,
int off2,
int len2)
NOTE: This only works for characters in the range 0-127.
c2 - the array to compare tooff2 - offsetlen2 - lengthtrue if the comparison succeeded, false otherwisepublic boolean startsWith(java.lang.String s,
int pos)
NOTE: This only works for characters in the range 0-127.
s - the stringpos - The positiontrue if the start matchespublic boolean startsWithIgnoreCase(java.lang.String s,
int pos)
NOTE: This only works for characters in the range 0-127.
s - the stringpos - The positiontrue if the start matchesprotected int getBufferElement(int index)
getBufferElement in class AbstractChunkpublic int indexOf(char c,
int starting)
NOTE: This only works for characters in the range 0-127.
c - The characterstarting - The start positionpublic static int indexOf(byte[] bytes,
int start,
int end,
char s)
NOTE: This only works for characters in the range 0-127.
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 forpublic static int findByte(byte[] bytes,
int start,
int end,
byte b)
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 forpublic static int findBytes(byte[] bytes,
int start,
int end,
byte[] b)
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 forpublic static byte[] convertToBytes(java.lang.String value)
value - to convert to byte arrayCopyright © 2000-2024 Apache Software Foundation.
Apache Tomcat, Tomcat, Apache, the Apache Tomcat logo and the Apache logo are either registered trademarks or trademarks of the Apache Software Foundation.