Top Description Inners Fields Constructors Methods
org.apache.tomcat.util.buf

public final Class ByteChunk

extends AbstractChunk
Class Inheritance
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.CharacterCodingException, .Charset, .CodingErrorAction, .StandardCharsets

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.

In a server it is very important to be able to operate on the original byte[] without converting everything to chars. Some protocols are ASCII only, and some allow different non-UNICODE encodings. The encoding is not known beforehand, and can even change during the execution of the protocol. ( for example a multipart message may have parts with different encoding )

For HTTP it is not very clear how the encoding of RequestURI and mime values can be determined, but it is a great advantage to be able to parse the request without converting to string.

Authors
[email protected], James Todd [[email protected]], Costin Manolache, Remy Maucherat

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static interface
ByteChunk.ByteInputChannel

Input interface, used when the buffer is empty.

public static interface
ByteChunk.ByteOutputChannel

When we need more space we'll either grow the buffer ( up to the limit ) or send it to a channel.

Field Summary

Modifier and TypeField and Description
private byte[]
private transient Charset
public static final Charset
DEFAULT_CHARSET

Default encoding used to convert to strings.

private transient ByteChunk.ByteInputChannel
private transient ByteChunk.ByteOutputChannel
private static final long
Inherited from org.apache.tomcat.util.buf.AbstractChunk:
ARRAY_MAX_SIZEendhasHashCodeisSetsmstart

Constructor Summary

AccessConstructor and Description
public
ByteChunk()

Creates a new, uninitialized ByteChunk object.

public
ByteChunk(int initial)

Method Summary

Modifier and TypeMethod and Description
public void
allocate(int initial, int limit)

public void
append(byte b)

public void
public void
append(byte[]
Bytes array
src
,
int
Offset
off
,
int
Length
len
)

Add data to the buffer.

public void
append(ByteBuffer
the ByteBuffer with the data
from
)

Add data to the buffer.

private boolean
public Object
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

public static byte[]

Returns:

the byte array value
convertToBytes
(String
to convert to byte array
value
)

Convert specified String to a byte array.

