Top Description Inners Fields Constructors Methods
org.glassfish.grizzly.http.util

public final Class ByteChunk

extends Object
implements Chunk, Cloneable, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.lang.Cloneable, org.glassfish.grizzly.http.util.Chunk
Imports
java.io.IOException, .Serializable, .UnsupportedEncodingException, java.nio.ByteBuffer, java.nio.charset.Charset, java.util.Arrays

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
[email protected], James Todd [[email protected]], Costin Manolache, Remy Maucherat

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface
public static interface

Field Summary

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

Default encoding used to convert to strings.

private int
private transient ByteChunk.ByteInputChannel
private boolean
private int
private boolean
private transient ByteChunk.ByteOutputChannel
private static final long
private int

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(char c)

public void
append(byte b)

public void
public void
append(byte[] src, int off, int len)

Add data to the buffer

pack-priv boolean
public static byte[]

Returns:

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

Convert specified String to a byte array.

public void
delete(final int start, final int end)

Implements org.glassfish.grizzly.http.util.Chunk.delete.

public boolean
equals(Object o)

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
(final String
the String to compare
s
)

Compares the message bytes to the specified String object.

public boolean

Returns:

true if the comparison succeeded, false otherwise
equals
(final byte[]
the byte[] to compare
bytes
)

Compares the message bytes to the specified byte array.

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

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

public static boolean
equals(final byte[] b1, final int b1Offs, final int b1Len, final byte[] b2, final int b2Offs, final int b2Len)

public static boolean
equals(final byte[] b, int offs, final int len, final String s)

public boolean

Returns:

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

Compares the message bytes to the specified String object.

public boolean
equalsIgnoreCase(final byte[] b)

public boolean
equalsIgnoreCase(final byte[] b, final int offset, final int len)

public static boolean
equalsIgnoreCase(final byte[] b1, final int b1Offs, final int b1Len, final byte[] b2, final int b2Offs, final int b2Len)

public static boolean

Returns:

true if the comparison succeeded, false otherwise
equalsIgnoreCase
(final byte[] b, final int offset, final int len, final String
the String to compare
s
)

Compares the message bytes to the specified String object.

public boolean
public static boolean

Returns:

true if the comparison succeeded, false otherwise
equalsIgnoreCaseLowerCase
(final byte[]
the byte[] to compare
buffer
,
final int
buffer start
start
,
final int
buffer end
end
,
final byte[]
byte[] to compare against
cmpTo
)

Compares the buffer chunk to the specified byte array representing lower-case ASCII characters.

public static int

Returns:

index of char if found, -1 if not
findChar
(byte[] buf, int start, int end, char c)

Find a character, no side effects.

public static int

Returns:

index of char if found, -1 if not
findChars
(byte[] buf, int start, int end, byte[] c)

Find a character, no side effects.

public static int

Returns:

index of char if found, -1 if not
findNotChars
(byte[] buf, int start, int end, byte[] c)

Find the first character != c

public void
public byte[]
getBuffer()

Returns the message bytes.

public byte[]
getBytes()

Returns the message bytes.

public Charset
public ByteChunk
public int
public int
public int
getLength()

Implements org.glassfish.grizzly.http.util.Chunk.getLength.

Returns the length of the bytes.

public int
public long
public int
public int
getStart()

Implements org.glassfish.grizzly.http.util.Chunk.getStart.

Returns the start offset of the bytes.

public int
hash()

private static int
hashBytes(byte[] buff, int start, int bytesLen)

private static int
hashBytesIC(byte[] bytes, int start, int bytesLen)

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for this object.

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

public int
indexOf(char
the character
c
,
int
The start position
starting
)

Implements org.glassfish.grizzly.http.util.Chunk.indexOf.

Returns true if the message bytes starts with the specified string.

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

public boolean
private void
makeSpace(int count)

Make space for len chars.

protected void
notifyDirectUpdate()

Notify the Chunk that its content is going to be changed directly

public void
recycle()

Resets the message buff to an uninitialized state.

public void
public void
protected void
public void
setByteInputChannel(ByteChunk.ByteInputChannel in)

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

