## org.apache.tomcat.util.buf
Class ByteChunk

    java.lang.Object
      org.apache.tomcat.util.buf.ByteChunk

All Implemented Interfaces:
  java.io.Serializable, java.lang.Cloneable

    public final class ByteChunk

extends java.lang.Object
implements java.lang.Cloneable, java.io.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
See Also:
  Serialized Form

Nested Class Summary
`static interface` `ByteChunk.ByteInputChannel` Input interface, used when the buffer is emptiy Same as java.nio.channel.ReadableByteChannel
`static interface` `ByteChunk.ByteOutputChannel` Same as java.nio.channel.WrittableByteChannel.

Field Summary
`static java.lang.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(byte[] src, int off, int len)` Add data to the buffer
`void` `append(ByteChunk src)`
`void` `append(char c)` Append a char, by casting it to byte.
`static byte[]` `convertToBytes(java.lang.String value)` Convert specified String to a byte array.
`boolean` `equals(byte[] b2, int off2, int len2)`
`boolean` `equals(ByteChunk bb)`
`boolean` `equals(char[] c2, int off2, int len2)`
`boolean` `equals(CharChunk cc)`
`boolean` `equals(java.lang.String s)` Compares the message bytes to the specified String object.
`boolean` `equalsIgnoreCase(java.lang.String s)` Compares the message bytes to the specified String object.
`static int` `findChar(byte[] buf, int start, int end, char c)` Find a character, no side effects.
`static int` `findChars(byte[] buf, int start, int end, byte[] c)` Find a character, no side effects.
`static int` `findNotChars(byte[] buf, int start, int end, byte[] c)` Find the first character !
`void` `flushBuffer()` Send the buffer to the sink.
`byte[]` `getBuffer()` Returns the message bytes.
`byte[]` `getBytes()` Returns the message bytes.
`ByteChunk` `getClone()`
`java.lang.String` `getEncoding()`
`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()`
`static int` `indexOf(byte[] bytes, int off, int end, char qq)`
`int` `indexOf(char c, int starting)` Returns true if the message bytes starts with the specified string.
`int` `indexOf(java.lang.String src, int srcOff, int srcLen, int myOff)`
`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(java.lang.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(byte[] b2)`
`boolean` `startsWith(java.lang.String s)` Returns true if the message bytes starts with the specified string.
`boolean` `startsWithIgnoreCase(java.lang.String s, int pos)` Returns true if the message bytes starts with the specified string.
`int` `substract()`
`int` `substract(byte[] src, int off, int len)`
`int` `substract(ByteChunk src)`
`java.lang.String` `toString()`
`java.lang.String` `toStringInternal()`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Field Detail

### DEFAULT_CHARACTER_ENCODING

    public static final java.lang.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.

  See Also:
    Constant Field Values

Constructor Detail

### ByteChunk

    public ByteChunk()

  Creates a new, uninitialized ByteChunk object.

### ByteChunk

    public ByteChunk(int initial)

Method Detail

### getClone

    public ByteChunk getClone()

### isNull

    public boolean isNull()

### recycle

    public void recycle()

  Resets the message buff to an uninitialized state.

### reset

    public void reset()

### allocate

    public void allocate(int initial,
                         int 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

### setOptimizedWrite

    public void setOptimizedWrite(boolean optimizedWrite)

### setEncoding

    public void setEncoding(java.lang.String enc)

### getEncoding

    public java.lang.String getEncoding()

### getBytes

    public byte[] getBytes()

  Returns the message bytes.

### getBuffer

    public byte[] getBuffer()

  Returns the message bytes.

### getStart

    public int getStart()

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

### getOffset

    public int getOffset()

### setOffset

    public void setOffset(int off)

### getLength

    public int getLength()

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

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

### getLimit

    public int getLimit()

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

### getEnd

    public int getEnd()

### setEnd

    public void setEnd(int i)

### append

    public void append(char c)
                throws java.io.IOException

  Append a char, by casting it to byte. This IS NOT intended for unicode.

  Parameters:
    `c` -
  Throws:
    `java.io.IOException`

### append

    public void append(byte b)
                throws java.io.IOException

  Throws:
    `java.io.IOException`

### append

    public void append(ByteChunk src)
                throws java.io.IOException

  Throws:
    `java.io.IOException`

### append

    public void append(byte[] src,
                       int off,
                       int len)
                throws java.io.IOException

  Add data to the buffer

  Throws:
    `java.io.IOException`

### substract

    public int substract()
                  throws java.io.IOException

  Throws:
    `java.io.IOException`

### substract

    public int substract(ByteChunk src)
                  throws java.io.IOException

  Throws:
    `java.io.IOException`

### substract

    public int substract(byte[] src,
                         int off,
                         int len)
                  throws java.io.IOException

  Throws:
    `java.io.IOException`

### flushBuffer

    public void flushBuffer()
                     throws java.io.IOException

  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:
    `java.io.IOException`

### toString

    public java.lang.String toString()

  Overrides:
    `toString` in class `java.lang.Object`

### toStringInternal

    public java.lang.String toStringInternal()

### getInt

    public int getInt()

### getLong

    public long getLong()

### equals

    public boolean equals(java.lang.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

### equalsIgnoreCase

    public boolean equalsIgnoreCase(java.lang.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)

### startsWith

    public boolean startsWith(java.lang.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(java.lang.String s,
                                        int pos)

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

  Parameters:
    `s` - the string
    `pos` - The position

### indexOf

    public int indexOf(java.lang.String src,
                       int srcOff,
                       int srcLen,
                       int myOff)

### hash

    public int hash()

### hashIgnoreCase

    public int hashIgnoreCase()

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

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

### convertToBytes

    public static final byte[] convertToBytes(java.lang.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

Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.