org.apache.struts2.util.tomcat.buf

## Class ByteChunk

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

- All Implemented Interfaces:
    Serializable, Cloneable

      public final class ByteChunk
      extends Object
      implements Cloneable, 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

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static interface` | `ByteChunk.ByteInputChannel` Input interface, used when the buffer is empty Same as java.nio.channel.ReadableByteChannel
    `static interface` | `ByteChunk.ByteOutputChannel` Same as java.nio.channel.WrittableByteChannel.

  - ### Field Summary

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

  - ### Constructor Summary

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

  - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods Deprecated 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(ByteChunk src)`
    `void` | `append(char c)` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `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(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.
    `static int` | `findNotBytes(byte[] bytes, int start, int end, byte[] b)` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `void` | `flushBuffer()` Send the buffer to the sink.
    `byte[]` | `getBuffer()` Returns the message bytes.
    `byte[]` | `getBytes()` Returns the message bytes.
    `Charset` | `getCharset()`
    `ByteChunk` | `getClone()` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `int` | `getEnd()`
    `int` | `getInt()` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `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()` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `static int` | `indexOf(byte[] bytes, int start, int end, char c)` 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.
    `int` | `indexOf(String src, int srcOff, int srcLen, int myOff)`
    `boolean` | `isNull()`
    `void` | `makeSpace(int count)` Make space for len chars.
    `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` | `setCharset(Charset charset)`
    `void` | `setEnd(int i)`
    `void` | `setLimit(int limit)` Maximum amount of data in this buffer.
    `void` | `setOffset(int off)`
    `void` | `setOptimizedWrite(boolean optimizedWrite)` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `boolean` | `startsWith(byte[] b2)` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `boolean` | `startsWith(String s)` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `boolean` | `startsWithIgnoreCase(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)` Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
    `byte` | `substractB()`
    `String` | `toString()`
    `String` | `toStringInternal()`

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

      `clone, equals, finalize, getClass, hashCode, 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

    - #### getClone

          @Deprecated
          public ByteChunk getClone()

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.

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

          @Deprecated
          public void setOptimizedWrite(boolean optimizedWrite)

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.

    - #### setCharset

          public void setCharset(Charset charset)

    - #### getCharset

          public Charset getCharset()

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

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

          @Deprecated
          public void append(char c)
                                  throws IOException

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
      Append a char, by casting it to byte. This IS NOT intended for unicode.
      Parameters:
        `c` -
      Throws:
        `IOException`

    - #### 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
      Throws:
        `IOException`

    - #### substract

          public int substract()
                        throws IOException

      Throws:
        `IOException`

    - #### substract

          @Deprecated
          public int substract(ByteChunk src)
                                    throws IOException

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
      Throws:
        `IOException`

    - #### substractB

          public byte substractB()
                          throws IOException

      Throws:
        `IOException`

    - #### substract

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

      Throws:
        `IOException`

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

    - #### makeSpace

          public void makeSpace(int count)

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

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`

    - #### toStringInternal

          public String toStringInternal()

    - #### getInt

          @Deprecated
          public int getInt()

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.

    - #### getLong

          public long getLong()

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

          @Deprecated
          public boolean startsWith(String s)

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
      Returns true if the message bytes starts with the specified string.
      Parameters:
        `s` - the string

    - #### startsWith

          @Deprecated
          public boolean startsWith(byte[] b2)

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
      Returns true if the message bytes start with the specified byte array.

    - #### startsWithIgnoreCase

          public boolean startsWithIgnoreCase(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(String src,
                             int srcOff,
                             int srcLen,
                             int myOff)

    - #### hash

          public int hash()

    - #### hashIgnoreCase

          @Deprecated
          public int hashIgnoreCase()

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.

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

      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 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
        `c` - 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.

    - #### findNotBytes

          @Deprecated
          public static int findNotBytes(byte[] bytes,
                                                     int start,
                                                     int end,
                                                     byte[] b)

      Deprecated. Unused. Will be removed in Tomcat 8.0.x onwards.
      Returns the first instance of any byte that is not one 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 list of bytes to search for
      Returns:
        The position of the first instance a byte that is not in the list of bytes to search for or -1 if no such byte is 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–2023 Apache Software Foundation. All rights reserved.