de.lmu.ifi.dbs.elki.persistent
Class ByteArrayUtil

java.lang.Object
  extended by de.lmu.ifi.dbs.elki.persistent.ByteArrayUtil

public final class ByteArrayUtil
extends Object

Class with various utilities for manipulating byte arrays. If you find a reusable copy of this in the Java API, please tell me. Using a ByteArrayOutputStream and DataInputStream doesn't seem appropriate. C.f. DataOutputStream and ByteArrayOutputStream


Nested Class Summary
static class ByteArrayUtil.ByteSerializer
          Serializer for byte objects
static class ByteArrayUtil.DoubleSerializer
          Serializer for double objects
static class ByteArrayUtil.FloatSerializer
          Serializer for float objects
static class ByteArrayUtil.IntegerSerializer
          Serializer for integer objects
static class ByteArrayUtil.LongSerializer
          Serializer for long objects
static class ByteArrayUtil.ShortSerializer
          Serializer for short objects
static class ByteArrayUtil.StringSerializer
          Serializer for String objects
static class ByteArrayUtil.VarintSerializer
          Serializer for Integer objects using a variable size encoding
 
Field Summary
static ByteArrayUtil.ByteSerializer BYTE_SERIALIZER
          Static instance.
static ByteArrayUtil.DoubleSerializer DOUBLE_SERIALIZER
          Static instance.
static ByteArrayUtil.FloatSerializer FLOAT_SERIALIZER
          Static instance.
static ByteArrayUtil.IntegerSerializer INT_SERIALIZER
          Static instance.
static ByteArrayUtil.LongSerializer LONG_SERIALIZER
          Static instance.
static ByteArrayUtil.ShortSerializer SHORT_SERIALIZER
          Static instance.
static int SIZE_BYTE
          Size of a byte in bytes.
static int SIZE_DOUBLE
          Size of a double in bytes.
static int SIZE_FLOAT
          Size of a float in bytes.
static int SIZE_INT
          Size of an integer in bytes.
static int SIZE_LONG
          Size of a long in bytes.
static int SIZE_SHORT
          Size of a short in bytes.
static ByteArrayUtil.StringSerializer STRING_SERIALIZER
          Static instance.
static ByteArrayUtil.VarintSerializer VARINT_SERIALIZER
          Static instance.
 
Constructor Summary
ByteArrayUtil()
           
 
Method Summary
static int getSignedVarintLongSize(long val)
          Compute the size of the varint encoding for this signed integer
static int getSignedVarintSize(int val)
          Compute the size of the varint encoding for this signed integer
static int getUnsignedVarintLongSize(long obj)
          Compute the size of the varint encoding for this unsigned integer
static int getUnsignedVarintSize(int obj)
          Compute the size of the varint encoding for this unsigned integer
static double readDouble(byte[] array, int offset)
          Read a double from the byte array at the given offset.
static float readFloat(byte[] array, int offset)
          Read a float from the byte array at the given offset.
static int readInt(byte[] array, int offset)
          Read an integer from the byte array at the given offset.
static long readLong(byte[] array, int offset)
          Read a long from the byte array at the given offset.
static short readShort(byte[] array, int offset)
          Read a short from the byte array at the given offset.
static int readSignedVarint(ByteBuffer buffer)
          Read a signed integer.
static long readSignedVarintLong(ByteBuffer buffer)
          Read a signed long.
static int readUnsignedShort(byte[] array, int offset)
          Read an unsigned short from the byte array at the given offset.
static int readUnsignedVarint(ByteBuffer buffer)
          Read an unsigned integer.
static long readUnsignedVarintLong(ByteBuffer buffer)
          Read an unsigned long.
static void unmapByteBuffer(MappedByteBuffer map)
          Unmap a byte buffer.
static int writeDouble(byte[] array, int offset, double v)
          Write a double to the byte array at the given offset.
static int writeFloat(byte[] array, int offset, float v)
          Write a float to the byte array at the given offset.
static int writeInt(byte[] array, int offset, int v)
          Write an integer to the byte array at the given offset.
static int writeLong(byte[] array, int offset, long v)
          Write a long to the byte array at the given offset.
static int writeShort(byte[] array, int offset, int v)
          Write a short to the byte array at the given offset.
