Package org.apache.tomcat.util.buf
Class ByteChunk
- java.lang.Object
- 
- org.apache.tomcat.util.buf.AbstractChunk
- 
- org.apache.tomcat.util.buf.ByteChunk
 
 
- 
- All Implemented Interfaces:
- java.io.Serializable,- java.lang.Cloneable
 
 public final class ByteChunk extends AbstractChunk This class is used to represent a chunk of bytes, and utilities to manipulate byte[]. 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:
- Serialized Form
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static classByteChunk.BufferOverflowExceptionstatic interfaceByteChunk.ByteInputChannelInput interface, used when the buffer is empty.static interfaceByteChunk.ByteOutputChannelWhen we need more space we'll either grow the buffer ( up to the limit ) or send it to a channel.
 - 
Field SummaryFields Modifier and Type Field Description static java.nio.charset.CharsetDEFAULT_CHARSETDefault encoding used to convert to strings.- 
Fields inherited from class org.apache.tomcat.util.buf.AbstractChunkARRAY_MAX_SIZE, end, hasHashCode, isSet, start
 
- 
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidallocate(int initial, int limit)voidappend(byte b)voidappend(byte[] src, int off, int len)Add data to the buffer.voidappend(java.nio.ByteBuffer from)Add data to the buffer.voidappend(ByteChunk src)java.lang.Objectclone()static byte[]convertToBytes(java.lang.String value)Convert specified String to a byte array.booleanequals(byte[] b2, int off2, int len2)booleanequals(char[] c2, int off2, int len2)booleanequals(java.lang.Object obj)booleanequals(java.lang.String s)Compares the message bytes to the specified String object.booleanequals(ByteChunk bb)booleanequals(CharChunk cc)booleanequalsIgnoreCase(java.lang.String s)Compares 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.voidflushBuffer()Send the buffer to the sink.byte[]getBuffer()protected intgetBufferElement(int index)byte[]getBytes()java.nio.charset.CharsetgetCharset()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.voidsetByteInputChannel(ByteChunk.ByteInputChannel in)When the buffer is empty, read the data from the input channel.voidsetByteOutputChannel(ByteChunk.ByteOutputChannel out)When 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(java.nio.charset.Charset charset)booleanstartsWith(java.lang.String s, int pos)Returns true if the buffer starts with the specified string when tested in a case sensitive manner.booleanstartsWithIgnoreCase(java.lang.String s, int pos)Returns true if the buffer starts with the specified string when tested in a case insensitive manner.intsubstract()intsubstract(byte[] dest, int off, int len)intsubstract(java.nio.ByteBuffer to)Transfers bytes from the buffer to the specified ByteBuffer.bytesubstractB()java.lang.StringtoString()java.lang.StringtoStringInternal()
 
- 
- 
- 
Method Detail- 
clonepublic java.lang.Object clone() throws java.lang.CloneNotSupportedException- Overrides:
- clonein class- java.lang.Object
- Throws:
- java.lang.CloneNotSupportedException
 
 - 
recyclepublic void recycle() Description copied from class:AbstractChunkResets the chunk to an uninitialized state.- Overrides:
- recyclein class- AbstractChunk
 
 - 
allocatepublic void allocate(int initial, int limit)
 - 
setBytespublic void setBytes(byte[] b, int off, int len)Sets the buffer to the specified subarray of bytes.- Parameters:
- b- the ascii bytes
- off- the start offset of the bytes
- len- the length of the bytes
 
 - 
setCharsetpublic void setCharset(java.nio.charset.Charset charset) 
 - 
getCharsetpublic java.nio.charset.Charset getCharset() 
 - 
getBytespublic byte[] getBytes() - Returns:
- the buffer.
 
 - 
getBufferpublic byte[] getBuffer() - Returns:
- the buffer.
 
 - 
setByteInputChannelpublic void setByteInputChannel(ByteChunk.ByteInputChannel in) When the buffer is empty, read the data from the input channel.- Parameters:
- in- The input channel
 
 - 
