Package com.clickhouse.data
Class ClickHouseByteBuffer
java.lang.Object
com.clickhouse.data.ClickHouseByteBuffer
- All Implemented Interfaces:
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 Summary
FieldsModifier and TypeFieldDescriptionprotected byte[]static final ByteBufferEmpty and read-only byte buffer.static final byte[]Empty byte array.protected intprotected int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedClickHouseByteBuffer(byte[] bytes, int offset, int length) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionbyte[]array()Gets byte array.Converts to ASCII string.asBigDecimal(int scale) Converts all bytes(little-endian) to signed big integer.asBigIntegerArray(int byteLength, boolean unsigned) booleanboolean[]asDate()asDateTime(int scale) doubleasDouble()double[]floatasFloat()float[]intint[]longasLong()long[]shortasShort()short[]Converts to string using given charset.Converts to UTF-8 string.Converts all bytes(little-endian) to unsigned big integer.longlong[]intint[]asUuid()compact()Compacts byte array by creating a new copy with exact samelengthbutpositionchanged to zero.copy(boolean deep) Creates a copy of the current byte buffer.booleanbytegetBigInteger(int offset, int byteLength, boolean unsigned) ConvertsbyteLengthbytes(little-endian) starting fromposition() + offsetto big integer.booleangetBoolean(int offset) Converts the byte atposition() + offsetin array to boolean.bytegetByte(int offset) doublegetDouble(int offset) Converts 8 bytes(little-endian) starting fromposition() + offsetto double.floatgetFloat(int offset) Converts 4 bytes(little-endian) starting fromposition() + offsetto float.intgetInteger(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.longgetUnsignedInteger(int offset) Converts 4 bytes(little-endian) starting fromposition() + offsetto unsigned integer.getUnsignedLong(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.getUuid(int offset) inthashCode()booleanChecks 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.booleanmatch(byte[] bytes) Checks if the byte buffer is same as the given byte array.static ClickHouseByteBufferCreates 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(ByteBuffer buffer) Wraps given byte buffer.static ClickHouseByteBufferWraps given byte arrays as byte buffer.intposition()Gets start position.position(int newPosition) Sets new position.reset()Resets the buffer to empty.reverse()Reverses the byte array.reverse(int offset, int length) Reverses the byte array.setLength(int newLength) Sets new length.slice(int offset, int length) Gets slice of the byte buffer.toString()update(byte[] bytes) Updates buffer.update(byte[] bytes, int offset, int length) Updates buffer.update(ByteBuffer buffer) Updates buffer.Updates buffer.
-
Field Details
-
EMPTY_BYTES
public static final byte[] EMPTY_BYTESEmpty byte array. -
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 arrayoffset- start positionlength- length of bytes
-
-
Method Details
-
newInstance
Creates an empty byte buffer.- Returns:
- empty byte buffer
-
of
Wraps given byte buffer.- Parameters:
buffer- byte buffer- Returns:
- non-null wrapped byte buffer
-
of
Wraps given byte array as byte buffer.- Parameters:
bytes- byte array- Returns:
- non-null byte buffer
-
of
Wraps given byte array as byte buffer.- Parameters:
bytes- byte arrayoffset- start positionlength- length- Returns:
- non-null byte buffer
-
of
Wraps given byte arrays as byte buffer.- Parameters:
list- list of non-null byte arraysoffset- offsetlength- length- Returns:
- non-null byte buffer
-
asBigInteger
Converts all bytes(little-endian) to signed big integer. Same asgetBigInteger(0, length(), false).- Returns:
- non-null signed big integer
-
asBigDecimal
-
asBigDecimal
-
asBoolean
public boolean asBoolean() -
asBooleanArray
public boolean[] asBooleanArray() -
asDouble
public double asDouble() -
asDoubleArray
public double[] asDoubleArray() -
asDate
-
asDateTime
-
asDateTime
-
asFloat
public float asFloat() -
asFloatArray
public float[] asFloatArray() -
asUnsignedBigInteger
Converts all bytes(little-endian) to unsigned big integer. Same asgetBigInteger(0, length(), true).- Returns:
- non-null unsigned big integer
-
getBigInteger
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
-
getBoolean
public boolean getBoolean(int offset) Converts the byte atposition() + offsetin array to boolean.- Parameters:
offset- zero-based relative offset, 1 means the second byte starting fromposition()- Returns:
- boolean
-
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
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
-
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
-
asUnsignedShort
public int asUnsignedShort() -
asUnsignedIntegerArray
public long[] asUnsignedIntegerArray() -
asUnsignedShortArray
public int[] asUnsignedShortArray() -
asUuid
-
getUuid
-
asAsciiString
Converts to ASCII string.- Returns:
- non-null ASCII string
-
asString
Converts to string using given charset.- Parameters:
charset- optional charset, null is treated asStandardCharsets.UTF_8- Returns:
- non-null string
-
asUnicodeString
Converts to UTF-8 string.- Returns:
- non-null UTF-8 string
-
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
-
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
Resets the buffer to empty.- Returns:
- this byte buffer
-
reverse
Reverses the byte array.- Returns:
- this byte buffer
-
reverse
Reverses the byte array.- Parameters:
offset- start positionlength- bytes to reserve- Returns:
- this byte buffer
-
slice
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
Updates buffer.- Parameters:
buffer- byte buffer- Returns:
- this byte buffer
-
update
Updates buffer.- Parameters:
bytes- byte array, null is same as empty byte array- Returns:
- this byte buffer
-
update
Updates buffer.- Parameters:
bytes- byte array, null is same as empty byte arrayoffset- start positionlength- length of bytes- Returns:
- this byte buffer
-
update
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
-
copy
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
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
-
setLength
Sets new length.- Parameters:
newLength- new length, negative number is treated as zero- Returns:
- this byte buffer
-
hashCode
public int hashCode() -
equals
-
toString
-