public void
setByteOutputChannel(ByteChunk.ByteOutputChannel 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 message bytes to the specified sub-array of bytes.

public void
public void
public void
setLimit(int limit)

Maximum amount of data in this buffer.

public void
setOffset(int off)

public void
setOptimizedWrite(boolean optimizedWrite)

public void
public boolean
startsWith(String
the string
s
)

Returns true if the message bytes starts with the specified string.

public boolean
startsWith(String
the string
s
,
int
The position
offset
)

Returns true if the message bytes starts with the specified string.

public boolean
startsWith(byte[] b2)

public static boolean
startsWith(final byte[] buffer, final int start, final int end, final byte[] cmpTo)

public boolean
startsWithIgnoreCase(String
the string
s
,
int
The position
pos
)

Returns true if the message bytes starts with the specified string.

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

public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public String
public String
toString(Charset charset)

public String
public void
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

buffback to summary
private byte[] buff
cachedStringback to summary
private String cachedString
cachedStringCharsetback to summary
private Charset cachedStringCharset
charsetback to summary
private Charset charset
DEFAULT_CHARSETback to summary
private 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.

endback to summary
private int end
inback to summary
private transient ByteChunk.ByteInputChannel in
isSetback to summary
private boolean isSet
limitback to summary
private int limit
optimizedWriteback to summary
private boolean optimizedWrite
outback to summary
private transient ByteChunk.ByteOutputChannel out
serialVersionUIDback to summary
private static final long serialVersionUID
startback to summary
private int start

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(char c) throws IOException
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

canGrowback to summary
pack-priv boolean canGrow()
convertToBytesback to summary
public static byte[] convertToBytes(String value)

Convert specified String to a byte array.

Parameters
value:String

to convert to byte array

Returns:byte[]

the byte array value

deleteback to summary
public void delete(final int start, final int end)

Implements org.glassfish.grizzly.http.util.Chunk.delete.

Annotations
@Override
equalsback to summary
public boolean equals(Object o)

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.

Annotations
@Override
equalsback to summary
public boolean equals(final 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(final byte[] bytes)

Compares the message bytes to the specified byte array.

Parameters
bytes:byte[]

the byte[] to compare

Returns:boolean

true if the comparison succeeded, false otherwise

Since
2.3
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)
equalsback to summary
public static boolean equals(final byte[] b1, final int b1Offs, final int b1Len, final byte[] b2, final int b2Offs, final int b2Len)
equalsback to summary
public static boolean equals(final byte[] b, int offs, final int len, final String s)
equalsIgnoreCaseback to summary
public boolean equalsIgnoreCase(final 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

equalsIgnoreCaseback to summary
public boolean equalsIgnoreCase(final byte[] b)
equalsIgnoreCaseback to summary
public boolean equalsIgnoreCase(final byte[] b, final int offset, final int len)
equalsIgnoreCaseback to summary
public static boolean equalsIgnoreCase(final byte[] b1, final int b1Offs, final int b1Len, final byte[] b2, final int b2Offs, final int b2Len)
equalsIgnoreCaseback to summary
public static boolean equalsIgnoreCase(final byte[] b, final int offset, final int len, final 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

equalsIgnoreCaseLowerCaseback to summary
public boolean equalsIgnoreCaseLowerCase(byte[] cmpTo)
equalsIgnoreCaseLowerCaseback to summary
public static boolean equalsIgnoreCaseLowerCase(final byte[] buffer, final int start, final int end, final byte[] cmpTo)

Compares the buffer chunk to the specified byte array representing lower-case ASCII characters.

Parameters
buffer:byte[]

the byte[] to compare

start:int

buffer start

end:int

buffer end

cmpTo:byte[]

byte[] to compare against

Returns:boolean

true if the comparison succeeded, false otherwise

Since
2.3
findCharback to summary
public static int findChar(byte[] buf, int start, int end, char c)

Find a character, no side effects.

Returns:int

index of char if found, -1 if not

findCharsback to summary
public static int findChars(byte[] buf, int start, int end, byte[] c)

Find a character, no side effects.

Returns:int

index of char if found, -1 if not

findNotCharsback to summary
public static int findNotChars(byte[] buf, int start, int end, byte[] c)

Find the first character != c

Returns:int

index of char if found, -1 if not

flushBufferback to summary
public void flushBuffer() throws IOException
getBufferback to summary
public byte[] getBuffer()

Returns the message bytes.

getBytesback to summary
public byte[] getBytes()

Returns the message bytes.

getCharsetback to summary
public Charset getCharset()
getCloneback to summary
public ByteChunk getClone()
getEndback to summary
public int getEnd()

Implements org.glassfish.grizzly.http.util.Chunk.getEnd.

Annotations
@Override
getIntback to summary
public int getInt()
getLengthback to summary
public int getLength()

Implements org.glassfish.grizzly.http.util.Chunk.getLength.

Returns the length of the bytes. XXX need to clean this up

Annotations
@Override
getLimitback to summary
public int getLimit()
getLongback to summary
public long getLong()
getOffsetback to summary
public int getOffset()
getStartback to summary
public int getStart()

Implements org.glassfish.grizzly.http.util.Chunk.getStart.

Returns the start offset of the bytes. For output this is the end of the buffer.

Annotations
@Override
hashback to summary
public int hash()
hashBytesback to summary
private static int hashBytes(byte[] buff, int start, int bytesLen)
hashBytesICback to summary
private static int hashBytesIC(byte[] bytes, int start, int bytesLen)
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.
Annotations
@Override
hashIgnoreCaseback to summary
public int hashIgnoreCase()
indexOfback to summary
public int indexOf(final String s, int fromIdx)

Implements org.glassfish.grizzly.http.util.Chunk.indexOf.

Annotations
@Override
indexOfback to summary
public int indexOf(String src, int srcOff, int srcLen, int myOff)
indexOfback to summary
public int indexOf(char c, int starting)

Implements org.glassfish.grizzly.http.util.Chunk.indexOf.

Returns true if the message bytes starts with the specified string.

Parameters
c:char

the character

starting:int

The start position

Annotations
@Override
indexOfback to summary
public static int indexOf(byte[] bytes, int off, int end, char qq)
isNullback to summary
public boolean isNull()
makeSpaceback to summary
private void makeSpace(int count)

Make space for len chars. If len is small, allocate a reserve space too. Never grow bigger than limit.

notifyDirectUpdateback to summary
protected void notifyDirectUpdate()

Notify the Chunk that its content is going to be changed directly

recycleback to summary
public void recycle()

Resets the message buff to an uninitialized state.

recycleAndResetback to summary
public void recycleAndReset()
resetback to summary
public void reset()
resetStringCacheback to summary
protected void resetStringCache()
setByteInputChannelback to summary
public void setByteInputChannel(ByteChunk.ByteInputChannel in)

When the buffer is empty, read the data from 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.

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

Sets the message bytes to the specified sub-array 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)
setEndback to summary
public void setEnd(int i)

Implements org.glassfish.grizzly.http.util.Chunk.setEnd.

Annotations
@Override
setLimitback to summary
public void setLimit(int limit)

Maximum amount of data in this buffer. If -1 or not set, the buffer will grow indefinitely. 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.

setOffsetback to summary
public void setOffset(int off)
setOptimizedWriteback to summary
public void setOptimizedWrite(boolean optimizedWrite)
setStartback to summary
public void setStart(int start)

Implements org.glassfish.grizzly.http.util.Chunk.setStart.

Annotations
@Override
startsWithback to summary
public boolean startsWith(String s)

Returns true if the message bytes starts with the specified string.

Parameters
s:String

the string

startsWithback to summary
public boolean startsWith(String s, int offset)

Returns true if the message bytes starts with the specified string.

Parameters
s:String

the string

offset:int

The position

startsWithback to summary
public boolean startsWith(byte[] b2)
startsWithback to summary
public static boolean startsWith(final byte[] buffer, final int start, final int end, final byte[] cmpTo)
startsWithIgnoreCaseback to summary
public boolean startsWithIgnoreCase(String s, int pos)

Returns true if the message bytes starts with the specified string.

Parameters
s:String

the string

pos:int

The position

substractback to summary
public int substract() throws IOException
substractback to summary
public int substract(ByteChunk src) throws IOException
substractback to summary
public int substract(byte[] src, int off, int len) 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.

Annotations
@Override
toStringback to summary
public String toString(int start, int end)

Implements org.glassfish.grizzly.http.util.Chunk.toString.

Annotations
@Override
toStringback to summary
public String toString(Charset charset)
toStringInternalback to summary
public String toStringInternal()
trimLeftback to summary
public void trimLeft()
org.glassfish.grizzly.http.util back to summary

public Interface ByteChunk.ByteInputChannel


Method Summary

Modifier and TypeMethod and Description
public int
realReadBytes(byte[] cbuf, int off, int len)

Read new bytes ( usually the internal conversion buffer ).

Method Detail

realReadBytesback to summary
public int realReadBytes(byte[] cbuf, int off, int len) throws IOException

Read new bytes ( usually the internal conversion buffer ). The implementation is allowed to ignore the parameters, and mutate the chunk if it wishes to implement its own buffering.

org.glassfish.grizzly.http.util back to summary

public Interface ByteChunk.ByteOutputChannel


Method Summary

Modifier and TypeMethod and Description
public void
realWriteBytes(byte[] cbuf, int off, int len)

Send the bytes ( usually the internal conversion buffer ).

Method Detail

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

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