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

public final Class MessageBytes

extends Object
implements Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable
Imports
java.io.IOException, .Serializable, java.nio.ByteBuffer, .CharBuffer, java.nio.charset.Charset, java.util.Locale, org.apache.tomcat.util.res.StringManager

This class is used to represent a subarray of bytes in an HTTP message. It represents all request/response elements. The byte/char conversions are delayed and cached. Everything is recyclable.

The object can represent a byte[], a char[], or a (sub) String. All operations can be made in case sensitive mode or not.

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private final ByteChunk
private final CharChunk
public static final char[]
private static final MessageBytes.MessageBytesFactory
private boolean
private int
private boolean
private long
private static final long
private static final StringManager
private String
public static final int
T_BYTES

getType() is T_BYTES if the the object used to create the MessageBytes was a byte[].

public static final int
T_CHARS

getType() is T_CHARS if the the object used to create the MessageBytes was a char[].

public static final int
public static final int
T_STR

getType() is T_STR if the the object used to create the MessageBytes was a String.

private int

Constructor Summary

AccessConstructor and Description
private
MessageBytes()

Creates a new, uninitialized MessageBytes object.

Method Summary

Modifier and TypeMethod and Description
public Object
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.

public void
duplicate(MessageBytes
The source
src
)

Copy the src into this MessageBytes, allocating more space if needed.

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

Returns:

the byte chunk
getByteChunk
()

Returns the byte chunk, representing the byte[] and offset/length.

public CharChunk

Returns:

the char chunk
getCharChunk
()

Returns the char chunk, representing the char[] and offset/length.

public Charset

Returns:

the Charset used for string<->byte conversions.
getCharset
()

public int

Returns:

the length
getLength
()

Returns the length of the original buffer.

public long

Returns:

the long value
getLong
()

Convert the buffer to a long, cache the value.

public String

Returns:

the string
getString
()

Returns the string value.

public int

Returns:

the type
getType
()

Return the type of the original content.

private int
hash()

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

public int
indexOf(String s, int starting)

public int
public int
indexOfIgnoreCase(String s, int starting)

public boolean
public static MessageBytes

Returns:

the instance
newInstance
()

Construct a new MessageBytes instance.

public void
recycle()

Resets the message bytes to an uninitialized (NULL) state.

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

Sets the content to the specified subarray of bytes.

public void
setChars(char[]
the chars
c
,
int
the start offset of the chars
off
,
int
the length of the chars
len
)

Sets the content to be a char[]

public void
setCharset(Charset
The charset
charset
)

Set the Charset used for string<->byte conversions.

public void
setLong(long
The long
l
)

Set the buffer to the representation of a long.

public void
setString(String
The string
s
)

Set the content to be a string

public boolean

Returns:

true if the message bytes starts with the specified string.
startsWithIgnoreCase
(String
the string
s
,
int
The start position
pos
)

public void
toBytes()

Convert to bytes and fill the ByteChunk with the converted value.

private void
toBytesSimple(char[] chars, int start, int len)

Simple conversion of chars to bytes.

public void
toChars()

Convert to char[] and fill the CharChunk.

public String

Returns:

the string
toString
()

Overrides java.lang.Object.toString.

Compute the string value.

public String

Returns:

The current value as a String
toStringType
()

Convert to String (if not already of the String type) and then return the String value.

Inherited from java.lang.Object:
finalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

byteCback to summary
private final ByteChunk byteC
charCback to summary
private final CharChunk charC
EMPTY_CHAR_ARRAYback to summary
public static final char[] EMPTY_CHAR_ARRAY
factoryback to summary
private static final MessageBytes.MessageBytesFactory factory
hasHashCodeback to summary
private boolean hasHashCode
hashCodeback to summary
private int hashCode
hasLongValueback to summary
private boolean hasLongValue
longValueback to summary
private long longValue
serialVersionUIDback to summary
private static final long serialVersionUID
smback to summary
private static final StringManager sm
strValueback to summary
private String strValue
T_BYTESback to summary
public static final int T_BYTES

getType() is T_BYTES if the the object used to create the MessageBytes was a byte[].

T_CHARSback to summary
public static final int T_CHARS

getType() is T_CHARS if the the object used to create the MessageBytes was a char[].

T_NULLback to summary
public static final int T_NULL
T_STRback to summary
public static final int T_STR

getType() is T_STR if the the object used to create the MessageBytes was a String.

typeback to summary
private int type

Constructor Detail