public boolean
equals(Object
the reference object with which to compare.
obj
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

public boolean

Returns:

true if the comparison succeeded, false otherwise
equals
(String
the String to compare
s
)

Compares the message bytes to the specified String object.

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

public boolean
public boolean

Returns:

true if the comparison succeeded, false otherwise
equals
(char[]
the array to compare to
c2
,
int
offset
off2
,
int
length
len2
)

Compares the message bytes to the specified char array.

public boolean

Returns:

true if the comparison succeeded, false otherwise
equalsIgnoreCase
(String
the String to compare
s
)

Compares the message bytes to the specified String object.

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

public static int

Returns:

The position of the first instance of the byte or -1 if the byte is not found.
findByte
(byte[]
The byte array to search
bytes
,
int
The point to start searching from in the byte array
start
,
int
The point to stop searching in the byte array
end
,
byte
The byte to search for
b
)

Returns the first instance of the given byte in the byte array between the specified start and end.

public static int

Returns:

The position of the first instance of the byte or -1 if the byte is not found.
findBytes
(byte[]
The byte array to search
bytes
,
int
The point to start searching from in the byte array
start
,
int
The point to stop searching in the byte array
end
,
byte[]
The array of bytes to search for
b
)

Returns the first instance of any of the given bytes in the byte array between the specified start and end.

public void
flushBuffer()

Send the buffer to the sink.

public byte[]

Returns:

the buffer.
getBuffer
()

protected int
public byte[]

Returns:

the buffer.
getBytes
()

public Charset
public long
public int

Returns:

The position of the first instance of the character or -1 if the character is not found.
indexOf
(char
The character
c
,
int
The start position
starting
)

Returns the first instance of the given character in this ByteChunk starting at the specified byte.

public static int

Returns:

The position of the first instance of the character or -1 if the character is not found.
indexOf
(byte[]
The array to search
bytes
,
int
The point to start searching from in the array
start
,
int
The point to stop searching in the array
end
,
char
The character to search for
s
)

Returns the first instance of the given character in the given byte array between the specified start and end.

public void
makeSpace(int
The size
count
)

Make space for len bytes.

private void
public void
recycle()

Overrides org.apache.tomcat.util.buf.AbstractChunk.recycle.

Resets the chunk to an uninitialized state.

public void
setByteInputChannel(ByteChunk.ByteInputChannel
The input channel
in
)

When the buffer is empty, read the data from the input channel.

public void
setByteOutputChannel(ByteChunk.ByteOutputChannel
The output channel
out
)

When the buffer is full, write the data to the output channel.

public void
setBytes(byte[]
the ascii bytes
b
,
int
the start offset of the bytes
off
,
int
the length of the bytes
len
)

Sets the buffer to the specified subarray of bytes.

public void
public boolean

Returns:

true if the start matches
startsWith
(String
the string
s
,
int
The position
pos
)

Returns true if the buffer starts with the specified string when tested in a case sensitive manner.

public boolean

Returns:

true if the start matches
startsWithIgnoreCase
(String
the string
s
,
int
The position
pos
)

Returns true if the buffer starts with the specified string when tested in a case insensitive manner.

public int
public int
subtract(byte[] dest, int off, int len)

public int

Returns:

an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached
subtract
(ByteBuffer
the ByteBuffer into which bytes are to be written.
to
)

Transfers bytes from the buffer to the specified ByteBuffer.

public byte
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public String
toString(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction)

public String

Returns:

The result of converting the bytes to a String
toStringInternal
()
Deprecated Unused. This method will be removed in Tomcat 11 onwards.

Converts the current content of the byte buffer to a String using the configured character set.

public String

Returns:

The result of converting the bytes to a String
toStringInternal
(CodingErrorAction
Action to take if the input is malformed
malformedInputAction
,
CodingErrorAction
Action to take if a byte sequence can't be mapped to a character
unmappableCharacterAction
)

Converts the current content of the byte buffer to a String using the configured character set.

private void
Inherited from org.apache.tomcat.util.buf.AbstractChunk:
getEndgetLengthgetLimitgetLimitInternalgetOffsetgetStarthashhashCodeindexOfisNullsetEndsetLimitsetOffset

Field Detail

buffback to summary
private byte[] buff
charsetback to summary
private transient Charset charset
DEFAULT_CHARSETback to summary
public static final Charset 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.

inback to summary
private transient ByteChunk.ByteInputChannel in
outback to summary
private transient ByteChunk.ByteOutputChannel out
serialVersionUIDback to summary
private static final long serialVersionUID

Hides org.apache.tomcat.util.buf.AbstractChunk.serialVersionUID.

Constructor Detail

ByteChunkback to summary
public ByteChunk()

Creates a new, uninitialized ByteChunk object.

ByteChunkback to summary
public ByteChunk(int initial)

Method Detail

allocateback to summary
public void allocate(int initial, int limit)
appendback to summary
public void append(byte b) throws IOException
appendback to summary
public void append(ByteChunk src) throws IOException
appendback to summary
public void append(byte[] src, int off, int len) throws IOException

Add data to the buffer.

Parameters
src:byte[]

Bytes array

off:int

Offset

len:int

Length

Exceptions
IOException:
Writing overflow data to the output channel failed
appendback to summary
public void append(ByteBuffer from) throws IOException

Add data to the buffer.

Parameters
from:ByteBuffer

the ByteBuffer with the data

Exceptions
IOException:
Writing overflow data to the output channel failed
checkEofback to summary
private boolean checkEof() throws IOException
cloneback to summary
public Object clone() throws CloneNotSupportedException

Overrides java.lang.Object.clone.

Doc from java.lang.Object.clone.

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any object x, the expression:

x.clone() != x
will be true, and that the expression:
x.clone().getClass() == x.getClass()
will be true, but these are not absolute requirements. While it is typically the case that:
x.clone().equals(x)
will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified.

Returns:Object

a clone of this instance.

Annotations
@Override
Exceptions
CloneNotSupportedException:
if the object's class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
convertToBytesback to summary
public static byte[] convertToBytes(String value)

Convert specified String to a byte array. This ONLY WORKS for ascii, UTF chars will be truncated.

Parameters
value:String

to convert to byte array

Returns:byte[]

the byte array value

equalsback to summary
public boolean equals(Object obj)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
obj:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
equalsback to summary
public boolean equals(String s)

Compares the message bytes to the specified String object.

Note

This only works for characters in the range 0-127.

Parameters
s:String

the String to compare

Returns:boolean

true if the comparison succeeded, false otherwise

equalsback to summary
public boolean equals(ByteChunk bb)
equalsback to summary
public boolean equals(byte[] b2, int off2, int len2)
equalsback to summary
public boolean equals(CharChunk cc)
equalsback to summary
public boolean equals(char[] c2, int off2, int len2)

Compares the message bytes to the specified char array.

Note

This only works for characters in the range 0-127.

Parameters
c2:char[]

the array to compare to

off2:int

offset

len2:int

length

Returns:boolean

true if the comparison succeeded, false otherwise

equalsIgnoreCaseback to summary
public boolean equalsIgnoreCase(String s)

Compares the message bytes to the specified String object.

Note

This only works for characters in the range 0-127.

Parameters
s:String

the String to compare

Returns:boolean

true if the comparison succeeded, false otherwise

equalsIgnoreCaseback to summary
public boolean equalsIgnoreCase(byte[] b2, int off2, int len2)
findByteback to summary
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:byte[]

The byte array to search

start:int

The point to start searching from in the byte array

end:int

The point to stop searching in the byte array

b:byte

The byte to search for

Returns:int

The position of the first instance of the byte or -1 if the byte is not found.

findBytesback to summary
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:byte[]

The byte array to search

start:int

The point to start searching from in the byte array

end:int

The point to stop searching in the byte array

b:byte[]

The array of bytes to search for

Returns:int

The position of the first instance of the byte or -1 if the byte is not found.

flushBufferback to summary
public void flushBuffer() throws IOException

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.

Exceptions
IOException:
Writing overflow data to the output channel failed
getBufferback to summary
public byte[] getBuffer()
Returns:byte[]

the buffer.

getBufferElementback to summary
protected int getBufferElement(int index)

Implements abstract org.apache.tomcat.util.buf.AbstractChunk.getBufferElement.

Annotations
@Override
getBytesback to summary
public byte[] getBytes()
Returns:byte[]

the buffer.

getCharsetback to summary
public Charset getCharset()
getLongback to summary
public long getLong()
indexOfback to summary
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:char

The character

starting:int

The start position

Returns:int

The position of the first instance of the character or -1 if the character is not found.

indexOfback to summary
public 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:byte[]

The array to search

start:int

The point to start searching from in the array

end:int

The point to stop searching in the array

s:char

The character to search for

Returns:int

The position of the first instance of the character or -1 if the character is not found.

makeSpaceback to summary
public void makeSpace(int count)

Make space for len bytes. If len is small, allocate a reserve space too. Never grow bigger than the limit or AbstractChunk#ARRAY_MAX_SIZE.

Parameters
count:int

The size

readObjectback to summary
private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException
recycleback to summary
public void recycle()

Overrides org.apache.tomcat.util.buf.AbstractChunk.recycle.

Doc from org.apache.tomcat.util.buf.AbstractChunk.recycle.

Resets the chunk to an uninitialized state.

Annotations
@Override
setByteInputChannelback to summary
public void setByteInputChannel(ByteChunk.ByteInputChannel in)

When the buffer is empty, read the data from the input channel.

Parameters
in:ByteChunk.ByteInputChannel

The input channel

setByteOutputChannelback to summary
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.

Parameters
out:ByteChunk.ByteOutputChannel

The output channel

setBytesback to summary
public void setBytes(byte[] b, int off, int len)

Sets the buffer to the specified subarray of bytes.

Parameters
b:byte[]

the ascii bytes

off:int

the start offset of the bytes

len:int

the length of the bytes

setCharsetback to summary
public void setCharset(Charset charset)
startsWithback to summary
public boolean startsWith(String s, int pos)

Returns true if the buffer starts with the specified string when tested in a case sensitive manner.

Note

This only works for characters in the range 0-127.

Parameters
s:String

the string

pos:int

The position

Returns:boolean

true if the start matches

startsWithIgnoreCaseback to summary
public boolean startsWithIgnoreCase(String s, int pos)

Returns true if the buffer starts with the specified string when tested in a case insensitive manner.

Note

This only works for characters in the range 0-127.

Parameters
s:String

the string

pos:int

The position

Returns:boolean

true if the start matches

subtractback to summary
public int subtract() throws IOException
subtractback to summary
public int subtract(byte[] dest, int off, int len) throws IOException
subtractback to summary
public int subtract(ByteBuffer to) throws IOException

Transfers 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:ByteBuffer

the ByteBuffer into which bytes are to be written.

Returns:int

an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached

Exceptions
IOException:
if an input or output exception has occurred
subtractBback to summary
public byte subtractB() throws IOException
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@Override
toStringback to summary
public String toString(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) throws CharacterCodingException
toStringInternalback to summary
public String toStringInternal()

Deprecated

Unused. This method will be removed in Tomcat 11 onwards.

Converts the current content of the byte buffer to a String using the configured character set.

Returns:String

The result of converting the bytes to a String

Annotations
@Deprecated
toStringInternalback to summary
public String toStringInternal(CodingErrorAction malformedInputAction, CodingErrorAction unmappableCharacterAction) throws CharacterCodingException

Converts the current content of the byte buffer to a String using the configured character set.

Parameters
malformedInputAction:CodingErrorAction

Action to take if the input is malformed

unmappableCharacterAction:CodingErrorAction

Action to take if a byte sequence can't be mapped to a character

Returns:String

The result of converting the bytes to a String

Exceptions
CharacterCodingException:
If an error occurs during the conversion
writeObjectback to summary
private void writeObject(ObjectOutputStream oos) throws IOException
org.apache.tomcat.util.buf back to summary

public Class ByteChunk.BufferOverflowException

extends IOException
Class Inheritance

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
public

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.io.IOException.serialVersionUID.

Constructor Detail

BufferOverflowExceptionback to summary
public BufferOverflowException(String message)
org.apache.tomcat.util.buf back to summary

public Interface ByteChunk.ByteInputChannel

Known Direct Implementers
org.apache.catalina.connector.InputBuffer

Input interface, used when the buffer is empty. Same as java.nio.channels.ReadableByteChannel

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

The number of bytes read
realReadBytes
()

Read new bytes.

Method Detail

realReadBytesback to summary
public int realReadBytes() throws IOException

Read new bytes.

Returns:int

The number of bytes read

Exceptions
IOException:
If an I/O error occurs during reading
org.apache.tomcat.util.buf back to summary

public Interface ByteChunk.ByteOutputChannel


When we need more space we'll either grow the buffer ( up to the limit ) or send it to a channel. Same as java.nio.channel.WritableByteChannel.

Method Summary

Modifier and TypeMethod and Description
public void
realWriteBytes(byte[]
bytes that will be written
buf
,
int
offset in the bytes array
off
,
int
length that will be written
len
)

Send the bytes ( usually the internal conversion buffer ).

public void
realWriteBytes(ByteBuffer
bytes that will be written
from
)

Send the bytes ( usually the internal conversion buffer ).

Method Detail

realWriteBytesback to summary
public void realWriteBytes(byte[] buf, int off, int len) throws IOException

Send the bytes ( usually the internal conversion buffer ). Expect 8k output if the buffer is full.

Parameters
buf:byte[]

bytes that will be written

off:int

offset in the bytes array

len:int

length that will be written

Exceptions
IOException:
If an I/O occurs while writing the bytes
realWriteBytesback to summary
public void realWriteBytes(ByteBuffer from) throws IOException

Send the bytes ( usually the internal conversion buffer ). Expect 8k output if the buffer is full.

Parameters
from:ByteBuffer

bytes that will be written

Exceptions
IOException:
If an I/O occurs while writing the bytes