org.apache.tomcat.util.buf

Class ByteChunk

Implemented Interfaces:
Cloneable, Serializable

public final class ByteChunk
extends java.lang.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.

Authors:
dac@sun.com
James Todd [gonzo@sun.com]
Costin Manolache
Remy Maucherat

Nested Class Summary

static interface
ByteChunk.ByteInputChannel
static interface
ByteChunk.ByteOutputChannel

Field Summary

static String
DEFAULT_CHARACTER_ENCODING
Default encoding used to convert to strings.

Constructor Summary

ByteChunk()
Creates a new, uninitialized ByteChunk object.
ByteChunk(int initial)

Method Summary

void
allocate(int initial, int limit)
void
append(byte b)
void
append(char c)
void
append(ByteChunk src)
void
append(src[] , int off, int len)
Add data to the buffer
boolean
equals(String s)
Compares the message bytes to the specified String object.
boolean
equals(b2[] , int off2, int len2)
boolean
equals(c2[] , int off2, int len2)
boolean
equals(ByteChunk bb)
boolean
equals(CharChunk cc)
boolean
equalsIgnoreCase(String s)
Compares the message bytes to the specified String object.
static int
findChar(buf[] , int start, int end, char c)
Find a character, no side effects.
static int
findChars(buf[] , int start, int end, c[] )
Find a character, no side effects.
static int
findNotChars(buf[] , int start, int end, c[] )
Find the first character != c
void
flushBuffer()
byte[]
getBuffer()
Returns the message bytes.
byte[]
getBytes()
Returns the message bytes.
ByteChunk
getClone()
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)
static int
indexOf(bytes[] , int off, int end, char qq)
int
indexOf(char c, int starting)
Returns true if the message bytes starts with the specified string.
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(src[] , int off, int len)
String
toString()

Field Details

DEFAULT_CHARACTER_ENCODING

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.

Constructor Details

ByteChunk

public ByteChunk()
Creates a new, uninitialized ByteChunk object.


ByteChunk

public ByteChunk(int initial)

Method Details

allocate

public void allocate(int initial,
                     int limit)


append

public void append(byte b)
            throws IOException


append

public void append(char c)
            throws IOException


append

public void append(ByteChunk src)
            throws IOException


append

public void append(src[] ,
                   int off,
                   int len)
            throws IOException
Add data to the buffer


equals

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


equals

public boolean equals(b2[] ,
                      int off2,
                      int len2)


equals

public boolean equals(c2[] ,
                      int off2,
                      int len2)


equals

public boolean equals(ByteChunk bb)


equals

public boolean equals(CharChunk cc)


equalsIgnoreCase

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


findChar

public static int findChar(buf[] ,
                           int start,
                           int end,
                           char c)
Find a character, no side effects.


findChars

public static int findChars(buf[] ,
                            int start,
                            int end,
                            c[] )
Find a character, no side effects.


findNotChars

public static int findNotChars(buf[] ,
                               int start,
                               int end,
                               c[] )
Find the first character != c


flushBuffer

public void flushBuffer()
            throws IOException


getBuffer

public byte[] getBuffer()
Returns the message bytes.


getBytes

public byte[] getBytes()
Returns the message bytes.


getClone

public ByteChunk getClone()


getEnd

public int getEnd()


getInt

public int getInt()


getLength

public int getLength()
Returns the length of the bytes. XXX need to clean this up


getLimit

public int getLimit()


getLong

public long getLong()


getOffset

public int getOffset()


getStart

public int getStart()
Returns the start offset of the bytes. For output this is the end of the buffer.


hash

public int hash()


hashIgnoreCase

public int hashIgnoreCase()


indexOf

public int indexOf(String src,
                   int srcOff,
                   int srcLen,
                   int myOff)


indexOf

public static int indexOf(bytes[] ,
                          int off,
                          int end,
                          char qq)


indexOf

public int indexOf(char c,
                   int starting)
Returns true if the message bytes starts with the specified string.

Parameters:


isNull

public boolean isNull()


recycle

public void recycle()
Resets the message buff to an uninitialized state.


reset

public void reset()


setByteInputChannel

public void setByteInputChannel(ByteChunk.ByteInputChannel in)
When the buffer is empty, read the data from the input channel.


setByteOutputChannel

public 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.


setBytes

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


setEncoding

public void setEncoding(String enc)


setEnd

public void setEnd(int i)


setLimit

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.


setOffset

public void setOffset(int off)


setOptimizedWrite

public void setOptimizedWrite(boolean optimizedWrite)


startsWith

public boolean startsWith(String s)
Returns true if the message bytes starts with the specified string.

Parameters:
s - the string


startsWith

public boolean startsWith(byte[] b2)


startsWithIgnoreCase

public boolean startsWithIgnoreCase(String s,
                                    int pos)
Returns true if the message bytes starts with the specified string.

Parameters:
s - the string


substract

public int substract()
            throws IOException


substract

public int substract(ByteChunk src)
            throws IOException


substract

public int substract(src[] ,
                     int off,
                     int len)
            throws IOException


toString

public String toString()


Copyright B) 2000-2003 Apache Software Foundation. All Rights Reserved.