MessageBytesback to summary
private MessageBytes()

Creates a new, uninitialized MessageBytes object. Use static newInstance() in order to allow future hooks.

Method Detail

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.
duplicateback to summary
public void duplicate(MessageBytes src) throws IOException

Copy the src into this MessageBytes, allocating more space if needed.

Parameters
src:MessageBytes

The source

Exceptions
IOException:
Writing overflow data to the output channel failed
equalsback to summary
public boolean equals(String s)

Compares the message bytes to the specified String object.

Parameters
s:String

the String to compare

Returns:boolean

true if the comparison succeeded, false otherwise

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(MessageBytes mb)
equalsIgnoreCaseback to summary
public boolean equalsIgnoreCase(String s)

Compares the message bytes to the specified String object.

Parameters
s:String

the String to compare

Returns:boolean

true if the comparison succeeded, false otherwise

getByteChunkback to summary
public ByteChunk getByteChunk()

Returns the byte chunk, representing the byte[] and offset/length. Valid only if T_BYTES or after a conversion was made.

Returns:ByteChunk

the byte chunk

getCharChunkback to summary
public CharChunk getCharChunk()

Returns the char chunk, representing the char[] and offset/length. Valid only if T_CHARS or after a conversion was made.

Returns:CharChunk

the char chunk

getCharsetback to summary
public Charset getCharset()
Returns:Charset

the Charset used for string<->byte conversions.

getLengthback to summary
public int getLength()

Returns the length of the original buffer.

Note

The length in bytes may be different from the length in chars.

Returns:int

the length

getLongback to summary
public long getLong()

Convert the buffer to a long, cache the value. Used for headers conversion.

Returns:long

the long value

getStringback to summary
public String getString()

Returns the string value. Valid only if T_STR or after a conversion was made.

Returns:String

the string

getTypeback to summary
public int getType()

Return the type of the original content. Can be T_STR, T_BYTES, T_CHARS or T_NULL

Returns:int

the type

hashback to summary
private int hash()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object

Annotations
@Override
indexOfback to summary
public int indexOf(String s, int starting)
indexOfback to summary
public int indexOf(String s)
indexOfIgnoreCaseback to summary
public int indexOfIgnoreCase(String s, int starting)
isNullback to summary
public boolean isNull()
newInstanceback to summary
public static MessageBytes newInstance()

Construct a new MessageBytes instance.

Returns:MessageBytes

the instance

recycleback to summary
public void recycle()

Resets the message bytes to an uninitialized (NULL) state.

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

Sets the content to the specified subarray of bytes.

Parameters
b:byte[]

the bytes

off:int

the start offset of the bytes

len:int

the length of the bytes

setCharsback to summary
public void setChars(char[] c, int off, int len)

Sets the content to be a char[]

Parameters
c:char[]

the chars

off:int

the start offset of the chars

len:int

the length of the chars

setCharsetback to summary
public void setCharset(Charset charset)

Set the Charset used for string<->byte conversions.

Parameters
charset:Charset

The charset

setLongback to summary
public void setLong(long l)

Set the buffer to the representation of a long.

Parameters
l:long

The long

setStringback to summary
public void setString(String s)

Set the content to be a string

Parameters
s:String

The string

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

the string

pos:int

The start position

Returns:boolean

true if the message bytes starts with the specified string.

toBytesback to summary
public void toBytes()

Convert to bytes and fill the ByteChunk with the converted value.

toBytesSimpleback to summary
private void toBytesSimple(char[] chars, int start, int len)

Simple conversion of chars to bytes.

Exceptions
IllegalArgumentException:
if any of the characters to convert are above code point 0xFF.
toCharsback to summary
public void toChars()

Convert to char[] and fill the CharChunk.

Note

The conversion from bytes is not optimised - it converts to String first. However, Tomcat doesn't call this method to convert from bytes so there is no benefit from optimising that path.

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Compute the string value.

Returns:String

the string

Annotations
@Override
toStringTypeback to summary
public String toStringType()

Convert to String (if not already of the String type) and then return the String value.

Returns:String

The current value as a String

org.apache.tomcat.util.buf back to summary

private Class MessageBytes.MessageBytesFactory

extends Object
Class Inheritance

Constructor Summary

AccessConstructor and Description
protected

Method Summary

Modifier and TypeMethod and Description
public MessageBytes
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

MessageBytesFactoryback to summary
protected MessageBytesFactory()

Method Detail

newInstanceback to summary
public MessageBytes newInstance()