org.apache.tomcat.util.buf
Class ByteChunk
public final
class
ByteChunk
extends Object
implements Cloneable, Serializable
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
Method Summary |
void | allocate(int initial, int limit) |
void | append(char c) Append a char, by casting it to byte. |
void | append(byte b) |
void | append(ByteChunk src) |
void | append(byte[] src, int off, int len) Add data to the buffer |
static byte[] | convertToBytes(String value)
Convert specified String to a byte array. |
boolean | equals(String s)
Compares the message bytes to the specified String object. |
boolean | equals(ByteChunk bb) |
boolean | equals(byte[] b2, int off2, int len2) |
boolean | equals(CharChunk cc) |
boolean | equals(char[] c2, int off2, int len2) |
boolean | equalsIgnoreCase(String s)
Compares the message bytes to the specified String object. |
static int | findChar(byte[] buf, int start, int end, char c) Find a character, no side effects. |
static int | findChars(byte[] buf, int start, int end, byte[] c) Find a character, no side effects. |
static int | findNotChars(byte[] buf, int start, int end, byte[] c) Find the first character ! |
void | flushBuffer() Send the buffer to the sink. |
byte[] | getBuffer()
Returns the message bytes. |
byte[] | getBytes()
Returns the message bytes. |
ByteChunk | getClone() |
String | getEncoding() |
int | getEnd() |
int | getInt() |
int | getLength()
Returns the length of the bytes.
|
int | getLimit() |
long | getLong() |
int | getOffset() |
int | getStart()
Returns the start offset of the bytes.
|
int | hash() |
int | hashIgnoreCase() |
int | indexOf(String src, int srcOff, int srcLen, int myOff) |
int | indexOf(char c, int starting)
Returns true if the message bytes starts with the specified string. |
static int | indexOf(byte[] bytes, int off, int end, char qq) |
boolean | isNull() |
void | recycle()
Resets the message buff to an uninitialized state. |
void | reset() |
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 message bytes to the specified subarray of bytes.
|
void | setEncoding(String enc) |
void | setEnd(int i) |
void | setLimit(int limit) Maximum amount of data in this buffer.
|
void | setOffset(int off) |
void | setOptimizedWrite(boolean optimizedWrite) |
boolean | startsWith(String s)
Returns true if the message bytes starts with the specified string. |
boolean | startsWith(byte[] b2) |
boolean | startsWithIgnoreCase(String s, int pos)
Returns true if the message bytes starts with the specified string. |
int | substract() |
int | substract(ByteChunk src) |
int | substract(byte[] src, int off, int len) |
String | toString() |
String | toStringInternal() |
public static final String DEFAULT_CHARACTER_ENCODING
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.
public ByteChunk()
Creates a new, uninitialized ByteChunk object.
public ByteChunk(int initial)
public void allocate(int initial, int limit)
public void append(char c)
Append a char, by casting it to byte. This IS NOT intended for unicode.
Parameters: c
Throws: IOException
public void append(byte b)
public void append(byte[] src, int off, int len)
Add data to the buffer
public static final byte[] convertToBytes(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
public boolean equals(String s)
Compares the message bytes to the specified String object.
Parameters: s the String to compare
Returns: true if the comparison succeeded, false otherwise
public boolean equals(byte[] b2, int off2, int len2)
public boolean equals(char[] c2, int off2, int len2)
public boolean equalsIgnoreCase(String s)
Compares the message bytes to the specified String object.
Parameters: s the String to compare
Returns: true if the comparison succeeded, false otherwise
public static int findChar(byte[] buf, int start, int end, char c)
Find a character, no side effects.
Returns: index of char if found, -1 if not
public static int findChars(byte[] buf, int start, int end, byte[] c)
Find a character, no side effects.
Returns: index of char if found, -1 if not
public static int findNotChars(byte[] buf, int start, int end, byte[] c)
Find the first character != c
Returns: index of char if found, -1 if not
public void flushBuffer()
Send the buffer to the sink. Called by append() when the limit is reached.
You can also call it explicitely to force the data to be written.
Throws: IOException
public byte[] getBuffer()
Returns the message bytes.
public byte[] getBytes()
Returns the message bytes.
public String getEncoding()
public int getEnd()
public int getInt()
public int getLength()
Returns the length of the bytes.
XXX need to clean this up
public int getLimit()
public long getLong()
public int getOffset()
public int getStart()
Returns the start offset of the bytes.
For output this is the end of the buffer.
public int hash()
public int hashIgnoreCase()
public int indexOf(String src, int srcOff, int srcLen, int myOff)
public int indexOf(char c, int starting)
Returns true if the message bytes starts with the specified string.
Parameters: c the character starting The start position
public static int indexOf(byte[] bytes, int off, int end, char qq)
public boolean isNull()
public void recycle()
Resets the message buff to an uninitialized state.
public void reset()
When the buffer is empty, read the data from the input channel.
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.
public void setBytes(byte[] b, int off, int len)
Sets the message bytes to the specified subarray of bytes.
Parameters: b the ascii bytes off the start offset of the bytes len the length of the bytes
public void setEncoding(String enc)
public void setEnd(int i)
public void setLimit(int limit)
Maximum amount of data in this buffer.
If -1 or not set, the buffer will grow undefinitely.
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.
public void setOffset(int off)
public void setOptimizedWrite(boolean optimizedWrite)
public boolean startsWith(String s)
Returns true if the message bytes starts with the specified string.
Parameters: s the string
public boolean startsWith(byte[] b2)
public boolean startsWithIgnoreCase(String s, int pos)
Returns true if the message bytes starts with the specified string.
Parameters: s the string pos The position
public int substract()
public int substract(byte[] src, int off, int len)
public String toString()
public String toStringInternal()
Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.