Package com.clickhouse.client
Class ClickHouseByteBuffer
- java.lang.Object
-
- com.clickhouse.client.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 usesposition()andlength()to define the slice. You may think of it as a lite version ofByteBuffer.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected byte[]arraystatic ByteBufferEMPTY_BUFFEREmpty and read-only byte buffer.static byte[]EMPTY_BYTESEmpty byte array.protected intlengthprotected intposition
-
Constructor Summary
Constructors Modifier Constructor Description protectedClickHouseByteBuffer(byte[] bytes, int offset, int length)Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description byte[]array()Gets byte array.StringasAsciiString()Converts to ASCII string.BigIntegerasBigInteger()Converts all bytes(little-endian) to signed big integer.BigInteger[]asBigIntegerArray(int byteLength, boolean unsigned)doubleasDouble()double[]asDoubleArray()floatasFloat()float[]asFloatArray()intasInt32()Deprecated.will be removed in v0.3.3, please useasInteger()insteadlongasInt64()Deprecated.will be removed in v0.3.3, please useasLong()insteadintasInteger()int[]asIntegerArray()longasLong()long[]asLongArray()shortasShort()short[]asShortArray()StringasString(Charset charset)Converts to string using given charset.StringasUnicodeString()Converts to UTF-8 string.BigIntegerasUnsignedBigInteger()Converts all bytes(little-endian) to unsigned big integer.longasUnsignedInt32()Deprecated.will be removed in v0.3.3, please useasUnsignedInteger()insteadBigIntegerasUnsignedInt64()Deprecated.will be removed in v0.3.3, please useasUnsignedLong()insteadlongasUnsignedInteger()long[]asUnsignedIntegerArray()BigIntegerasUnsignedLong()intasUnsignedShort()int[]asUnsignedShortArray()UUIDasUuid()ClickHouseByteBuffercompact()Compacts byte array by creating a new copy with exact samelengthbutpositionchanged to zero.booleanequals(Object obj)bytefirstByte()BigIntegergetBigInteger(int offset, int byteLength, boolean unsigned)ConvertsbyteLengthbytes(little-endian) starting fromposition() + offsetto big integer.doublegetDouble(int offset)Converts 8 bytes(little-endian) starting fromposition() + offsetto double.floatgetFloat(int offset)Converts 4 bytes(little-endian) starting fromposition() + offsetto float.intgetInt32(int offset)Deprecated.will be removed in v0.3.3, please usegetInteger(int)insteadlonggetInt64(int offset)Deprecated.will be removed in v0.3.3, please usegetLong(int)insteadintgetInteger(int offset)Converts 4 bytes(little-endian) starting fromposition() + offsetto signed integer.longgetLong(int offset)Converts 8 bytes(little-endian) starting fromposition() + offsetto signed long.shortgetShort(int offset)Converts 2 bytes(little-endian) starting fromposition() + offsetto signed short.longgetUnsignedInt32(int offset)Deprecated.will be removed in v0.3.3, please usegetUnsignedInteger(int)insteadlonggetUnsignedInteger(int offset)Converts 4 bytes(little-endian) starting fromposition() + offsetto unsigned integer.BigIntegergetUnsignedLong(int offset)Converts 8 bytes(little-endian) starting fromposition() + offsetto unsigned long.intgetUnsignedShort(int offset)Converts 2 bytes(little-endian) starting fromposition() + offsetto unsigned short.UUIDgetUuid(int offset)inthashCode()booleanisCompact()Checks whether the buffer is compact or not.booleanisEmpty()Checks whether the buffer is empty or not.bytelastByte()intlength()Gets length of bytes.intlimit()Gets end position.static ClickHouseByteBuffernewInstance()Creates an empty byte buffer.static ClickHouseByteBufferof(byte[] bytes)Wraps given byte array as byte buffer.static ClickHouseByteBufferof(byte[] bytes, int offset, int length)Wraps given byte array as byte buffer.static ClickHouseByteBufferof(List<byte[]> list, int offset, int length)Wraps given byte arrays as byte buffer.intposition()Gets start position.ClickHouseByteBufferposition(int newPosition)Sets new position.ClickHouseByteBufferreset()Resets the buffer to empty.ClickHouseByteBufferreverse()Reverses the byte array.ClickHouseByteBufferreverse(int offset, int length)Reverses the byte array.ClickHouseByteBufferslice(int offset, int length)Gets slice of the byte buffer.StringtoString()ClickHouseByteBufferupdate(byte[] bytes)Updates buffer.ClickHouseByteBufferupdate(byte[] bytes, int offset, int length)Updates buffer.ClickHouseByteBufferupdate(List<byte[]> list, int offset, int length)Updates buffer.
-
-
-
Field Detail
-
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
-
-
Method Detail
-
newInstance
public static ClickHouseByteBuffer newInstance()
Creates an empty byte buffer.- Returns:
- empty 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 arrayoffset- start positionlength- 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 arraysoffset- offsetlength- length- Returns:
- non-null byte buffer
-
asInt32
@Deprecated public int asInt32()
Deprecated.will be removed in v0.3.3, please useasInteger()insteadConverts 4 bytes(little-endian) to Int32. Same asgetInteger(0).- Returns:
- Int32 value
-
asUnsignedInt32
@Deprecated public long asUnsignedInt32()
Deprecated.will be removed in v0.3.3, please useasUnsignedInteger()insteadConverts 4 bytes(little-endian) to UInt32. Same asgetUnsignedInteger(0).- Returns:
- UInt32 value
-
getInt32
@Deprecated public int getInt32(int offset)
Deprecated.will be removed in v0.3.3, please usegetInteger(int)insteadConverts 4 bytes(little-endian) starting fromposition() + offsetto Int32.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- Int32 value
-
getUnsignedInt32
@Deprecated public long getUnsignedInt32(int offset)
Deprecated.will be removed in v0.3.3, please usegetUnsignedInteger(int)insteadConverts 4 bytes(little-endian) starting fromposition() + offsetto UInt32.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- UInt32 value
-
asInt64
@Deprecated public long asInt64()
Deprecated.will be removed in v0.3.3, please useasLong()insteadConverts 8 bytes(little-endian) to Int64. Same asgetLong(0).- Returns:
- Int64 value
-
asUnsignedInt64
@Deprecated public BigInteger asUnsignedInt64()
Deprecated.will be removed in v0.3.3, please useasUnsignedLong()insteadConverts 8 bytes(little-endian) to unsigned Int64. Same asgetBigInteger(0, 8, true).- Returns:
- unsigned Int64 value
-
getInt64
@Deprecated public long getInt64(int offset)
Deprecated.will be removed in v0.3.3, please usegetLong(int)insteadConverts 8 bytes(little-endian) starting fromposition() + offsetto Int64.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- Int64 value
-
asBigInteger
public BigInteger asBigInteger()
Converts all bytes(little-endian) to signed big integer. Same asgetBigInteger(0, length(), false).- Returns:
- non-null signed big integer
-
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 asgetBigInteger(0, length(), true).- Returns:
- non-null unsigned big integer
-
getBigInteger
public BigInteger getBigInteger(int offset, int byteLength, boolean unsigned)
ConvertsbyteLengthbytes(little-endian) starting fromposition() + offsetto big integer.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()byteLength- bytes to convertunsigned- true if it's unsigned big integer; false otherwise- Returns:
- non-null big integer
-
getDouble
public double getDouble(int offset)
Converts 8 bytes(little-endian) starting fromposition() + offsetto double.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- double
-
getFloat
public float getFloat(int offset)
Converts 4 bytes(little-endian) starting fromposition() + offsetto float.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- float
-
getInteger
public int getInteger(int offset)
Converts 4 bytes(little-endian) starting fromposition() + offsetto signed integer.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- signed integer
-
getLong
public long getLong(int offset)
Converts 8 bytes(little-endian) starting fromposition() + offsetto signed long.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- signed long
-
getShort
public short getShort(int offset)
Converts 2 bytes(little-endian) starting fromposition() + offsetto signed short.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- signed short
-
getUnsignedInteger
public long getUnsignedInteger(int offset)
Converts 4 bytes(little-endian) starting fromposition() + offsetto unsigned integer.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- unsigned integer
-
getUnsignedLong
public BigInteger getUnsignedLong(int offset)
Converts 8 bytes(little-endian) starting fromposition() + offsetto unsigned long.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- non-null unsigned long
-
getUnsignedShort
public int getUnsignedShort(int offset)
Converts 2 bytes(little-endian) starting fromposition() + offsetto unsigned short.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- 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 asStandardCharsets.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 samelengthbutpositionchanged to zero. It does nothing whenpositionis zero andlengthis same asbytes.length.- Returns:
- this byte buffer
-
isCompact
public boolean isCompact()
Checks whether the buffer is compact or not. A buffer is compact whenpositionpoints to zero andlengthequals toarray().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 returntruewhenlength()is zero, evenarray()may return an non-empty byte array.- Returns:
- true if the buffer is empty; 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 positionlength- 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 fromposition()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(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 arrayoffset- start positionlength- 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 arrayoffset- start positionlength- length of bytes- Returns:
- this byte buffer
-
array
public byte[] array()
Gets byte array.- Returns:
- non-null byte array
-
firstByte
public byte firstByte()
-
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 thanlimit()- 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
-
-