Class BinaryStreamUtils


  • public final class BinaryStreamUtils
    extends Object
    Utility class for dealing with binary stream and data.
    • Field Detail

      • U_INT64_MAX

        public static final BigInteger U_INT64_MAX
      • U_INT128_MAX

        public static final BigInteger U_INT128_MAX
      • U_INT256_MAX

        public static final BigInteger U_INT256_MAX
      • DATE32_MAX

        public static final int DATE32_MAX
      • DATE32_MIN

        public static final int DATE32_MIN
      • DECIMAL32_MAX

        public static final BigDecimal DECIMAL32_MAX
      • DECIMAL32_MIN

        public static final BigDecimal DECIMAL32_MIN
      • DECIMAL64_MAX

        public static final BigDecimal DECIMAL64_MAX
      • DECIMAL64_MIN

        public static final BigDecimal DECIMAL64_MIN
      • DECIMAL128_MAX

        public static final BigDecimal DECIMAL128_MAX
      • DECIMAL128_MIN

        public static final BigDecimal DECIMAL128_MIN
      • DECIMAL256_MAX

        public static final BigDecimal DECIMAL256_MAX
      • DECIMAL256_MIN

        public static final BigDecimal DECIMAL256_MIN
      • DATETIME64_MAX

        public static final long DATETIME64_MAX
      • DATETIME64_MIN

        public static final long DATETIME64_MIN
      • MILLIS_IN_DAY

        public static final long MILLIS_IN_DAY
    • Method Detail

      • toInt32

        public static int toInt32​(byte[] bytes,
                                  int offset)
      • toInt64

        public static long toInt64​(byte[] bytes,
                                   int offset)
      • setInt32

        public static void setInt32​(byte[] bytes,
                                    int offset,
                                    int value)
      • setInt64

        public static void setInt64​(byte[] bytes,
                                    int offset,
                                    long value)
      • reverse

        public static byte[] reverse​(byte[] bytes)
        Reverse the given byte array.
        Parameters:
        bytes - byte array to manipulate
        Returns:
        same byte array but reserved
      • getVarIntSize

        public static int getVarIntSize​(int value)
        Get varint length of given integer.
        Parameters:
        value - integer
        Returns:
        varint length
      • getVarLongSize

        public static int getVarLongSize​(long value)
        Get varint length of given long.
        Parameters:
        value - long
        Returns:
        varint length
      • writeByteBuffer

        public static void writeByteBuffer​(OutputStream output,
                                           ByteBuffer buffer)
                                    throws IOException
        Writes bytes into given output stream.
        Parameters:
        output - non-null output stream
        buffer - non-null byte buffer
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeBitmap

        public static void writeBitmap​(OutputStream output,
                                       ClickHouseBitmap bitmap)
                                throws IOException
        Writes bitmap into given output stream.
        Parameters:
        output - non-null output stream
        bitmap - non-null bitmap
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeBytes

        public static void writeBytes​(OutputStream output,
                                      byte[] bytes)
                               throws IOException
        Writes bytes into given output stream.
        Parameters:
        output - non-null output stream
        bytes - non-null byte array
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readCharacters

        public static char[] readCharacters​(Reader input,
                                            int length)
                                     throws IOException
        Reads length characters from given reader. It behaves in a similar way as DataInput.readFully(byte[]).
        Parameters:
        input - non-null reader
        length - number of characters to read
        Returns:
        character array and its length should be length
        Throws:
        IOException - when failed to read value from input stream, not able to retrieve all bytes, or reached end of the stream
      • readBoolean

        public static boolean readBoolean​(ClickHouseInputStream input)
                                   throws IOException
        Read boolean from given input stream. It uses ClickHouseInputStream.readByte() to get value and return true only when the value is 1.
        Parameters:
        input - non-null input stream
        Returns:
        boolean
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeBoolean

        public static void writeBoolean​(OutputStream output,
                                        boolean value)
                                 throws IOException
        Write boolean into given output stream.
        Parameters:
        output - non-null output stream
        value - boolean value, true == 1 and false == 0
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeBoolean

        public static void writeBoolean​(OutputStream output,
                                        int value)
                                 throws IOException
        Write integer as boolean into given output stream.
        Parameters:
        output - non-null output stream
        value - integer, everyting else besides one will be treated as zero(false)
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readEnum8

        public static <T extends Enum<T>> T readEnum8​(ClickHouseInputStream input,
                                                      Class<T> enumType)
                                               throws IOException
        Read enum value from given input stream.
        Type Parameters:
        T - enum type
        Parameters:
        input - non-null input stream
        enumType - enum class
        Returns:
        enum value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeEnum8

        public static void writeEnum8​(OutputStream output,
                                      byte value)
                               throws IOException
        Write enum value into given output stream. Same as writeInt8(OutputStream, byte).
        Parameters:
        output - non-null output stream
        value - enum value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeEnum8

        public static <T extends Enum<T>> void writeEnum8​(OutputStream output,
                                                          T value)
                                                   throws IOException
        Write enum value into given output stream.
        Type Parameters:
        T - type of the value
        Parameters:
        output - non-null output stream
        value - enum value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readEnum16

        public static <T extends Enum<T>> T readEnum16​(ClickHouseInputStream input,
                                                       Class<T> enumType)
                                                throws IOException
        Read enum value from given input stream.
        Type Parameters:
        T - enum type
        Parameters:
        input - non-null input stream
        enumType - enum class
        Returns:
        enum value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeEnum16

        public static void writeEnum16​(OutputStream output,
                                       int value)
                                throws IOException
        Write enum value into given output stream. Same as writeInt16(OutputStream, int).
        Parameters:
        output - non-null output stream
        value - enum value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeEnum16

        public static <T extends Enum<T>> void writeEnum16​(OutputStream output,
                                                           T value)
                                                    throws IOException
        Write enum value into given output stream.
        Type Parameters:
        T - type of the value
        Parameters:
        output - non-null output stream
        value - enum value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readGeoPoint

        public static double[] readGeoPoint​(ClickHouseInputStream input)
                                     throws IOException
        Read geo point(X and Y coordinates) from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        X and Y coordinates
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeGeoPoint

        public static void writeGeoPoint​(OutputStream output,
                                         double[] value)
                                  throws IOException
        Write geo point(X and Y coordinates).
        Parameters:
        output - non-null output stream
        value - X and Y coordinates
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeGeoPoint

        public static void writeGeoPoint​(OutputStream output,
                                         double x,
                                         double y)
                                  throws IOException
        Write geo point(X and Y coordinates).
        Parameters:
        output - non-null output stream
        x - X coordinate
        y - Y coordinate
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readGeoRing

        public static double[][] readGeoRing​(ClickHouseInputStream input)
                                      throws IOException
        Read geo ring(array of X and Y coordinates) from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        array of X and Y coordinates
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeGeoRing

        public static void writeGeoRing​(OutputStream output,
                                        double[][] value)
                                 throws IOException
        Write geo ring(array of X and Y coordinates).
        Parameters:
        output - non-null output stream
        value - array of X and Y coordinates
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readGeoPolygon

        public static double[][][] readGeoPolygon​(ClickHouseInputStream input)
                                           throws IOException
        Read geo polygon(array of rings) from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        array of rings
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeGeoPolygon

        public static void writeGeoPolygon​(OutputStream output,
                                           double[][][] value)
                                    throws IOException
        Write geo polygon(array of rings).
        Parameters:
        output - non-null output stream
        value - array of rings
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readGeoMultiPolygon

        public static double[][][][] readGeoMultiPolygon​(ClickHouseInputStream input)
                                                  throws IOException
        Read geo multi-polygon(array of polygons) from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        array of polygons
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeGeoMultiPolygon

        public static void writeGeoMultiPolygon​(OutputStream output,
                                                double[][][][] value)
                                         throws IOException
        Write geo polygon(array of rings).
        Parameters:
        output - non-null output stream
        value - array of polygons
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeNull

        public static void writeNull​(OutputStream output)
                              throws IOException
        Write null marker. Same as writeBoolean(outut, true).
        Parameters:
        output - non-null output stream
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeNonNull

        public static void writeNonNull​(OutputStream output)
                                 throws IOException
        Write non-null marker. Same as writeBoolean(outut, false).
        Parameters:
        output - non-null output stream
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readInet4Address

        public static Inet4Address readInet4Address​(ClickHouseInputStream input)
                                             throws IOException
        Read Inet4Address from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        Inet4Address
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeInet4Address

        public static void writeInet4Address​(OutputStream output,
                                             Inet4Address value)
                                      throws IOException
        Write Inet4Address to given output stream.
        Parameters:
        output - non-null output stream
        value - Inet4Address
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readInet6Address

        public static Inet6Address readInet6Address​(ClickHouseInputStream input)
                                             throws IOException
        Read Inet6Address from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        Inet6Address
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeInet6Address

        public static void writeInet6Address​(OutputStream output,
                                             Inet6Address value)
                                      throws IOException
        Write Inet6Address to given output stream.
        Parameters:
        output - non-null output stream
        value - Inet6Address
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeInt8

        public static void writeInt8​(OutputStream output,
                                     byte value)
                              throws IOException
        Write a byte to given output stream.
        Parameters:
        output - non-null output stream
        value - byte
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeInt8

        public static void writeInt8​(OutputStream output,
                                     int value)
                              throws IOException
        Write a byte to given output stream.
        Parameters:
        output - non-null output stream
        value - byte
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readUnsignedInt8

        public static short readUnsignedInt8​(ClickHouseInputStream input)
                                      throws IOException
        Read an unsigned byte as short from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        unsigned byte
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeUnsignedInt8

        public static void writeUnsignedInt8​(OutputStream output,
                                             int value)
                                      throws IOException
        Write an unsigned byte to given output stream.
        Parameters:
        output - non-null output stream
        value - unsigned byte
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readInt16

        public static short readInt16​(ClickHouseInputStream input)
                               throws IOException
        Read a short value from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        short value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeInt16

        public static void writeInt16​(OutputStream output,
                                      short value)
                               throws IOException
        Write a short value to given output stream.
        Parameters:
        output - non-null output stream
        value - short value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeInt16

        public static void writeInt16​(OutputStream output,
                                      int value)
                               throws IOException
        Write a short value to given output stream.
        Parameters:
        output - non-null output stream
        value - short value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readUnsignedInt16

        public static int readUnsignedInt16​(ClickHouseInputStream input)
                                     throws IOException
        Read an unsigned short value from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        unsigned short value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeUnsignedInt16

        public static void writeUnsignedInt16​(OutputStream output,
                                              int value)
                                       throws IOException
        Write an unsigned short value to given output stream.
        Parameters:
        output - non-null output stream
        value - unsigned short value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readInt32

        public static int readInt32​(ClickHouseInputStream input)
                             throws IOException
        Read an integer from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        integer
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeInt32

        public static void writeInt32​(OutputStream output,
                                      int value)
                               throws IOException
        Write an integer to given output stream.
        Parameters:
        output - non-null output stream
        value - integer
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readUnsignedInt32

        public static long readUnsignedInt32​(ClickHouseInputStream input)
                                      throws IOException
        Read an unsigned integer from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        unsigned integer
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeUnsignedInt32

        public static void writeUnsignedInt32​(OutputStream output,
                                              long value)
                                       throws IOException
        Write an unsigned integer to given output stream.
        Parameters:
        output - non-null output stream
        value - unsigned integer
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readInt64

        public static long readInt64​(ClickHouseInputStream input)
                              throws IOException
        Read a long value from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        long value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeInt64

        public static void writeInt64​(OutputStream output,
                                      long value)
                               throws IOException
        Write a long value to given output stream.
        Parameters:
        output - non-null output stream
        value - long value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readUnsignedInt64

        public static BigInteger readUnsignedInt64​(ClickHouseInputStream input)
                                            throws IOException
        Read an unsigned long value from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        unsigned long value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeUnsignedInt64

        public static void writeUnsignedInt64​(OutputStream output,
                                              long value)
                                       throws IOException
        Write an unsigned long value to given output stream.
        Parameters:
        output - non-null output stream
        value - unsigned long value, negative number will be treated as positive
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeUnsignedInt64

        public static void writeUnsignedInt64​(OutputStream output,
                                              BigInteger value)
                                       throws IOException
        Write an unsigned long value to given output stream. Due to overhead of BigInteger, this method in general uses more memory and slower than writeUnsignedInt64(OutputStream, long).
        Parameters:
        output - non-null output stream
        value - unsigned long value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readInt128

        public static BigInteger readInt128​(ClickHouseInputStream input)
                                     throws IOException
        Read a big integer(16 bytes) from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        big integer
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeInt128

        public static void writeInt128​(OutputStream output,
                                       BigInteger value)
                                throws IOException
        Write a big integer(16 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - big integer
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readUnsignedInt128

        public static BigInteger readUnsignedInt128​(ClickHouseInputStream input)
                                             throws IOException
        Read an unsigned big integer from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        unsigned big integer
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeUnsignedInt128

        public static void writeUnsignedInt128​(OutputStream output,
                                               BigInteger value)
                                        throws IOException
        Write an unsigned big integer(16 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - unsigned big integer
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readInt256

        public static BigInteger readInt256​(ClickHouseInputStream input)
                                     throws IOException
        Read a big integer(32 bytes) from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        big integer
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeInt256

        public static void writeInt256​(OutputStream output,
                                       BigInteger value)
                                throws IOException
        Write a big integer(32 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - big integer
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readUnsignedInt256

        public static BigInteger readUnsignedInt256​(ClickHouseInputStream input)
                                             throws IOException
        Read an unsigned big integer(32 bytes) from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        big integer
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeUnsignedInt256

        public static void writeUnsignedInt256​(OutputStream output,
                                               BigInteger value)
                                        throws IOException
        Write an unsigned big integer(32 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - unsigned big integer
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readFloat32

        public static float readFloat32​(ClickHouseInputStream input)
                                 throws IOException
        Read a float value from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        float value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeFloat32

        public static void writeFloat32​(OutputStream output,
                                        float value)
                                 throws IOException
        Write a float value to given output stream.
        Parameters:
        output - non-null output stream
        value - float value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readFloat64

        public static double readFloat64​(ClickHouseInputStream input)
                                  throws IOException
        Read a double value from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        double value
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeFloat64

        public static void writeFloat64​(OutputStream output,
                                        double value)
                                 throws IOException
        Write a double value to given output stream.
        Parameters:
        output - non-null output stream
        value - double value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readUuid

        public static UUID readUuid​(ClickHouseInputStream input)
                             throws IOException
        Read UUID from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        UUID
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeUuid

        public static void writeUuid​(OutputStream output,
                                     UUID value)
                              throws IOException
        Write a UUID to given output stream.
        Parameters:
        output - non-null output stream
        value - UUID
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeBigInteger

        public static void writeBigInteger​(OutputStream output,
                                           BigInteger value,
                                           int length)
                                    throws IOException
        Write a length-byte long big integer to given output stream.
        Parameters:
        output - non-null output stream
        value - big integer
        length - byte length of the value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDecimal

        public static BigDecimal readDecimal​(ClickHouseInputStream input,
                                             int precision,
                                             int scale)
                                      throws IOException
        Read big decimal(4 - 32 bytes) from given input stream.
        Parameters:
        input - non-null input stream
        precision - precision of the decimal
        scale - scale of the decimal
        Returns:
        big decimal
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDecimal

        public static void writeDecimal​(OutputStream output,
                                        BigDecimal value,
                                        int precision,
                                        int scale)
                                 throws IOException
        Write a big decimal(4 - 32 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - big decimal
        precision - precision of the decimal
        scale - scale of the decimal, might be different from BigDecimal.scale()
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDecimal32

        public static BigDecimal readDecimal32​(ClickHouseInputStream input,
                                               int scale)
                                        throws IOException
        Read big decimal(4 bytes) from given input stream.
        Parameters:
        input - non-null input stream
        scale - scale of the decimal
        Returns:
        big decimal
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDecimal32

        public static void writeDecimal32​(OutputStream output,
                                          BigDecimal value,
                                          int scale)
                                   throws IOException
        Write a big decimal(4 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - big decimal
        scale - scale of the decimal, might be different from BigDecimal.scale()
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDecimal64

        public static BigDecimal readDecimal64​(ClickHouseInputStream input,
                                               int scale)
                                        throws IOException
        Read big decimal(8 bytes) from gicen input stream.
        Parameters:
        input - non-null input stream
        scale - scale of the decimal
        Returns:
        big decimal
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDecimal64

        public static void writeDecimal64​(OutputStream output,
                                          BigDecimal value,
                                          int scale)
                                   throws IOException
        Write a big decimal(8 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - big decimal
        scale - scale of the decimal, might be different from BigDecimal.scale()
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDecimal128

        public static BigDecimal readDecimal128​(ClickHouseInputStream input,
                                                int scale)
                                         throws IOException
        Read big decimal(16 bytes) from given input stream.
        Parameters:
        input - non-null input stream
        scale - scale of the decimal
        Returns:
        big decimal
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDecimal128

        public static void writeDecimal128​(OutputStream output,
                                           BigDecimal value,
                                           int scale)
                                    throws IOException
        Write a big decimal(16 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - big decimal
        scale - scale of the decimal, might be different from BigDecimal.scale()
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDecimal256

        public static BigDecimal readDecimal256​(ClickHouseInputStream input,
                                                int scale)
                                         throws IOException
        Read big decimal from given input stream.
        Parameters:
        input - non-null input stream
        scale - scale of the decimal
        Returns:
        big decimal
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDecimal256

        public static void writeDecimal256​(OutputStream output,
                                           BigDecimal value,
                                           int scale)
                                    throws IOException
        Write a big decimal(32 bytes) to given output stream.
        Parameters:
        output - non-null output stream
        value - big decimal
        scale - scale of the decimal, might be different from BigDecimal.scale()
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDate

        public static LocalDate readDate​(ClickHouseInputStream input,
                                         TimeZone tz)
                                  throws IOException
        Read LocalDate from given input stream.
        Parameters:
        input - non-null input stream
        tz - time zone for date, could be null
        Returns:
        local date
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDate

        public static void writeDate​(OutputStream output,
                                     LocalDate value,
                                     TimeZone tz)
                              throws IOException
        Write a LocalDate to given output stream.
        Parameters:
        output - non-null output stream
        value - local date
        tz - time zone for date, could be null
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeDate

        public static void writeDate​(OutputStream output,
                                     LocalDate value)
                              throws IOException
        Write a LocalDate to given output stream.
        Parameters:
        output - non-null output stream
        value - local date
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDate32

        public static LocalDate readDate32​(ClickHouseInputStream input,
                                           TimeZone tz)
                                    throws IOException
        Read LocalDate from given input stream.
        Parameters:
        input - non-null input stream
        tz - time zone for date, could be null
        Returns:
        local date
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDate32

        public static void writeDate32​(OutputStream output,
                                       LocalDate value,
                                       TimeZone tz)
                                throws IOException
        Write a LocalDate to given output stream.
        Parameters:
        output - non-null output stream
        value - local date
        tz - time zone for date, could be null
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeDate32

        public static void writeDate32​(OutputStream output,
                                       LocalDate value)
                                throws IOException
        Write a LocalDate to given output stream.
        Parameters:
        output - non-null output stream
        value - local date
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDateTime

        public static LocalDateTime readDateTime​(ClickHouseInputStream input,
                                                 int scale,
                                                 TimeZone tz)
                                          throws IOException
        Read LocalDateTime from given input stream.
        Parameters:
        input - non-null input stream
        scale - scale of the datetime, must between 0 and 9 inclusive
        tz - time zone, null is treated as UTC
        Returns:
        local datetime
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDateTime

        public static void writeDateTime​(OutputStream output,
                                         LocalDateTime value,
                                         TimeZone tz)
                                  throws IOException
        Write a LocalDateTime to given output stream.
        Parameters:
        output - non-null output stream
        value - local datetime
        tz - time zone, null is treated as UTC
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeDateTime

        public static void writeDateTime​(OutputStream output,
                                         LocalDateTime value,
                                         int scale,
                                         TimeZone tz)
                                  throws IOException
        Write a LocalDateTime to given output stream.
        Parameters:
        output - non-null output stream
        value - local datetime
        scale - scale of the datetime, must between 0 and 9 inclusive
        tz - time zone, null is treated as UTC
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeDateTime32

        public static void writeDateTime32​(OutputStream output,
                                           LocalDateTime value,
                                           TimeZone tz)
                                    throws IOException
        Write a LocalDateTime to given output stream.
        Parameters:
        output - non-null output stream
        value - local datetime
        tz - time zone, null is treated as UTC
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readDateTime64

        public static LocalDateTime readDateTime64​(ClickHouseInputStream input,
                                                   TimeZone tz)
                                            throws IOException
        Read LocalDateTime from given input stream. Same as readDateTime64(input, 3).
        Parameters:
        input - non-null input stream
        tz - time zone, null is treated as UTC
        Returns:
        local datetime
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • readDateTime64

        public static LocalDateTime readDateTime64​(ClickHouseInputStream input,
                                                   int scale,
                                                   TimeZone tz)
                                            throws IOException
        Read LocalDateTime from given input stream.
        Parameters:
        input - non-null input stream
        scale - scale of the datetime
        tz - time zone, null is treated as UTC
        Returns:
        local datetime
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeDateTime64

        public static void writeDateTime64​(OutputStream output,
                                           LocalDateTime value,
                                           TimeZone tz)
                                    throws IOException
        Write a LocalDateTime to given output stream. Same as writeDateTime64(output, value, 3).
        Parameters:
        output - non-null output stream
        value - local datetime
        tz - time zone, null is treated as UTC
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeDateTime64

        public static void writeDateTime64​(OutputStream output,
                                           LocalDateTime value,
                                           int scale,
                                           TimeZone tz)
                                    throws IOException
        Write a LocalDateTime to given output stream.
        Parameters:
        output - non-null output stream
        value - local datetime
        scale - scale of the datetime, must between 0 and 9 inclusive
        tz - time zone, null is treated as UTC
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readFixedString

        public static String readFixedString​(ClickHouseInputStream input,
                                             int length)
                                      throws IOException
        Read string with fixed length from given input stream.
        Parameters:
        input - non-null input stream
        length - byte length of the string
        Returns:
        string with fixed length
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • readFixedString

        public static String readFixedString​(ClickHouseInputStream input,
                                             int length,
                                             Charset charset)
                                      throws IOException
        Read string with fixed length from given input stream.
        Parameters:
        input - non-null input stream
        length - byte length of the string
        charset - charset used to convert string to byte array, null means UTF-8
        Returns:
        string with fixed length
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • writeFixedString

        public static void writeFixedString​(OutputStream output,
                                            String value,
                                            int length)
                                     throws IOException
        Write a string with fixed length to given output stream.
        Parameters:
        output - non-null output stream
        value - string
        length - byte length of the string
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeFixedString

        public static void writeFixedString​(OutputStream output,
                                            String value,
                                            int length,
                                            Charset charset)
                                     throws IOException
        Write a string with fixed length to given output stream.
        Parameters:
        output - non-null output stream
        value - string
        length - byte length of the string
        charset - charset used to convert string to byte array, null means UTF-8
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readString

        public static String readString​(Reader input,
                                        int length)
                                 throws IOException
        Reads characters from given reader.
        Parameters:
        input - non-null reader
        length - length in character
        Returns:
        string value
        Throws:
        IOException - when failed to read value from reader or reached end of the stream
      • writeString

        public static void writeString​(OutputStream output,
                                       String value)
                                throws IOException
        Write a string to given output stream.
        Parameters:
        output - non-null output stream
        value - string
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeString

        public static void writeString​(OutputStream output,
                                       String value,
                                       Charset charset)
                                throws IOException
        Write a string to given output stream.
        Parameters:
        output - non-null output stream
        value - string
        charset - charset used to convert string to byte array, null means UTF-8
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeString

        public static void writeString​(OutputStream output,
                                       byte[] value)
                                throws IOException
        Writes a binary string to given output stream.
        Parameters:
        output - non-null output stream
        value - non-null byte array
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • readVarInt

        public static int readVarInt​(InputStream input)
                              throws IOException
        Read varint from given input stream.
        Parameters:
        input - non-null input stream
        Returns:
        varint
        Throws:
        IOException - when failed to read value from input stream or reached end of the stream
      • readVarInt

        public static int readVarInt​(ByteBuffer buffer)
        Read varint from given byte buffer.
        Parameters:
        buffer - non-null byte buffer
        Returns:
        varint
      • writeVarInt

        public static void writeVarInt​(OutputStream output,
                                       long value)
                                throws IOException
        Write varint to given output stream.
        Parameters:
        output - non-null output stream
        value - long value
        Throws:
        IOException - when failed to write value to output stream or reached end of the stream
      • writeVarInt

        public static void writeVarInt​(ByteBuffer buffer,
                                       int value)
        Write varint to given output stream.
        Parameters:
        buffer - non-null byte buffer
        value - integer value