Class BinaryStreamReader

java.lang.Object
com.clickhouse.client.api.data_formats.internal.BinaryStreamReader

public class BinaryStreamReader extends Object
This class is not thread safe and should not be shared between multiple threads. Internally it may use a shared buffer to read data from the input stream. It is done mainly to reduce extra memory allocations for reading numbers.
  • Field Details

  • Constructor Details

    • BinaryStreamReader

      public BinaryStreamReader(InputStream input, TimeZone timeZone, org.slf4j.Logger log, BinaryStreamReader.ByteBufferAllocator bufferAllocator, boolean jsonAsString, Map<ClickHouseDataType,Class<?>> typeHintMapping, boolean binaryStringSupport)
      Createa a BinaryStreamReader instance that will use the provided buffer allocator.
      Parameters:
      input - - source of raw data in a suitable format
      timeZone - - timezone to use for date and datetime values
      log - - logger
      bufferAllocator - - byte buffer allocator
      jsonAsString - - use string to serialize/deserialize JSON columns
      typeHintMapping - - what type use as hint if hint is not set or may not be known.
      binaryStringSupport - - when true, top-level String/FixedString columns are read as StringValue preserving raw bytes; nested string values stay String.
  • Method Details

    • readValue

      public <T> T readValue(ClickHouseColumn column) throws IOException
      Reads a value from the internal input stream.
      Type Parameters:
      T - - target type of the value
      Parameters:
      column - - column information
      Returns:
      value
      Throws:
      IOException - when IO error occurs
    • readValue

      public <T> T readValue(ClickHouseColumn column, Class<?> typeHint) throws IOException
      Reads a value from the internal input stream. Method will use type hint to do smarter conversion if possible. For example, all datetime values are of ZonedDateTime; if a type hint is LocalDateTime then ZonedDateTime.toLocalDateTime() .
      Type Parameters:
      T - - target type of the value
      Parameters:
      column - - column information
      typeHint - - type hint
      Returns:
      value
      Throws:
      IOException - when IO error occurs
    • readShortLE

      public short readShortLE() throws IOException
      Read a short value in little-endian from the internal input stream.
      Returns:
      short value
      Throws:
      IOException - when IO error occurs
    • readShortLE

      public static short readShortLE(InputStream input, byte[] buff) throws IOException
      Reads a little-endian short from input stream. Uses buff to receive data from the input stream.
      Parameters:
      input - - source of bytes
      buff - - buffer to store data
      Returns:
      short value
      Throws:
      IOException - when IO error occurs
    • readIntLE

      public int readIntLE() throws IOException
      Reads an int value in little-endian from the internal input stream.
      Returns:
      int value
      Throws:
      IOException - when IO error occurs
    • readIntLE

      public static int readIntLE(InputStream input, byte[] buff) throws IOException
      Reads a little-endian int from input stream. Uses buff to receive data from the input stream.
      Parameters:
      input - - source of bytes
      buff - - buffer to store data
      Returns:
      - int value
      Throws:
      IOException - when IO error occurs
    • readLongLE

      public long readLongLE() throws IOException
      Reads a long value in little-endian from the internal input stream.
      Returns:
      long value
      Throws:
      IOException - when IO error occurs
    • readLongLE

      public static long readLongLE(InputStream input, byte[] buff) throws IOException
      Reads a little-endian long from input stream. Uses buff to receive data from the input stream.
      Parameters:
      input - - source of bytes
      buff - - buffer to store data
      Returns:
      - long value
      Throws:
      IOException - when IO error occurs
    • readByte

      public byte readByte() throws IOException
      Read byte from the internal input stream.
      Returns:
      byte value
      Throws:
      IOException - when IO error occurs
    • readUnsignedByte

      public short readUnsignedByte() throws IOException
      Reads an unsigned byte value from the internal input stream.
      Returns:
      unsigned byte value
      Throws:
      IOException - when IO error occurs
    • readUnsignedShortLE

      public int readUnsignedShortLE() throws IOException
      Reads an unsigned short value from the internal input stream.
      Returns:
      unsigned short value
      Throws:
      IOException - when IO error occurs
    • readUnsignedShortLE

      public static int readUnsignedShortLE(InputStream input, byte[] buff) throws IOException
      Reads a little-endian unsigned short from input stream. Uses buff to receive data from the input stream.
      Parameters:
      input - - source of bytes
      buff - - buffer to store data
      Returns:
      - unsigned short value
      Throws:
      IOException
    • readUnsignedIntLE

      public long readUnsignedIntLE() throws IOException
      Reads an unsigned int value in little-endian from the internal input stream.
      Returns:
      unsigned int value
      Throws:
      IOException - when IO error occurs
    • readUnsignedIntLE

      public static long readUnsignedIntLE(InputStream input, byte[] buff) throws IOException
      Reads a little-endian unsigned int from input stream. Uses buff to receive data from the input stream.
      Parameters:
      input - - source of bytes
      buff - - buffer to store data
      Returns:
      - unsigned int value
      Throws:
      IOException - when IO error occurs
    • readBigIntegerLE

      public BigInteger readBigIntegerLE(int len, boolean unsigned) throws IOException
      Reads a big integer value in little-endian from the internal input stream.
      Parameters:
      len - - number of bytes to read
      unsigned - - whether the value is unsigned
      Returns:
      big integer value
      Throws:
      IOException - when IO error occurs
    • readBigIntegerLE

      public static BigInteger readBigIntegerLE(InputStream input, byte[] buff, int len, boolean unsigned) throws IOException
      Reads a little-endian big integer from input stream. Uses buff to receive data from the input stream.
      Parameters:
      input - - source of bytes
      buff - - buffer to store data
      len - - number of bytes to read
      unsigned - - whether the value is unsigned
      Returns:
      - big integer value
      Throws:
      IOException
    • readFloatLE

      public float readFloatLE() throws IOException
      Reads a decimal value from the internal input stream.
      Returns:
      decimal value
      Throws:
      IOException - when IO error occurs
    • readDoubleLE

      public double readDoubleLE() throws IOException
      Reads a double value from the internal input stream.
      Returns:
      double value
      Throws:
      IOException - when IO error occurs
    • readDecimal

      public BigDecimal readDecimal(int precision, int scale) throws IOException
      Reads a decimal value from the internal input stream.
      Parameters:
      precision - - precision of the decimal value
      scale - - scale of the decimal value
      Returns:
      decimal value
      Throws:
      IOException - when IO error occurs
    • readNBytes

      public static byte[] readNBytes(InputStream inputStream, int len) throws IOException
      Throws:
      IOException
    • readNBytes

      public static byte[] readNBytes(InputStream inputStream, byte[] buffer, int offset, int len) throws IOException
      Reads len bytes from input stream to buffer.
      Parameters:
      inputStream - - source of bytes
      buffer - - target buffer
      offset - - target buffer offset
      len - - number of bytes to read
      Returns:
      target buffer
      Throws:
      IOException
    • readNBytesLE

      public static byte[] readNBytesLE(InputStream input, byte[] buffer, int offset, int len) throws IOException
      Reads len bytes from input stream to buffer in little-endian order.
      Parameters:
      input - - source of bytes
      buffer - - target buffer
      offset - - target buffer offset
      len - - number of bytes to read
      Returns:
      - target buffer
      Throws:
      IOException
    • readArray

      Reads a array into an ArrayValue object.
      Parameters:
      column - - column information
      Returns:
      array value
      Throws:
      IOException - when IO error occurs
    • readArrayItem

      public BinaryStreamReader.ArrayValue readArrayItem(ClickHouseColumn itemTypeColumn, int len) throws IOException
      Throws:
      IOException
    • skipValue

      public void skipValue(ClickHouseColumn column) throws IOException
      Throws:
      IOException
    • readMap

      public Map<?,?> readMap(ClickHouseColumn column) throws IOException
      Reads a map.
      Parameters:
      column - - column information
      Returns:
      a map
      Throws:
      IOException - when IO error occurs
    • readTuple

      public Object[] readTuple(ClickHouseColumn column) throws IOException
      Reads a tuple.
      Parameters:
      column - - column information
      Returns:
      a tuple
      Throws:
      IOException - when IO error occurs
    • readNested

      public BinaryStreamReader.ArrayValue readNested(ClickHouseColumn column) throws IOException
      Reads a nested into an ArrayValue object.
      Parameters:
      column - - column information
      Returns:
      array value
      Throws:
      IOException - when IO error occurs
    • readVariant

      public Object readVariant(ClickHouseColumn column) throws IOException
      Throws:
      IOException
    • readGeoPoint

      public double[] readGeoPoint() throws IOException
      Reads a GEO point as an array of two doubles what represents coordinates (X, Y).
      Returns:
      X, Y coordinates
      Throws:
      IOException - when IO error occurs
    • readGeoRing

      public double[][] readGeoRing() throws IOException
      Reads a GEO ring as an array of points.
      Returns:
      array of points
      Throws:
      IOException - when IO error occurs
    • readGeoPolygon

      public double[][][] readGeoPolygon() throws IOException
      Reads a GEO polygon as an array of rings.
      Returns:
      polygon
      Throws:
      IOException - when IO error occurs
    • readVarInt

      public static int readVarInt(InputStream input) throws IOException
      Reads a varint from input stream.
      Returns:
      varint
      Throws:
      IOException - when failed to read value from input stream or reached end of the stream
    • readDate

      public static ZonedDateTime readDate(InputStream input, byte[] buff, TimeZone tz) throws IOException
      Reads a date from input stream.
      Parameters:
      input - - source of bytes
      buff - - for reading short value. Should be 2 bytes.
      tz - - timezone
      Returns:
      ZonedDateTime
      Throws:
      IOException - when IO error occurs
    • readDate32

      public ZonedDateTime readDate32(TimeZone tz) throws IOException
      Reads a Date32 value from internal input stream.
      Parameters:
      tz - - timezone
      Returns:
      ZonedDateTime
      Throws:
      IOException - when IO error occurs
    • readDateAsLocalDate

      public LocalDate readDateAsLocalDate() throws IOException
      Throws:
      IOException
    • readDate32AsLocalDate

      public LocalDate readDate32AsLocalDate() throws IOException
      Throws:
      IOException
    • readTime

      public LocalDateTime readTime() throws IOException
      Throws:
      IOException
    • readTime64

      public LocalDateTime readTime64(int precision) throws IOException
      Throws:
      IOException
    • readDate32

      public static ZonedDateTime readDate32(InputStream input, byte[] buff, TimeZone tz) throws IOException
      Reads a date32 from input stream.
      Parameters:
      input - - source of bytes
      buff - - for reading int value. Should be 4 bytes.
      tz - - timezone
      Returns:
      ZonedDateTime
      Throws:
      IOException - when IO error occurs
    • readDateTime32

      public static ZonedDateTime readDateTime32(InputStream input, byte[] buff, TimeZone tz) throws IOException
      Reads a datetime32 from input stream.
      Parameters:
      input - - source of bytes
      buff - - for reading int value. Should be 4 bytes.
      tz - - timezone
      Returns:
      ZonedDateTime
      Throws:
      IOException - when IO error occurs
    • readDateTime64

      public ZonedDateTime readDateTime64(int scale, TimeZone tz) throws IOException
      Reads a datetime64 from internal input stream.
      Parameters:
      scale - - scale of the datetime64
      tz - - timezone
      Returns:
      ZonedDateTime
      Throws:
      IOException - when IO error occurs
    • readDateTime64

      public static ZonedDateTime readDateTime64(InputStream input, byte[] buff, int scale, TimeZone tz) throws IOException
      Reads a datetime64 from input stream.
      Parameters:
      input - - source of bytes
      buff - - for reading long value. Should be 8 bytes.
      scale - - scale of the datetime64
      tz - - timezone
      Returns:
      Throws:
      IOException
    • readString

      public String readString() throws IOException
      Reads a string from the internal input stream. Uses pre-allocated buffer to store tmp data.
      Returns:
      Throws:
      IOException
    • readStringValue

      public StringValue readStringValue() throws IOException
      Reads a string from the internal input stream preserving the raw bytes as a StringValue. Unlike readString() this does not decode bytes into a String and never reuses the shared buffer, so the value is safe to keep after the next read.
      Returns:
      string value holding the raw bytes
      Throws:
      IOException - when IO error occurs
    • readStringBytes

      public static byte[] readStringBytes(InputStream input, int len) throws IOException
      Reads the raw bytes of a string from the input stream given its length.
      Parameters:
      input - - source of bytes
      len - - number of bytes to read
      Returns:
      byte[] containing the raw string bytes
      Throws:
      IOException - when IO error occurs
    • readString

      public static String readString(InputStream input) throws IOException
      Reads a string value from input stream.
      Parameters:
      input - - source of bytes
      Returns:
      String
      Throws:
      IOException - when IO error occurs
    • readByteOrEOF

      public static int readByteOrEOF(InputStream input) throws IOException
      Throws:
      IOException
    • isReadToPrimitive

      public static boolean isReadToPrimitive(ClickHouseDataType dataType)