ByteChunk (Apache Tomcat 8.5.57 API Documentation)

org.apache.tomcat.util.buf

## Class ByteChunk

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

- All Implemented Interfaces:
    Serializable, Cloneable

      public final class ByteChunk
      extends AbstractChunk

  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

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `ByteChunk.ByteInputChannel` Input interface, used when the buffer is empty.
    `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

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static Charset` | `DEFAULT_CHARSET` Default encoding used to convert to strings.

    - ### Fields inherited from class org.apache.tomcat.util.buf.AbstractChunk

      `ARRAY_MAX_SIZE, end, hasHashCode, isSet, start`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ByteChunk()` Creates a new, uninitialized ByteChunk object.
    `ByteChunk(int initial)`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `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(ByteBuffer from)` Add data to the buffer.
    `void` | `append(ByteChunk src)`
    `Object` | `clone()`
    `static byte[]` | `convertToBytes(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(Object obj)`
    `boolean` | `equals(String s)` Compares the message bytes to the specified String object.
    `boolean` | `equalsIgnoreCase(String s)` Compares the message bytes to the specified String object.
    `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.
    `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.
    `void` | `flushBuffer()` Send the buffer to the sink.
    `byte[]` | `getBuffer()`
    `protected int` | `getBufferElement(int index)`
    `byte[]` | `getBytes()`
    `Charset` | `getCharset()`
    `long` | `getLong()`
    `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.
    `int` | `indexOf(char c, int starting)` Returns the first instance of the given character in this ByteChunk starting at the specified byte.
    `void` | `makeSpace(int count)` Make space for len bytes.
    `void` | `recycle()` Resets the chunk to an uninitialized state.
    `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 buffer to the specified subarray of bytes.
    `void` | `setCharset(Charset charset)`
    `boolean` | `startsWith(String s, int pos)` Returns true if the buffer starts with the specified string when tested in a case sensitive manner.
    `boolean` | `startsWithIgnoreCase(String s, int pos)` Returns true if the buffer starts with the specified string when tested in a case insensitive manner.
    `int` | `substract()`
    `int` | `substract(byte[] dest, int off, int len)`
    `int` | `substract(ByteBuffer to)` Transfers bytes from the buffer to the specified ByteBuffer.
    `byte` | `substractB()`
    `String` | `toString()`
    `String` | `toStringInternal()`

    - ### Methods inherited from class org.apache.tomcat.util.buf.AbstractChunk

      `getEnd, getLength, getLimit, getLimitInternal, getOffset, getStart, hash, hashCode, indexOf, isNull, setEnd, setLimit, setOffset`

    - ### Methods inherited from class java.lang.Object

      `finalize, getClass, notify, notifyAll, wait, wait, wait`

- - ### Field Detail

    - #### DEFAULT_CHARSET

          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.

  - ### Constructor Detail

    - #### ByteChunk

          public ByteChunk()

      Creates a new, uninitialized ByteChunk object.

    - #### ByteChunk

          public ByteChunk(int initial)

  - ### Method Detail

    - #### clone

          public Object clone()
                       throws CloneNotSupportedException

      Overrides:
        `clone` in class `Object`
      Throws:
        `CloneNotSupportedException`

    - #### recycle

          public void recycle()

      Description copied from class: `AbstractChunk`
      Resets the chunk to an uninitialized state.
      Overrides:
        `recycle` in class `AbstractChunk`

    - #### allocate

          public void allocate(int initial,
                      int limit)

    - #### setBytes

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

      Sets the buffer to the specified subarray of bytes.
      Parameters:
        `b` - the ascii bytes
        `off` - the start offset of the bytes
        `len` - the length of the bytes

    - #### setCharset

          public void setCharset(Charset charset)

    - #### getCharset

          public Charset getCharset()

    - #### getBytes

          public byte[] getBytes()

      Returns:
        the buffer.

    - #### getBuffer

          public byte[] getBuffer()

      Returns:
        the buffer.

    - #### setByteInputChannel

          public void setByteInputChannel(ByteChunk.ByteInputChannel in)

      When the buffer is empty, read the data from the input channel.
      Parameters:
        `in` - 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.
      Parameters:
        `out` - The output channel

    - #### append

          public void append(byte b)
                      throws IOException

      Throws:
        `IOException`

    - #### append

          public void append(ByteChunk src)
                      throws IOException

      Throws:
        `IOException`

    - #### append

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

      Add data to the buffer.
      Parameters:
        `src` - Bytes array
        `off` - Offset
        `len` - Length
      Throws:
        `IOException` - Writing overflow data to the output channel failed

    - #### append

          public void append(ByteBuffer from)
                      throws IOException

      Add data to the buffer.
      Parameters:
        `from` - the ByteBuffer with the data
      Throws:
        `IOException` - Writing overflow data to the output channel failed

    - #### substract

          public int substract()
                        throws IOException

      Throws:
        `IOException`

    - #### substractB

          public byte substractB()
                          throws IOException

      Throws:
        `IOException`

    - #### substract

          public int substract(byte[] dest,
                      int off,
                      int len)
                        throws IOException

      Throws:
        `IOException`

    - #### substract

          public int substract(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 transfered bytes.
      Parameters:
        `to` - the ByteBuffer into which bytes are to be written.
      Returns:
        an integer specifying the actual number of bytes read, or -1 if the end of the stream is reached
      Throws:
        `IOException` - if an input or output exception has occurred

    - #### flushBuffer

          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.
      Throws:
        `IOException` - Writing overflow data to the output channel failed

    - #### makeSpace

          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` - The size

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### toStringInternal

          public String toStringInternal()

    - #### getLong

          public long getLong()

    - #### equals

          public boolean equals(Object obj)

      Overrides:
        `equals` in class `Object`

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

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

    - #### 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(String s,
                           int pos)

      Returns true if the buffer starts with the specified string when tested in a case sensitive manner.
      Parameters:
        `s` - the string
        `pos` - The position
      Returns:
        `true` if the start matches

    - #### startsWithIgnoreCase

          public boolean startsWithIgnoreCase(String s,
                                     int pos)

      Returns true if the buffer starts with the specified string when tested in a case insensitive manner.
      Parameters:
        `s` - the string
        `pos` - The position
      Returns:
        `true` if the start matches

    - #### getBufferElement

          protected int getBufferElement(int index)

      Specified by:
        `getBufferElement` in class `AbstractChunk`

    - #### indexOf

          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` - The character
        `starting` - The start position
      Returns:
        The position of the first instance of the character or -1 if the character is not found.

    - #### indexOf

          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` - The array to search
        `start` - The point to start searching from in the array
        `end` - The point to stop searching in the array
        `s` - The character to search for
      Returns:
        The position of the first instance of the character or -1 if the character is not found.

    - #### findByte

          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` - The byte array to search
        `start` - The point to start searching from in the byte array
        `end` - The point to stop searching in the byte array
        `b` - The byte to search for
      Returns:
        The position of the first instance of the byte or -1 if the byte is not found.

    - #### findBytes

          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` - The byte array to search
        `start` - The point to start searching from in the byte array
        `end` - The point to stop searching in the byte array
        `b` - The array of bytes to search for
      Returns:
        The position of the first instance of the byte or -1 if the byte is not found.

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

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