# Buffer (Luaj API)
Date: 2015-04-29

## org.luaj.vm2
Class Buffer

    java.lang.Object
      org.luaj.vm2.Buffer

    public final class Buffer

extends java.lang.Object

String buffer for use in string library methods, optimized for production of StrValue instances.

The buffer can begin initially as a wrapped `LuaValue` and only when concatenation actually occurs are the bytes first copied.

To convert back to a `LuaValue` again, the function `value()` is used.

See Also:
  `LuaValue`, `LuaValue.buffer()`, `LuaString`

Constructor Summary
`Buffer()` Create buffer with default capacity
`Buffer(int initialCapacity)` Create buffer with specified initial capacity
`Buffer(LuaValue value)` Create buffer with specified initial value

Method Summary
`Buffer` `append(byte b)` Append a single byte to the buffer.
`Buffer` `append(LuaString str)` Append a `LuaString` to the buffer.
`Buffer` `append(LuaValue val)` Append a `LuaValue` to the buffer.
`Buffer` `append(java.lang.String str)` Append a Java String to the buffer.
`Buffer` `concatTo(LuaNumber lhs)` Concatenate this buffer onto a `LuaNumber`
`Buffer` `concatTo(LuaString lhs)` Concatenate this buffer onto a `LuaString`
`Buffer` `concatTo(LuaValue lhs)` Concatenate this buffer onto a `LuaValue`
`void` `makeroom(int nbefore, int nafter)` Ensure there is enough room before and after the bytes.
`Buffer` `prepend(LuaString s)` Concatenate bytes from a `LuaString` onto the front of this buffer
`Buffer` `setvalue(LuaValue value)` Set buffer contents as a `LuaValue`
`java.lang.String` `tojstring()` Convert the buffer to a Java String
`LuaString` `tostring()` Convert the buffer to a `LuaString`
`java.lang.String` `toString()` Convert the buffer to a Java String
`LuaValue` `value()` Get buffer contents as a `LuaValue`

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

Constructor Detail

### Buffer

    public Buffer()

  Create buffer with default capacity

  See Also:
    `DEFAULT_CAPACITY`

### Buffer

    public Buffer(int initialCapacity)

  Create buffer with specified initial capacity

  Parameters:
    `initialCapacity` - the initial capacity

### Buffer

    public Buffer(LuaValue value)

  Create buffer with specified initial value

  Parameters:
    `value` - the initial value

Method Detail

### value

    public LuaValue value()

  Get buffer contents as a `LuaValue`

  Returns:
    value as a `LuaValue`, converting as necessary

### setvalue

    public Buffer setvalue(LuaValue value)

  Set buffer contents as a `LuaValue`

  Parameters:
    `value` - value to set

### tostring

    public final LuaString tostring()

  Convert the buffer to a `LuaString`

  Returns:
    the value as a `LuaString`

### tojstring

    public java.lang.String tojstring()

  Convert the buffer to a Java String

  Returns:
    the value as a Java String

### toString

    public java.lang.String toString()

  Convert the buffer to a Java String

  Overrides:
    `toString` in class `java.lang.Object`
  Returns:
    the value as a Java String

### append

    public final Buffer append(byte b)

  Append a single byte to the buffer.

  Returns:
    `this` to allow call chaining

### append

    public final Buffer append(LuaValue val)

  Append a `LuaValue` to the buffer.

  Returns:
    `this` to allow call chaining

### append

    public final Buffer append(LuaString str)

  Append a `LuaString` to the buffer.

  Returns:
    `this` to allow call chaining

### append

    public final Buffer append(java.lang.String str)

  Append a Java String to the buffer. The Java string will be converted to bytes using the UTF8 encoding.

  Returns:
    `this` to allow call chaining
  See Also:
    `LuaString.encodeToUtf8(char[], int, byte[], int)`

### concatTo

    public Buffer concatTo(LuaValue lhs)

  Concatenate this buffer onto a `LuaValue`

  Parameters:
    `lhs` - the left-hand-side value onto which we are concatenating `this`
  Returns:
    `Buffer` for use in call chaining.

### concatTo

    public Buffer concatTo(LuaString lhs)

  Concatenate this buffer onto a `LuaString`

  Parameters:
    `lhs` - the left-hand-side value onto which we are concatenating `this`
  Returns:
    `Buffer` for use in call chaining.

### concatTo

    public Buffer concatTo(LuaNumber lhs)

  Concatenate this buffer onto a `LuaNumber`

  The `LuaNumber` will be converted to a string before concatenating.

  Parameters:
    `lhs` - the left-hand-side value onto which we are concatenating `this`
  Returns:
    `Buffer` for use in call chaining.

### prepend

    public Buffer prepend(LuaString s)

  Concatenate bytes from a `LuaString` onto the front of this buffer

  Parameters:
    `s` - the left-hand-side value which we will concatenate onto the front of `this`
  Returns:
    `Buffer` for use in call chaining.

### makeroom

    public final void makeroom(int nbefore,
                               int nafter)

  Ensure there is enough room before and after the bytes.

  Parameters:
    `nbefore` - number of unused bytes which must precede the data after this completes
    `nafter` - number of unused bytes which must follow the data after this completes

Copyright © 2007-2015 Luaj.org. All Rights Reserved.