Class ClickHouseByteBuffer

java.lang.Object
com.clickhouse.data.ClickHouseByteBuffer
All Implemented Interfaces:
Serializable

public class ClickHouseByteBuffer extends Object implements Serializable
This class represents a slice of a byte array. It holds a reference to a byte array, and it uses position() and length() to define the slice. You may think of it as a lite version of ByteBuffer.
See Also:
  • Field Details

    • EMPTY_BYTES

      public static final byte[] EMPTY_BYTES
      Empty byte array.
    • EMPTY_BUFFER

      public static final ByteBuffer EMPTY_BUFFER
      Empty and read-only byte buffer.
    • array

      protected byte[] array
    • position

      protected int position
    • length

      protected int length
  • Constructor Details

    • ClickHouseByteBuffer

      protected ClickHouseByteBuffer(byte[] bytes, int offset, int length)
      Default constructor.
      Parameters:
      bytes - non-null byte array
      offset - start position
      length - length of bytes
  • Method Details

    • newInstance

      public static ClickHouseByteBuffer newInstance()
      Creates an empty byte buffer.
      Returns:
      empty byte buffer
    • of

      public static ClickHouseByteBuffer of(ByteBuffer buffer)
      Wraps given byte buffer.
      Parameters:
      buffer - byte buffer
      Returns:
      non-null wrapped byte buffer
    • of

      public static ClickHouseByteBuffer of(byte[] bytes)
      Wraps given byte array as byte buffer.
      Parameters:
      bytes - byte array
      Returns:
      non-null byte buffer
    • of

      public static ClickHouseByteBuffer of(byte[] bytes, int offset, int length)
      Wraps given byte array as byte buffer.
      Parameters:
      bytes - byte array
      offset - start position
      length - length
      Returns:
      non-null byte buffer
    • of

      public static ClickHouseByteBuffer of(List<byte[]> list, int offset, int length)
      Wraps given byte arrays as byte buffer.
      Parameters:
      list - list of non-null byte arrays
      offset - offset
      length - length
      Returns:
      non-null byte buffer
    • asBigInteger

      public BigInteger asBigInteger()
      Converts all bytes(little-endian) to signed big integer. Same as getBigInteger(0, length(), false).
      Returns:
      non-null signed big integer
    • asBoolean

      public boolean asBoolean()
    • asBooleanArray

      public boolean[] asBooleanArray()
    • asDouble

      public double asDouble()
    • asDoubleArray

      public double[] asDoubleArray()
    • asFloat

      public float asFloat()
    • asFloatArray

      public float[] asFloatArray()
    • asUnsignedBigInteger

      public BigInteger asUnsignedBigInteger()
      Converts all bytes(little-endian) to unsigned big integer. Same as getBigInteger(0, length(), true).
      Returns:
      non-null unsigned big integer
    • getBigInteger

      public BigInteger getBigInteger(int offset, int byteLength, boolean unsigned)
      Converts byteLength bytes(little-endian) starting from position() + offset to big integer.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      byteLength - bytes to convert
      unsigned - true if it's unsigned big integer; false otherwise
      Returns:
      non-null big integer
    • getBoolean

      public boolean getBoolean(int offset)
      Converts the byte at position() + offset in array to boolean.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      boolean
    • getDouble

      public double getDouble(int offset)
      Converts 8 bytes(little-endian) starting from position() + offset to double.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      double
    • getFloat

      public float getFloat(int offset)
      Converts 4 bytes(little-endian) starting from position() + offset to float.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      float
    • getInteger

      public int getInteger(int offset)
      Converts 4 bytes(little-endian) starting from position() + offset to signed integer.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      signed integer
    • getLong

      public long getLong(int offset)
      Converts 8 bytes(little-endian) starting from position() + offset to signed long.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      signed long
    • getShort

      public short getShort(int offset)
      Converts 2 bytes(little-endian) starting from position() + offset to signed short.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      signed short
    • getUnsignedInteger

      public long getUnsignedInteger(int offset)
      Converts 4 bytes(little-endian) starting from position() + offset to unsigned integer.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      unsigned integer
    • getUnsignedLong

      public BigInteger getUnsignedLong(int offset)
      Converts 8 bytes(little-endian) starting from position() + offset to unsigned long.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      non-null unsigned long
    • getUnsignedShort

      public int getUnsignedShort(int offset)
      Converts 2 bytes(little-endian) starting from position() + offset to unsigned short.
      Parameters:
      offset - zero-based relative offset, 1 means the second byte starting from position()
      Returns:
      unsigned short
    • asBigIntegerArray

      public BigInteger[] asBigIntegerArray(int byteLength, boolean unsigned)
    • asInteger

      public int asInteger()
    • asLong

      public long asLong()
    • asShort

      public short asShort()
    • asIntegerArray

      public int[] asIntegerArray()
    • asLongArray

      public long[] asLongArray()
    • asShortArray

      public short[] asShortArray()
    • asUnsignedInteger

      public long asUnsignedInteger()
    • asUnsignedLong

      public BigInteger asUnsignedLong()
    • asUnsignedShort

      public int asUnsignedShort()
    • asUnsignedIntegerArray

      public long[] asUnsignedIntegerArray()
    • asUnsignedShortArray

      public int[] asUnsignedShortArray()
    • asUuid

      public UUID asUuid()
    • getUuid

      public UUID getUuid(int offset)
    • asAsciiString

      public String asAsciiString()
      Converts to ASCII string.
      Returns:
      non-null ASCII string
    • asString

      public String asString(Charset charset)
      Converts to string using given charset.
      Parameters:
      charset - optional charset, null is treated as StandardCharsets.UTF_8
      Returns:
      non-null string
    • asUnicodeString

      public String asUnicodeString()
      Converts to UTF-8 string.
      Returns:
      non-null UTF-8 string
    • compact

      public ClickHouseByteBuffer compact()
      Compacts byte array by creating a new copy with exact same length but position changed to zero. It does nothing when position is zero and length is same as bytes.length.
      Returns:
      this byte buffer
    • isCompact

      public boolean isCompact()
      Checks whether the buffer is compact or not. A buffer is compact when position points to zero and length equals to array().length.
      Returns:
      true if the buffer is compact; false otherwise
    • isEmpty

      public boolean isEmpty()
      Checks whether the buffer is empty or not. Please pay attention that this will return true when length() is zero, even array() may return an non-empty byte array.
      Returns:
      true if the buffer is empty; false otherwise
    • match

      public boolean match(byte[] bytes)
      Checks if the byte buffer is same as the given byte array.
      Parameters:
      bytes - bytes to check
      Returns:
      true if the byte buffer is same as the given byte array; false otherwise
    • reset

      public ClickHouseByteBuffer reset()
      Resets the buffer to empty.
      Returns:
      this byte buffer
    • reverse

      public ClickHouseByteBuffer reverse()
      Reverses the byte array.
      Returns:
      this byte buffer
    • reverse

      public ClickHouseByteBuffer reverse(int offset, int length)
      Reverses the byte array.
      Parameters:
      offset - start position
      length - bytes to reserve
      Returns:
      this byte buffer
    • slice

      public ClickHouseByteBuffer slice(int offset, int length)
      Gets slice of the byte buffer.
      Parameters:
      offset - offset zero-based relative offset, 1 means the second byte starting from position()
      length - length of the slice
      Returns:
      non-null slice of the byte buffer(backed by the same byte array but with different position and length)
    • update

      public ClickHouseByteBuffer update(ByteBuffer buffer)
      Updates buffer.
      Parameters:
      buffer - byte buffer
      Returns:
      this byte buffer
    • update

      public ClickHouseByteBuffer update(byte[] bytes)
      Updates buffer.
      Parameters:
      bytes - byte array, null is same as empty byte array
      Returns:
      this byte buffer
    • update

      public ClickHouseByteBuffer update(byte[] bytes, int offset, int length)
      Updates buffer.
      Parameters:
      bytes - byte array, null is same as empty byte array
      offset - start position
      length - length of bytes
      Returns:
      this byte buffer
    • update

      public ClickHouseByteBuffer update(List<byte[]> list, int offset, int length)
      Updates buffer.
      Parameters:
      list - list of byte arrays, null is same as empty byte array
      offset - start position
      length - length of bytes
      Returns:
      this byte buffer
    • array

      public byte[] array()
      Gets byte array.
      Returns:
      non-null byte array
    • copy

      public ClickHouseByteBuffer copy(boolean deep)
      Creates a copy of the current byte buffer.
      Parameters:
      deep - true to copy the underlying byte array; false to reuse
      Returns:
      non-null copy of the current byte buffer
    • firstByte

      public byte firstByte()
    • getByte

      public byte getByte(int offset)
    • lastByte

      public byte lastByte()
    • position

      public int position()
      Gets start position.
      Returns:
      start position
    • position

      public ClickHouseByteBuffer position(int newPosition)
      Sets new position.
      Parameters:
      newPosition - new position, which should be always less than limit()
      Returns:
      this byte buffer
    • length

      public int length()
      Gets length of bytes.
      Returns:
      length of bytes
    • limit

      public int limit()
      Gets end position.
      Returns:
      end position
    • setLength

      public void setLength(int newLength)
      Sets new length.
      Parameters:
      newLength - new length
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object