Class ByteArrayUtil
- java.lang.Object
-
- elki.utilities.io.ByteArrayUtil
-
public final class ByteArrayUtil extends java.lang.ObjectClass with various utilities for manipulating byte arrays.If you find a reusable copy of this in the Java API, please tell me. Using a
ByteArrayOutputStreamandDataInputStreamdoesn't seem appropriate.C.f.
DataOutputStreamandByteArrayOutputStream- Since:
- 0.2
- Author:
- Erich Schubert
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classByteArrayUtil.ByteSerializerSerializer for byte objects.static classByteArrayUtil.DoubleSerializerSerializer for double objects.static classByteArrayUtil.FloatSerializerSerializer for float objects.static classByteArrayUtil.IntegerSerializerSerializer for integer objects.static classByteArrayUtil.LongSerializerSerializer for long objects.static classByteArrayUtil.ShortSerializerSerializer for short objects.static classByteArrayUtil.StringSerializerSerializer for String objects.static classByteArrayUtil.VarintSerializerSerializer for Integer objects using a variable size encoding.
-
Field Summary
Fields Modifier and Type Field Description static ByteArrayUtil.ByteSerializerBYTE_SERIALIZERStatic instance.static ByteArrayUtil.DoubleSerializerDOUBLE_SERIALIZERStatic instance.static ByteArrayUtil.FloatSerializerFLOAT_SERIALIZERStatic instance.static ByteArrayUtil.IntegerSerializerINT_SERIALIZERStatic instance.static ByteArrayUtil.LongSerializerLONG_SERIALIZERStatic instance.static ByteArrayUtil.ShortSerializerSHORT_SERIALIZERStatic instance.static intSIZE_BYTESize of a byte in bytes.static intSIZE_DOUBLESize of a double in bytes.static intSIZE_FLOATSize of a float in bytes.static intSIZE_INTSize of an integer in bytes.static intSIZE_LONGSize of a long in bytes.static intSIZE_SHORTSize of a short in bytes.static ByteArrayUtil.StringSerializerSTRING_SERIALIZERStatic instance.static ByteArrayUtil.VarintSerializerVARINT_SERIALIZERStatic instance.
-
Constructor Summary
Constructors Modifier Constructor Description privateByteArrayUtil()Fake constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static intgetSignedVarintLongSize(long val)Compute the size of the varint encoding for this signed integer.static intgetSignedVarintSize(int val)Compute the size of the varint encoding for this signed integer.static intgetStringSize(java.lang.String s)Compute the size of the string after encoding.static intgetUnsignedVarintLongSize(long obj)Compute the size of the varint encoding for this unsigned integer.static intgetUnsignedVarintSize(int obj)Compute the size of the varint encoding for this unsigned integer.static doublereadDouble(byte[] array, int offset)Read a double from the byte array at the given offset.static floatreadFloat(byte[] array, int offset)Read a float from the byte array at the given offset.static intreadInt(byte[] array, int offset)Read an integer from the byte array at the given offset.static longreadLong(byte[] array, int offset)Read a long from the byte array at the given offset.static shortreadShort(byte[] array, int offset)Read a short from the byte array at the given offset.static intreadSignedVarint(java.nio.ByteBuffer buffer)Read a signed integer.static longreadSignedVarintLong(java.nio.ByteBuffer buffer)Read a signed long.static java.lang.StringreadString(java.nio.ByteBuffer buffer)Read a string from the buffer.static intreadUnsignedShort(byte[] array, int offset)Read an unsigned short from the byte array at the given offset.static intreadUnsignedVarint(java.nio.ByteBuffer buffer)Read an unsigned integer.static longreadUnsignedVarintLong(java.nio.ByteBuffer buffer)Read an unsigned long.static intwriteDouble(byte[] array, int offset, double v)Write a double to the byte array at the given offset.static intwriteFloat(byte[] array, int offset, float v)Write a float to the byte array at the given offset.static intwriteInt(byte[] array, int offset, int v)Write an integer to the byte array at the given offset.static intwriteLong(byte[] array, int offset, long v)Write a long to the byte array at the given offset.static intwriteShort(byte[] array, int offset, int v)Write a short to the byte array at the given offset.static voidwriteSignedVarint(java.nio.ByteBuffer buffer, int val)Write an signed integer using a variable-length encoding.static voidwriteSignedVarintLong(java.nio.ByteBuffer buffer, long val)Write a signed long using a variable-length encoding.static voidwriteString(java.nio.ByteBuffer buffer, java.lang.String s)Write a string to the buffer.static voidwriteUnsignedVarint(java.nio.ByteBuffer buffer, int val)Write an unsigned integer using a variable-length encoding.static voidwriteUnsignedVarintLong(java.nio.ByteBuffer buffer, long val)Write an unsigned long using a variable-length encoding.
-
-
-
Field Detail
-
SIZE_BYTE
public static final int SIZE_BYTE
Size of a byte in bytes.- See Also:
- Constant Field Values
-
SIZE_SHORT
public static final int SIZE_SHORT
Size of a short in bytes.- See Also:
- Constant Field Values
-
SIZE_INT
public static final int SIZE_INT
Size of an integer in bytes.- See Also:
- Constant Field Values
-
SIZE_LONG
public static final int SIZE_LONG
Size of a long in bytes.- See Also:
- Constant Field Values
-
SIZE_FLOAT
public static final int SIZE_FLOAT
Size of a float in bytes.- See Also:
- Constant Field Values
-
SIZE_DOUBLE
public static final int SIZE_DOUBLE
Size of a double in bytes.- See Also:
- Constant Field Values
-
BYTE_SERIALIZER
public static final ByteArrayUtil.ByteSerializer BYTE_SERIALIZER
Static instance.
-
SHORT_SERIALIZER
public static final ByteArrayUtil.ShortSerializer SHORT_SERIALIZER
Static instance.
-
INT_SERIALIZER
public static final ByteArrayUtil.IntegerSerializer INT_SERIALIZER
Static instance.
-
LONG_SERIALIZER
public static final ByteArrayUtil.LongSerializer LONG_SERIALIZER
Static instance.
-
FLOAT_SERIALIZER
public static final ByteArrayUtil.FloatSerializer FLOAT_SERIALIZER
Static instance.
-
DOUBLE_SERIALIZER
public static final ByteArrayUtil.DoubleSerializer DOUBLE_SERIALIZER
Static instance.
-
STRING_SERIALIZER
public static final ByteArrayUtil.StringSerializer STRING_SERIALIZER
Static instance.
-
VARINT_SERIALIZER
public static final ByteArrayUtil.VarintSerializer VARINT_SERIALIZER
Static instance.
-
-
Method Detail
-
writeShort
public static int writeShort(byte[] array, int offset, int v)Write a short to the byte array at the given offset.- Parameters:
array- Array to write tooffset- Offset to write tov- data- Returns:
- number of bytes written
-
writeInt
public static int writeInt(byte[] array, int offset, int v)Write an integer to the byte array at the given offset.- Parameters:
array- Array to write tooffset- Offset to write tov- data- Returns:
- number of bytes written
-
writeLong
public static int writeLong(byte[] array, int offset, long v)Write a long to the byte array at the given offset.- Parameters:
array- Array to write tooffset- Offset to write tov- data- Returns:
- number of bytes written
-
writeFloat
public static int writeFloat(byte[] array, int offset, float v)Write a float to the byte array at the given offset.- Parameters:
array- Array to write tooffset- Offset to write tov- data- Returns:
- number of bytes written
-
writeDouble
public static int writeDouble(byte[] array, int offset, double v)Write a double to the byte array at the given offset.- Parameters:
array- Array to write tooffset- Offset to write tov- data- Returns:
- number of bytes written
-
readShort
public static short readShort(byte[] array, int offset)Read a short from the byte array at the given offset.- Parameters:
array- Array to read fromoffset- Offset to read at- Returns:
- (signed) short
-
readUnsignedShort
public static int readUnsignedShort(byte[] array, int offset)Read an unsigned short from the byte array at the given offset.- Parameters:
array- Array to read fromoffset- Offset to read at- Returns:
- short
-
readInt
public static int readInt(byte[] array, int offset)Read an integer from the byte array at the given offset.- Parameters:
array- Array to read fromoffset- Offset to read at- Returns:
- data
-
readLong
public static long readLong(byte[] array, int offset)Read a long from the byte array at the given offset.- Parameters:
array- Array to read fromoffset- Offset to read at- Returns:
- data
-
readFloat
public static float readFloat(byte[] array, int offset)Read a float from the byte array at the given offset.- Parameters:
array- Array to read fromoffset- Offset to read at- Returns:
- data
-
readDouble
public static double readDouble(byte[] array, int offset)Read a double from the byte array at the given offset.- Parameters:
array- Array to read fromoffset- Offset to read at- Returns:
- data
-
writeSignedVarint
public static void writeSignedVarint(java.nio.ByteBuffer buffer, int val)Write an signed integer using a variable-length encoding.The sign bit is moved to bit 0.
Data is always written in 7-bit little-endian, where the 8th bit is the continuation flag.
- Parameters:
buffer- Buffer to write toval- number to write
-
writeSignedVarintLong
public static void writeSignedVarintLong(java.nio.ByteBuffer buffer, long val)Write a signed long using a variable-length encoding.The sign bit is moved to bit 0.
Data is always written in 7-bit little-endian, where the 8th bit is the continuation flag.
- Parameters:
buffer- Buffer to write toval- number to write
-
writeUnsignedVarint
public static void writeUnsignedVarint(java.nio.ByteBuffer buffer, int val)Write an unsigned integer using a variable-length encoding.Data is always written in 7-bit little-endian, where the 8th bit is the continuation flag.
- Parameters:
buffer- Buffer to write toval- number to write
-
writeUnsignedVarintLong
public static void writeUnsignedVarintLong(java.nio.ByteBuffer buffer, long val)Write an unsigned long using a variable-length encoding.Data is always written in 7-bit little-endian, where the 8th bit is the continuation flag.
Note that for integers, this will result in the same encoding as
writeUnsignedVarint(java.nio.ByteBuffer, int)- Parameters:
buffer- Buffer to write toval- number to write
-
writeString
public static void writeString(java.nio.ByteBuffer buffer, java.lang.String s) throws java.io.IOExceptionWrite a string to the buffer.See
ByteArrayUtil.StringSerializerfor details.- Parameters:
buffer- Buffer to write tos- String to write- Throws:
java.io.IOException
-
getSignedVarintSize
public static int getSignedVarintSize(int val)
Compute the size of the varint encoding for this signed integer.- Parameters:
val- integer to write- Returns:
- Encoding size of this integer
-
getUnsignedVarintSize
public static int getUnsignedVarintSize(int obj)
Compute the size of the varint encoding for this unsigned integer.- Parameters:
obj- integer to write- Returns:
- Encoding size of this integer
-
getSignedVarintLongSize
public static int getSignedVarintLongSize(long val)
Compute the size of the varint encoding for this signed integer.- Parameters:
val- integer to write- Returns:
- Encoding size of this integer
-
getUnsignedVarintLongSize
public static int getUnsignedVarintLongSize(long obj)
Compute the size of the varint encoding for this unsigned integer.- Parameters:
obj- integer to write- Returns:
- Encoding size of this integer
-
getStringSize
public static int getStringSize(java.lang.String s) throws java.io.IOExceptionCompute the size of the string after encoding.- Parameters:
s- String to encode- Returns:
- Byte size
- Throws:
java.io.IOException
-
readSignedVarint
public static int readSignedVarint(java.nio.ByteBuffer buffer) throws java.io.IOExceptionRead a signed integer.- Parameters:
buffer- Buffer to read from- Returns:
- Integer value
- Throws:
java.io.IOException
-
readUnsignedVarint
public static int readUnsignedVarint(java.nio.ByteBuffer buffer) throws java.io.IOExceptionRead an unsigned integer.- Parameters:
buffer- Buffer to read from- Returns:
- Integer value
- Throws:
java.io.IOException
-
readSignedVarintLong
public static long readSignedVarintLong(java.nio.ByteBuffer buffer) throws java.io.IOExceptionRead a signed long.- Parameters:
buffer- Buffer to read from- Returns:
- long value
- Throws:
java.io.IOException
-
readUnsignedVarintLong
public static long readUnsignedVarintLong(java.nio.ByteBuffer buffer) throws java.io.IOExceptionRead an unsigned long.- Parameters:
buffer- Buffer to read from- Returns:
- long value
- Throws:
java.io.IOException
-
readString
public static java.lang.String readString(java.nio.ByteBuffer buffer) throws java.io.IOExceptionRead a string from the buffer.Note: this is not 100% symmetric to writeString, as a
nullvalue and the empty string are encoded the same way.- Parameters:
buffer- Buffer to read from.- Returns:
- Deserialized string
- Throws:
java.io.IOException
-
-