Class ScriptValue.BufferType

java.lang.Object
com.blackrook.rookscript.ScriptValue.BufferType
All Implemented Interfaces:
Iterable<ScriptIteratorType.IteratorPair>
Enclosing class:
ScriptValue

public static class ScriptValue.BufferType extends Object implements Iterable<ScriptIteratorType.IteratorPair>
The class used for a buffer of bytes.
  • Method Details

    • setPosition

      public void setPosition(int position)
      Sets the buffer's current cursor position.
      Parameters:
      position - the new position.
    • getPosition

      public int getPosition()
      Returns:
      the buffer's current cursor position.
    • setSize

      public void setSize(int size)
      Sets the size of this buffer in bytes. If this is resized, the buffer's data is kept.
      Parameters:
      size - the new buffer size.
    • setByteOrder

      public void setByteOrder(ByteOrder byteOrder)
      Sets the byte order of this buffer.
      Parameters:
      byteOrder - the new byte order.
    • getByteOrder

      public ByteOrder getByteOrder()
      Gets the byte order of this buffer.
      Returns:
      order the current byte order.
    • readBytes

      public int readBytes(Integer index, DataInput in, int length) throws IOException
      Reads bytes from an input stream into this buffer. If null is passed in as the index, the buffer's cursor position is advanced by the length.
      Parameters:
      index - the destination index (or null for current position).
      in - the input stream to read from.
      length - the amount of bytes to read.
      Returns:
      the amount of bytes actually read. May be less than length (see InputStream.read(byte[], int, int)).
      Throws:
      IOException - if a read error occurs.
      IndexOutOfBoundsException - if index + length exceeds the buffer length.
    • writeBytes

      public int writeBytes(Integer index, DataOutput out, int length) throws IOException
      Writes bytes from this buffer into an input stream. If null is passed in as the index, the buffer's cursor position is advanced by the amount written.
      Parameters:
      index - the destination index (or null for current position).
      out - the output stream to write to.
      length - the maximum amount of bytes to write.
      Returns:
      the amount of bytes actually written. May be less than length.
      Throws:
      IOException - if a write error occurs.
      IndexOutOfBoundsException - if index + length exceeds the buffer length.
    • readBytes

      public int readBytes(Integer index, ScriptValue.BufferType buffer, int length)
      Reads bytes from another buffer into this one. This relies on the other buffer's position to be set to where the read should occur. If null is passed in as the index, the buffer's cursor position is advanced by the length.
      Parameters:
      index - the destination index (or null for current position).
      buffer - the source buffer.
      length - the amount of bytes to read.
      Returns:
      the amount of bytes actually read.
      Throws:
      IndexOutOfBoundsException - if index exceeds this buffer's length.
    • readBytes

      public int readBytes(Integer index, ScriptValue.BufferType buffer, Integer offset, int length)
      Reads bytes from another buffer into this one. This relies on the other buffer's position to be set to where the read should occur. If null is passed in as the index, the buffer's cursor position is advanced by the length. If null is passed in as the offset, the source buffer's cursor position is advanced by the length.
      Parameters:
      index - the destination index (or null for current position).
      buffer - the source buffer.
      offset - the offset into the source buffer (or null for its current position).
      length - the amount of bytes to read.
      Returns:
      the amount of bytes actually read (length).
      Throws:
      IndexOutOfBoundsException - if index + length exceeds this buffer's length.
    • readBytes

      public int readBytes(Integer index, byte[] bytes, int offset, int length)
      Reads bytes from a byte array into this buffer. If null is passed in as the index, the buffer's cursor position is advanced by the length.
      Parameters:
      index - the destination index (or null for current position).
      bytes - the source array.
      offset - the offset into the provided array to start the read from.
      length - the amount of bytes to read.
      Returns:
      the amount of bytes read (length).
      Throws:
      IndexOutOfBoundsException - if index + length exceeds this buffer's length or index + length exceeds the length of the provided buffer.
    • putBytes

      public int putBytes(Integer index, byte value, int length)
      Fills bytes using a value. If null is passed in as the index, the buffer's cursor position is advanced by the length.
      Parameters:
      index - the destination index (or null for current position).
      value - the fill value.
      length - the amount of bytes to add.
      Returns:
      the amount of bytes set (length).
      Throws:
      ArrayIndexOutOfBoundsException - if index + length exceeds this buffer's length or index + length exceeds the length of the provided buffer.
    • putByte

      public void putByte(Integer index, byte value)
      Sets a byte value. If null is passed in as the index, the buffer's cursor position is advanced by 1.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getByte

      public byte getByte(Integer index)
      Gets a byte value. If null is passed in as the index, the buffer's cursor position is advanced by 1.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putShort

      public void putShort(Integer index, short value)
      Sets a short value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 2.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getShort

      public short getShort(Integer index)
      Gets a short value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 2.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putUnsignedShort

      public void putUnsignedShort(Integer index, int value)
      Sets an unsigned short value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 2.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getUnsignedShort

      public int getUnsignedShort(Integer index)
      Gets an unsigned short value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 2.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putInteger

      public void putInteger(Integer index, int value)
      Sets an integer value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 4.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getInteger

      public int getInteger(Integer index)
      Gets an integer value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 4.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putUnsignedInteger

      public void putUnsignedInteger(Integer index, long value)
      Sets an unsigned integer value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 4.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getUnsignedInteger

      public long getUnsignedInteger(Integer index)
      Gets an unsigned integer value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 4.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putFloat

      public void putFloat(Integer index, float value)
      Sets a float value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 4.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getFloat

      public float getFloat(Integer index)
      Gets a float value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 4.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putLong

      public void putLong(Integer index, long value)
      Sets a long value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 8.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getLong

      public long getLong(Integer index)
      Gets a long value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 8.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putDouble

      public void putDouble(Integer index, double value)
      Sets a double value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 8.
      Parameters:
      index - the destination index (or null for current position).
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getDouble

      public double getDouble(Integer index)
      Gets a double value. Pays attention to current byte order. If null is passed in as the index, the buffer's cursor position is advanced by 8.
      Parameters:
      index - the source index (or null for current position).
      Returns:
      the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • putString

      public void putString(Integer index, Charset charset, String value)
      Sets a string value. If null is passed in as the index, the buffer's cursor position is advanced by the amount of bytes written.
      Parameters:
      index - the destination index (or null for current position).
      charset - the encoding charset.
      value - the value.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • getString

      public String getString(Integer index, Charset charset, int length)
      Sets a string value. If null is passed in as the index, the buffer's cursor position is advanced by the length.
      Parameters:
      index - the destination index (or null for current position).
      charset - the encoding charset.
      length - the amount of bytes to use to create a string.
      Returns:
      the decoded string.
      Throws:
      ArrayIndexOutOfBoundsException - if the index is out-of-bounds.
    • size

      public int size()
      Returns:
      the size of this buffer in bytes.
    • getInputStream

      public InputStream getInputStream()
      Returns:
      an open input stream for reading from the buffer's current cursor.
    • getOutputStream

      public OutputStream getOutputStream()
      Returns:
      an open output stream for writing from the buffer's current cursor.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • iterator

      public ScriptIteratorType iterator()
      Specified by:
      iterator in interface Iterable<ScriptIteratorType.IteratorPair>