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.
- Author:
- dac@sun.com, James Todd [gonzo@sun.com], Costin Manolache, Remy Maucherat
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInput interface, used when the buffer is emptystatic interfaceSame as java.nio.channel.WrittableByteChannel. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CharsetDefault encoding used to convert to strings. -
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 buffervoidappend(char c) Deprecated.Unused.voidstatic final 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) booleanbooleanCompares the message bytes to the specified String object.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.static intfindNotBytes(byte[] bytes, int start, int end, byte[] b) Deprecated.Unused.voidSend the buffer to the sink.byte[]Returns the message bytes.byte[]getBytes()Returns the message bytes.getClone()Deprecated.Unused.intgetEnd()intgetInt()Deprecated.Unused.intReturns the length of the bytes.intgetLimit()longgetLong()intintgetStart()Returns the start offset of the bytes.inthash()intDeprecated.Unused.static intindexOf(byte[] bytes, int start, int end, char c) 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.intbooleanisNull()voidrecycle()Resets the message buff to an uninitialized state.voidreset()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 message bytes to the specified subarray of bytes.voidsetCharset(Charset charset) voidsetEnd(int i) voidsetLimit(int limit) Maximum amount of data in this buffer.voidsetOffset(int off) voidsetOptimizedWrite(boolean optimizedWrite) Deprecated.Unused.booleanstartsWith(byte[] b2) Deprecated.Unused.booleanstartsWith(String s) Deprecated.Unused.booleanstartsWithIgnoreCase(String s, int pos) Returns true if the message bytes starts with the specified string.intintsubstract(byte[] src, int off, int len) intDeprecated.Unused.toString()
-
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
-
getClone
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards. -
isNull
public boolean isNull() -
recycle
public void recycle()Resets the message buff to an uninitialized state. -
reset
public void reset() -
allocate
public void allocate(int initial, int limit) -
setBytes
public void setBytes(byte[] b, int off, int len) Sets the message bytes to the specified subarray of bytes.- Parameters:
b- the ascii bytesoff- the start offset of the byteslen- the length of the bytes
-
setOptimizedWrite
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards. -
setCharset
-
getCharset
-
getBytes
public byte[] getBytes()Returns the message bytes. -
getBuffer
public byte[] getBuffer()Returns the message bytes. -
getStart
public int getStart()Returns the start offset of the bytes. For output this is the end of the buffer. -
getOffset
public int getOffset() -
setOffset
public void setOffset(int off) -
getLength
public int getLength()Returns the length of the bytes. XXX need to clean this up -
setLimit
public void setLimit(int limit) Maximum amount of data in this buffer.If -1 or not set, the buffer will grow indefinitely. Can be smaller than the current buffer size ( which will not shrink ). When the limit is reached, the buffer will be flushed ( if out is set ) or throw exception.
-
getLimit
public int getLimit() -
setByteInputChannel
When the buffer is empty, read the data from 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.
-
getEnd
public int getEnd() -
setEnd
public void setEnd(int i) -
append
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards.Append a char, by casting it to byte. This IS NOT intended for unicode.- Parameters:
c-- Throws:
IOException
-
append
- Throws:
IOException
-
append
- Throws:
IOException
-
append
Add data to the buffer- Throws:
IOException
-
substract
- Throws:
IOException
-
substract
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards.- Throws:
IOException
-
substract
- Throws:
IOException
-
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
-
toString
-
toStringInternal
-
getInt
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards. -
getLong
public long getLong() -
equals
Compares the message bytes to the specified String object.- Parameters:
s- the String to compare- Returns:
- true if the comparison succeeded, false otherwise
-
equalsIgnoreCase
Compares the message bytes to the specified String object.- Parameters:
s- the String to compare- Returns:
- true if the comparison succeeded, false otherwise
-
equals
-
equals
public boolean equals(byte[] b2, int off2, int len2) -
equals
public boolean equals(char[] c2, int off2, int len2) -
startsWith
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards.Returns true if the message bytes starts with the specified string.- Parameters:
s- the string
-
startsWith
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards.Returns true if the message bytes start with the specified byte array. -
startsWithIgnoreCase
Returns true if the message bytes starts with the specified string.- Parameters:
s- the stringpos- The position
-
indexOf
-
hash
public int hash() -
hashIgnoreCase
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards. -
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 c) 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 byte array to searchstart- The point to start searching from in the byte arrayend- The point to stop searching in the byte arrayc- 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.
-
findNotBytes
Deprecated.Unused. Will be removed in Tomcat 8.0.x onwards.Returns the first instance of any byte that is not one 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 list of bytes to search for- Returns:
- The position of the first instance a byte that is not in the list of bytes to search for or -1 if no such byte is 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
-