static void writeSignedVarint(ByteBuffer buffer, int val)
          Write an signed integer using a variable-length encoding.
static void writeSignedVarintLong(ByteBuffer buffer, long val)
          Write a signed long using a variable-length encoding.
static void writeUnsignedVarint(ByteBuffer buffer, int val)
          Write an unsigned integer using a variable-length encoding.
static void writeUnsignedVarintLong(ByteBuffer buffer, long val)
          Write an unsigned long using a variable-length encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

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.

Constructor Detail

ByteArrayUtil

public ByteArrayUtil()
Method Detail

writeShort

public static final int writeShort(byte[] array,
                                   int offset,
                                   int v)
Write a short to the byte array at the given offset.

Parameters:
array - Array to write to
offset - Offset to write to
v - data
Returns:
number of bytes written

writeInt

public static final int writeInt(byte[] array,
                                 int offset,
                                 int v)
Write an integer to the byte array at the given offset.

Parameters:
array - Array to write to
offset - Offset to write to
v - data
Returns:
number of bytes written

writeLong

public static final int writeLong(byte[] array,
                                  int offset,
                                  long v)
Write a long to the byte array at the given offset.

Parameters:
array - Array to write to
offset - Offset to write to
v - data
Returns:
number of bytes written

writeFloat

public static final int writeFloat(byte[] array,
                                   int offset,
                                   float v)
Write a float to the byte array at the given offset.

Parameters:
array - Array to write to
offset - Offset to write to
v - data
Returns:
number of bytes written

writeDouble

public static final int writeDouble(byte[] array,
                                    int offset,
                                    double v)
Write a double to the byte array at the given offset.

Parameters:
array - Array to write to
offset - Offset to write to
v - data
Returns:
number of bytes written

readShort

public static final short readShort(byte[] array,
                                    int offset)
Read a short from the byte array at the given offset.

Parameters:
array - Array to read from
offset - Offset to read at
Returns:
(signed) short

readUnsignedShort

public static final int readUnsignedShort(byte[] array,
                                          int offset)
Read an unsigned short from the byte array at the given offset.

Parameters:
array - Array to read from
offset - Offset to read at
Returns:
short

readInt

public static final int readInt(byte[] array,
                                int offset)
Read an integer from the byte array at the given offset.

Parameters:
array - Array to read from
offset - Offset to read at
Returns:
data

readLong

public static final long readLong(byte[] array,
                                  int offset)
Read a long from the byte array at the given offset.

Parameters:
array - Array to read from
offset - Offset to read at
Returns:
data

readFloat

public static final float readFloat(byte[] array,
                                    int offset)
Read a float from the byte array at the given offset.

Parameters:
array - Array to read from
offset - Offset to read at
Returns:
data

readDouble

public static final double readDouble(byte[] array,
                                      int offset)
Read a double from the byte array at the given offset.

Parameters:
array - Array to read from
offset - Offset to read at
Returns:
data

writeSignedVarint

public static final void writeSignedVarint(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 to
val - number to write

writeSignedVarintLong

public static final void writeSignedVarintLong(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 to
val - number to write

writeUnsignedVarint

public static final void writeUnsignedVarint(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 to
val - number to write

writeUnsignedVarintLong

public static final void writeUnsignedVarintLong(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 to
val - number to write

getSignedVarintSize

public static final 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 final 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 final 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 final 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

readSignedVarint

public static final int readSignedVarint(ByteBuffer buffer)
Read a signed integer.

Parameters:
buffer - Buffer to read from
Returns:
Integer value

readUnsignedVarint

public static final int readUnsignedVarint(ByteBuffer buffer)
Read an unsigned integer.

Parameters:
buffer - Buffer to read from
Returns:
Integer value

readSignedVarintLong

public static final long readSignedVarintLong(ByteBuffer buffer)
Read a signed long.

Parameters:
buffer - Buffer to read from
Returns:
long value

readUnsignedVarintLong

public static final long readUnsignedVarintLong(ByteBuffer buffer)
Read an unsigned long.

Parameters:
buffer - Buffer to read from
Returns:
long value

unmapByteBuffer

public static void unmapByteBuffer(MappedByteBuffer map)
Unmap a byte buffer.

Parameters:
map - Byte buffer to unmap.

Release 0.4.0 (2011-09-20_1324)