org.vertx.java.core.buffer

## Class Buffer

- java.lang.Object
- - org.vertx.java.core.buffer.Buffer

-     public class Buffer
      extends java.lang.Object

  A Buffer represents a sequence of zero or more bytes that can be written to or read from, and which expands as necessary to accomodate any bytes written to it.

  There are two ways to write data to a Buffer: The first method involves methods that take the form `setXXX`. These methods write data into the buffer starting at the specified position. The position does not have to be inside data that has already been written to the buffer; the buffer will automatically expand to encompass the position plus any data that needs to be written. All positions are measured in bytes and start with zero.

  The second method involves methods that take the form `appendXXX`; these methods append data at the end of the buffer.

  Methods exist to both `set` and `append` all primitive types, `String`, `ByteBuffer` and other instances of Buffer.

  Data can be read from a buffer by invoking methods which take the form `getXXX`. These methods take a parameter representing the position in the Buffer from where to read data.

  Once a buffer has been written to a socket or other write stream, the same buffer instance can't be written again to another WriteStream.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Buffer()` Create an empty buffer
    `Buffer(byte[] bytes)` Create a new Buffer that contains the contents of a `byte[]`
    `Buffer(org.jboss.netty.buffer.ChannelBuffer buffer)` Create a new Buffer from a Netty `ChannelBuffer` instance.
    `Buffer(int initialSizeHint)` Creates a new empty Buffer that is expected to have a size of `initialSizeHint` after data has been written to it.
    `Buffer(java.lang.String str)` Create a new Buffer that contains the contents of `String str` encoded with UTF-8 encoding
    `Buffer(java.lang.String str, java.lang.String enc)` Create a new Buffer that contains the contents of a `String str` encoded according to the encoding `enc`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `Buffer` | `appendBuffer(Buffer buff)` Appends the specified `Buffer` to the end of this Buffer.
    `Buffer` | `appendByte(byte b)` Appends the specified `byte` to the end of the Buffer.
    `Buffer` | `appendBytes(byte[] bytes)` Appends the specified `byte[]` to the end of the Buffer.
    `Buffer` | `appendDouble(double d)` Appends the specified `double` to the end of the Buffer.
    `Buffer` | `appendFloat(float f)` Appends the specified `float` to the end of the Buffer.
    `Buffer` | `appendInt(int i)` Appends the specified `int` to the end of the Buffer.
    `Buffer` | `appendLong(long l)` Appends the specified `long` to the end of the Buffer.
    `Buffer` | `appendShort(short s)` Appends the specified `short` to the end of the Buffer.The buffer will expand as necessary to accomodate any bytes written.
    `Buffer` | `appendString(java.lang.String str)` Appends the specified `String str` to the end of the Buffer with UTF-8 encoding.
    `Buffer` | `appendString(java.lang.String str, java.lang.String enc)` Appends the specified `String` to the end of the Buffer with the encoding as specified by `enc`.
    `Buffer` | `copy()` Returns a copy of the entire Buffer.
    `boolean` | `equals(java.lang.Object o)`
    `Buffer` | `getBuffer(int start, int end)` Returns a copy of a sub-sequence the Buffer as a `Buffer` starting at position `start` and ending at position `end - 1`
    `byte` | `getByte(int pos)` Returns the `byte` at position `pos` in the Buffer.
    `byte[]` | `getBytes()` Returns a copy of the entire Buffer as a `byte[]`
    `byte[]` | `getBytes(int start, int end)` Returns a copy of a sub-sequence the Buffer as a `byte[]` starting at position `start` and ending at position `end - 1`
    `org.jboss.netty.buffer.ChannelBuffer` | `getChannelBuffer()` Returns the Buffer as a Netty `ChannelBuffer`.
    `double` | `getDouble(int pos)` Returns the `double` at position `pos` in the Buffer.
    `float` | `getFloat(int pos)` Returns the `float` at position `pos` in the Buffer.
    `int` | `getInt(int pos)` Returns the `int` at position `pos` in the Buffer.
    `long` | `getLong(int pos)` Returns the `long` at position `pos` in the Buffer.
    `short` | `getShort(int pos)` Returns the `short` at position `pos` in the Buffer.
    `java.lang.String` | `getString(int start, int end)` Returns a copy of a sub-sequence the Buffer as a `String` starting at position `start` and ending at position `end - 1` interpreted as a String in UTF-8 encoding
    `java.lang.String` | `getString(int start, int end, java.lang.String enc)` Returns a copy of a sub-sequence the Buffer as a `String` starting at position `start` and ending at position `end - 1` interpreted as a String in the specified encoding
    `int` | `length()` Returns the length of the buffer, measured in bytes.
    `Buffer` | `setBuffer(int pos, Buffer b)` Sets the bytes at position `pos` in the Buffer to the bytes represented by the `Buffer b`.
    `Buffer` | `setByte(int pos, byte b)` Sets the `byte` at position `pos` in the Buffer to the value `b`.
    `Buffer` | `setBytes(int pos, byte[] b)` Sets the bytes at position `pos` in the Buffer to the bytes represented by the `byte[] b`.
    `Buffer` | `setBytes(int pos, java.nio.ByteBuffer b)` Sets the bytes at position `pos` in the Buffer to the bytes represented by the `ByteBuffer b`.
    `Buffer` | `setDouble(int pos, double d)` Sets the `double` at position `pos` in the Buffer to the value `d`.
    `Buffer` | `setFloat(int pos, float f)` Sets the `float` at position `pos` in the Buffer to the value `f`.
    `Buffer` | `setInt(int pos, int i)` Sets the `int` at position `pos` in the Buffer to the value `i`.
    `Buffer` | `setLong(int pos, long l)` Sets the `long` at position `pos` in the Buffer to the value `l`.
    `Buffer` | `setShort(int pos, short s)` Sets the `short` at position `pos` in the Buffer to the value `s`.
    `Buffer` | `setString(int pos, java.lang.String str)` Sets the bytes at position `pos` in the Buffer to the value of `str` endoded in UTF-8.
    `Buffer` | `setString(int pos, java.lang.String str, java.lang.String enc)` Sets the bytes at position `pos` in the Buffer to the value of `str` encoded in encoding `enc`.
    `java.lang.String` | `toString()` Returns a `String` represention of the Buffer assuming it contains a `String` encoding in UTF-8
    `java.lang.String` | `toString(java.lang.String enc)` Returns a `String` represention of the Buffer with the encoding specified by `enc`

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

      `clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### Buffer

          public Buffer()

      Create an empty buffer

    - #### Buffer

          public Buffer(int initialSizeHint)

      Creates a new empty Buffer that is expected to have a size of `initialSizeHint` after data has been written to it.

      Please note that `length` of the Buffer immediately after creation will be zero.

      The `initialSizeHint` is merely a hint to the system for how much memory to initially allocate to the buffer to prevent excessive automatic re-allocations as data is written to it.

    - #### Buffer

          public Buffer(byte[] bytes)

      Create a new Buffer that contains the contents of a `byte[]`

    - #### Buffer

          public Buffer(java.lang.String str,
                java.lang.String enc)

      Create a new Buffer that contains the contents of a `String str` encoded according to the encoding `enc`

    - #### Buffer

          public Buffer(java.lang.String str)

      Create a new Buffer that contains the contents of `String str` encoded with UTF-8 encoding

    - #### Buffer

          public Buffer(org.jboss.netty.buffer.ChannelBuffer buffer)

      Create a new Buffer from a Netty `ChannelBuffer` instance. This method is meant for internal use only.

  - ### Method Detail

    - #### toString

          public java.lang.String toString()

      Returns a `String` represention of the Buffer assuming it contains a `String` encoding in UTF-8
      Overrides:
        `toString` in class `java.lang.Object`

    - #### toString

          public java.lang.String toString(java.lang.String enc)

      Returns a `String` represention of the Buffer with the encoding specified by `enc`

    - #### getByte

          public byte getByte(int pos)

      Returns the `byte` at position `pos` in the Buffer.
      Throws:
        `java.lang.IndexOutOfBoundsException` - if the specified `pos` is less than `0` or `pos + 1` is greater than the length of the Buffer.

    - #### getInt

          public int getInt(int pos)

      Returns the `int` at position `pos` in the Buffer.
      Throws:
        `java.lang.IndexOutOfBoundsException` - if the specified `pos` is less than `0` or `pos + 4` is greater than the length of the Buffer.

    - #### getLong

          public long getLong(int pos)

      Returns the `long` at position `pos` in the Buffer.
      Throws:
        `java.lang.IndexOutOfBoundsException` - if the specified `pos` is less than `0` or `pos + 8` is greater than the length of the Buffer.

    - #### getDouble

          public double getDouble(int pos)

      Returns the `double` at position `pos` in the Buffer.
      Throws:
        `java.lang.IndexOutOfBoundsException` - if the specified `pos` is less than `0` or `pos + 8` is greater than the length of the Buffer.

    - #### getFloat

          public float getFloat(int pos)

      Returns the `float` at position `pos` in the Buffer.
      Throws:
        `java.lang.IndexOutOfBoundsException` - if the specified `pos` is less than `0` or `pos + 4` is greater than the length of the Buffer.

    - #### getShort

          public short getShort(int pos)

      Returns the `short` at position `pos` in the Buffer.
      Throws:
        `java.lang.IndexOutOfBoundsException` - if the specified `pos` is less than `0` or `pos + 2` is greater than the length of the Buffer.

    - #### getBytes

          public byte[] getBytes()

      Returns a copy of the entire Buffer as a `byte[]`

    - #### getBytes

          public byte[] getBytes(int start,
                        int end)

      Returns a copy of a sub-sequence the Buffer as a `byte[]` starting at position `start` and ending at position `end - 1`

    - #### getBuffer

          public Buffer getBuffer(int start,
                         int end)

      Returns a copy of a sub-sequence the Buffer as a `Buffer` starting at position `start` and ending at position `end - 1`

    - #### getString

          public java.lang.String getString(int start,
                                   int end,
                                   java.lang.String enc)

      Returns a copy of a sub-sequence the Buffer as a `String` starting at position `start` and ending at position `end - 1` interpreted as a String in the specified encoding

    - #### getString

          public java.lang.String getString(int start,
                                   int end)

      Returns a copy of a sub-sequence the Buffer as a `String` starting at position `start` and ending at position `end - 1` interpreted as a String in UTF-8 encoding

    - #### appendBuffer

          public Buffer appendBuffer(Buffer buff)

      Appends the specified `Buffer` to the end of this Buffer. The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendBytes

          public Buffer appendBytes(byte[] bytes)

      Appends the specified `byte[]` to the end of the Buffer. The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendByte

          public Buffer appendByte(byte b)

      Appends the specified `byte` to the end of the Buffer. The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendInt

          public Buffer appendInt(int i)

      Appends the specified `int` to the end of the Buffer. The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendLong

          public Buffer appendLong(long l)

      Appends the specified `long` to the end of the Buffer. The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendShort

          public Buffer appendShort(short s)

      Appends the specified `short` to the end of the Buffer.The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendFloat

          public Buffer appendFloat(float f)

      Appends the specified `float` to the end of the Buffer. The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendDouble

          public Buffer appendDouble(double d)

      Appends the specified `double` to the end of the Buffer. The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendString

          public Buffer appendString(java.lang.String str,
                            java.lang.String enc)

      Appends the specified `String` to the end of the Buffer with the encoding as specified by `enc`.

      The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together.

    - #### appendString

          public Buffer appendString(java.lang.String str)

      Appends the specified `String str` to the end of the Buffer with UTF-8 encoding.

      The buffer will expand as necessary to accomodate any bytes written.

      Returns a reference to `this` so multiple operations can be appended together

    - #### setByte

          public Buffer setByte(int pos,
                       byte b)

      Sets the `byte` at position `pos` in the Buffer to the value `b`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setInt

          public Buffer setInt(int pos,
                      int i)

      Sets the `int` at position `pos` in the Buffer to the value `i`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setLong

          public Buffer setLong(int pos,
                       long l)

      Sets the `long` at position `pos` in the Buffer to the value `l`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setDouble

          public Buffer setDouble(int pos,
                         double d)

      Sets the `double` at position `pos` in the Buffer to the value `d`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setFloat

          public Buffer setFloat(int pos,
                        float f)

      Sets the `float` at position `pos` in the Buffer to the value `f`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setShort

          public Buffer setShort(int pos,
                        short s)

      Sets the `short` at position `pos` in the Buffer to the value `s`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setBuffer

          public Buffer setBuffer(int pos,
                         Buffer b)

      Sets the bytes at position `pos` in the Buffer to the bytes represented by the `Buffer b`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setBytes

          public Buffer setBytes(int pos,
                        java.nio.ByteBuffer b)

      Sets the bytes at position `pos` in the Buffer to the bytes represented by the `ByteBuffer b`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setBytes

          public Buffer setBytes(int pos,
                        byte[] b)

      Sets the bytes at position `pos` in the Buffer to the bytes represented by the `byte[] b`.

      The buffer will expand as necessary to accomodate any value written.

    - #### setString

          public Buffer setString(int pos,
                         java.lang.String str)

      Sets the bytes at position `pos` in the Buffer to the value of `str` endoded in UTF-8.

      The buffer will expand as necessary to accomodate any value written.

    - #### setString

          public Buffer setString(int pos,
                         java.lang.String str,
                         java.lang.String enc)

      Sets the bytes at position `pos` in the Buffer to the value of `str` encoded in encoding `enc`.

      The buffer will expand as necessary to accomodate any value written.

    - #### length

          public int length()

      Returns the length of the buffer, measured in bytes. All positions are indexed from zero.

    - #### copy

          public Buffer copy()

      Returns a copy of the entire Buffer.

    - #### getChannelBuffer

          public org.jboss.netty.buffer.ChannelBuffer getChannelBuffer()

      Returns the Buffer as a Netty `ChannelBuffer`.

      This method is meant for internal use only.

    - #### equals

          public boolean equals(java.lang.Object o)

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