setByteOutputChannelpublic void setByteOutputChannel(ByteChunk.ByteOutputChannel out) 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
 
 - 
appendpublic void append(byte b) throws java.io.IOException- Throws:
- java.io.IOException
 
 - 
appendpublic void append(ByteChunk src) throws java.io.IOException - Throws:
- java.io.IOException
 
 - 
appendpublic void append(byte[] src, int off, int len) throws java.io.IOExceptionAdd data to the buffer.- Parameters:
- src- Bytes array
- off- Offset
- len- Length
- Throws:
- java.io.IOException- Writing overflow data to the output channel failed
 
 - 
appendpublic void append(java.nio.ByteBuffer from) throws java.io.IOExceptionAdd data to the buffer.- Parameters:
- from- the ByteBuffer with the data
- Throws:
- java.io.IOException- Writing overflow data to the output channel failed
 
 - 
substractpublic int substract() throws java.io.IOException- Throws:
- java.io.IOException
 
 - 
substractBpublic byte substractB() throws java.io.IOException- Throws:
- java.io.IOException
 
 - 
substractpublic int substract(byte[] dest, int off, int len) throws java.io.IOException- Throws:
- java.io.IOException
 
 - 
substractpublic int substract(java.nio.ByteBuffer to) throws java.io.IOExceptionTransfers 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:
- java.io.IOException- if an input or output exception has occurred
 
 - 
flushBufferpublic void flushBuffer() throws java.io.IOExceptionSend 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:
- java.io.IOException- Writing overflow data to the output channel failed
 
 - 
makeSpacepublic 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
 
 - 
toStringpublic java.lang.String toString() - Overrides:
- toStringin class- java.lang.Object
 
 - 
toStringInternalpublic java.lang.String toStringInternal() 
 - 
getLongpublic long getLong() 
 - 
equalspublic boolean equals(java.lang.Object obj) - Overrides:
- equalsin class- java.lang.Object
 
 - 
equalspublic boolean equals(java.lang.String s) Compares the message bytes to the specified String object.- Parameters:
- s- the String to compare
- Returns:
- trueif the comparison succeeded,- falseotherwise
 
 - 
equalsIgnoreCasepublic boolean equalsIgnoreCase(java.lang.String s) Compares the message bytes to the specified String object.- Parameters:
- s- the String to compare
- Returns:
- trueif the comparison succeeded,- falseotherwise
 
 - 
equalspublic boolean equals(ByteChunk bb) 
 - 
equalspublic boolean equals(byte[] b2, int off2, int len2)
 - 
equalspublic boolean equals(CharChunk cc) 
 - 
equalspublic boolean equals(char[] c2, int off2, int len2)
 - 
startsWithpublic 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.- Parameters:
- s- the string
- pos- The position
- Returns:
- trueif the start matches
 
 - 
startsWithIgnoreCasepublic 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.- Parameters:
- s- the string
- pos- The position
- Returns:
- trueif the start matches
 
 - 
getBufferElementprotected int getBufferElement(int index) - Specified by:
- getBufferElementin class- AbstractChunk
 
 - 
indexOfpublic 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 character
- starting- The start position
- Returns:
- The position of the first instance of the character or -1 if the character is not found.
 
 - 
indexOfpublic 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 search
- start- The point to start searching from in the array
- end- The point to stop searching in the array
- s- The character to search for
- Returns:
- The position of the first instance of the character or -1 if the character is not found.
 
 - 
findBytepublic 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 search
- start- The point to start searching from in the byte array
- end- The point to stop searching in the byte array
- b- The byte to search for
- Returns:
- The position of the first instance of the byte or -1 if the byte is not found.
 
 - 
findBytespublic 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 search
- start- The point to start searching from in the byte array
- end- The point to stop searching in the byte array
- b- 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.
 
 - 
convertToBytespublic static final byte[] convertToBytes(java.lang.String value) 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
 
 
- 
 
-