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

Nested Class Summary
static interfaceByteChunk.ByteInputChannel
Input interface, used when the buffer is emptiy Same as java.nio.channel.ReadableByteChannel
static interfaceByteChunk.ByteOutputChannel
Same as java.nio.channel.WrittableByteChannel.
Field Summary
static StringDEFAULT_CHARACTER_ENCODING
Default encoding used to convert to strings.
Constructor Summary
ByteChunk()
Creates a new, uninitialized ByteChunk object.
ByteChunk(int initial)
Method Summary
voidallocate(int initial, int limit)
voidappend(char c)
Append a char, by casting it to byte.
voidappend(byte b)
voidappend(ByteChunk src)
voidappend(byte[] src, int off, int len)
Add data to the buffer
static byte[]convertToBytes(String value)
Convert specified String to a byte array.
booleanequals(String s)
Compares the message bytes to the specified String object.
booleanequals(ByteChunk bb)
booleanequals(byte[] b2, int off2, int len2)
booleanequals(CharChunk cc)
booleanequals(char[] c2, int off2, int len2)
booleanequalsIgnoreCase(String s)
Compares the message bytes to the specified String object.
static intfindChar(byte[] buf, int start, int end, char c)
Find a character, no side effects.
static intfindChars(byte[] buf, int start, int end, byte[] c)
Find a character, no side effects.
static intfindNotChars(byte[] buf, int start, int end, byte[] c)
Find the first character !
voidflushBuffer()
Send the buffer to the sink.
byte[]getBuffer()
Returns the message bytes.
byte[]getBytes()
Returns the message bytes.
ByteChunkgetClone()
StringgetEncoding()
intgetEnd()
intgetInt()
intgetLength()
Returns the length of the bytes.
intgetLimit()
longgetLong()
intgetOffset()
intgetStart()
Returns the start offset of the bytes.
inthash()
inthashIgnoreCase()
intindexOf(String src, int srcOff, int srcLen, int myOff)
intindexOf(char c, int starting)
Returns true if the message bytes starts with the specified string.
static intindexOf(byte[] bytes, int off, int end, char qq)
booleanisNull()
voidrecycle()
Resets the message buff to an uninitialized state.
voidreset()
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 message bytes to the specified subarray of bytes.
voidsetEncoding(String enc)
voidsetEnd(int i)
voidsetLimit(int limit)
Maximum amount of data in this buffer.
voidsetOffset(int off)
voidsetOptimizedWrite(boolean optimizedWrite)
booleanstartsWith(String s)
Returns true if the message bytes starts with the specified string.
booleanstartsWith(byte[] b2)
booleanstartsWithIgnoreCase(String s, int pos)
Returns true if the message bytes starts with the specified string.
intsubstract()
intsubstract(ByteChunk src)
intsubstract(byte[] src, int off, int len)
StringtoString()
StringtoStringInternal()

Field Detail

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 Detail

ByteChunk

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

ByteChunk

public ByteChunk(int initial)

Method Detail

allocate

public void allocate(int initial, int limit)

append

public void append(char c)
Append a char, by casting it to byte. This IS NOT intended for unicode.

Parameters: c

Throws: IOException

append

public void append(byte b)

append

public void append(ByteChunk src)

append

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

convertToBytes

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

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(ByteChunk bb)

equals

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

equals

public boolean equals(CharChunk cc)

equals

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

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(byte[] buf, int start, int end, char c)
Find a character, no side effects.

Returns: index of char if found, -1 if not

findChars

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

findNotChars

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

flushBuffer

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

getBuffer

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

getBytes

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

getClone

public ByteChunk getClone()

getEncoding

public String getEncoding()

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

indexOf

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

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 pos The position

substract

public int substract()

substract

public int substract(ByteChunk src)

substract

public int substract(byte[] src, int off, int len)

toString

public String toString()

toStringInternal

public String toStringInternal()
Copyright © 2000-2011 Apache Software Foundation. All Rights Reserved.