This project has retired. For details please refer to its Attic page.
ByteUtils (Apache Gora 0.2 API)

org.apache.gora.util
Class ByteUtils

java.lang.Object
  extended by org.apache.gora.util.ByteUtils

public class ByteUtils
extends Object

Utility class that handles byte arrays, conversions to/from other types.


Field Summary
static int SIZEOF_BOOLEAN
          Size of boolean in bytes
static int SIZEOF_BYTE
          Size of byte in bytes
static int SIZEOF_CHAR
          Size of char in bytes
static int SIZEOF_DOUBLE
          Size of double in bytes
static int SIZEOF_FLOAT
          Size of float in bytes
static int SIZEOF_INT
          Size of int in bytes
static int SIZEOF_LONG
          Size of long in bytes
static int SIZEOF_SHORT
          Size of short in bytes
 
Constructor Summary
ByteUtils()
           
 
Method Summary
static int bytesToVint(byte[] buffer)
           
static long bytesToVlong(byte[] buffer)
           
static int compareTo(byte[] left, byte[] right)
           
static int compareTo(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2)
           
static boolean equals(byte[] left, byte[] right)
           
static Object fromBytes(byte[] val, org.apache.avro.Schema schema, PersistentDatumReader<?> datumReader, Object object)
           
static int putByte(byte[] bytes, int offset, byte b)
          Write a single byte out to the specified byte array position.
static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes, int srcOffset, int srcLength)
          Put bytes at the specified byte array position.
static long readVLong(byte[] buffer, int offset)
          Reads a zero-compressed encoded long from input stream and returns it.
static boolean toBoolean(byte[] b)
           
static byte[] toBytes(boolean b)
          Convert a boolean to a byte array.
static byte[] toBytes(ByteBuffer bb)
          Returns a new byte array, copied from the passed ByteBuffer.
static byte[] toBytes(char val)
          Convert a char value to a byte array
static byte[] toBytes(double d)
           
static byte[] toBytes(float f)
           
static byte[] toBytes(int val)
          Convert an int value to a byte array
static byte[] toBytes(long val)
          Convert a long value to a byte array
static byte[] toBytes(Object o, org.apache.avro.Schema schema, PersistentDatumWriter<?> datumWriter)
           
static byte[] toBytes(short val)
          Convert a short value to a byte array
static byte[] toBytes(String s)
          Converts a string to a UTF-8 byte array.
static char toChar(byte[] bytes)
          Converts a byte array to a char value
static char toChar(byte[] bytes, int offset)
          Converts a byte array to a char value
static char toChar(byte[] bytes, int offset, int length)
          Converts a byte array to a char value
static char[] toChars(byte[] bytes)
          Converts a byte array to a char array value
static char[] toChars(byte[] bytes, int offset)
          Converts a byte array to a char array value
static char[] toChars(byte[] bytes, int offset, int length)
          Converts a byte array to a char array value
static double toDouble(byte[] bytes)
           
static double toDouble(byte[] bytes, int offset)
           
static float toFloat(byte[] bytes)
          Presumes float encoded as IEEE 754 floating-point "single format"
static float toFloat(byte[] bytes, int offset)
          Presumes float encoded as IEEE 754 floating-point "single format"
static int toInt(byte[] bytes)
          Converts a byte array to an int value
static int toInt(byte[] bytes, int offset)
          Converts a byte array to an int value
static int toInt(byte[] bytes, int offset, int length)
          Converts a byte array to an int value
static long toLong(byte[] bytes)
          Converts a byte array to a long value
static long toLong(byte[] bytes, int offset)
          Converts a byte array to a long value
static long toLong(byte[] bytes, int offset, int length)
          Converts a byte array to a long value
static short toShort(byte[] bytes)
          Converts a byte array to a short value
static short toShort(byte[] bytes, int offset)
          Converts a byte array to a short value
static short toShort(byte[] bytes, int offset, int length)
          Converts a byte array to a short value
static String toString(byte[] b)
           
static String toString(byte[] b, int off, int len)
           
static String toString(byte[] b1, String sep, byte[] b2)
           
static byte[] vintToBytes(long vint)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZEOF_BOOLEAN

public static final int SIZEOF_BOOLEAN
Size of boolean in bytes

See Also:
Constant Field Values

SIZEOF_BYTE

public static final int SIZEOF_BYTE
Size of byte in bytes

See Also:
Constant Field Values

SIZEOF_CHAR

public static final int SIZEOF_CHAR
Size of char in bytes

See Also:
Constant Field Values

SIZEOF_DOUBLE

public static final int SIZEOF_DOUBLE
Size of double in bytes

See Also:
Constant Field Values

SIZEOF_FLOAT

public static final int SIZEOF_FLOAT
Size of float in bytes

See Also:
Constant Field Values

SIZEOF_INT

public static final int SIZEOF_INT
Size of int in bytes

See Also:
Constant Field Values

SIZEOF_LONG

public static final int SIZEOF_LONG
Size of long in bytes

See Also:
Constant Field Values

SIZEOF_SHORT

public static final int SIZEOF_SHORT
Size of short in bytes

See Also:
Constant Field Values
Constructor Detail

ByteUtils

public ByteUtils()
Method Detail

putBytes

public static int putBytes(byte[] tgtBytes,
                           int tgtOffset,
                           byte[] srcBytes,
                           int srcOffset,
                           int srcLength)
Put bytes at the specified byte array position.

Parameters:
tgtBytes - the byte array
tgtOffset - position in the array
srcBytes - byte to write out
srcOffset -
srcLength -
Returns:
incremented offset

putByte

public static int putByte(byte[] bytes,
                          int offset,
                          byte b)
Write a single byte out to the specified byte array position.

Parameters:
bytes - the byte array
offset - position in the array
b - byte to write out
Returns:
incremented offset

toBytes

public static byte[] toBytes(ByteBuffer bb)
Returns a new byte array, copied from the passed ByteBuffer.

Parameters:
bb - A ByteBuffer
Returns:
the byte array

toString

public static String toString(byte[] b)
Parameters:
b - Presumed UTF-8 encoded byte array.
Returns:
String made from b

toString

public static String toString(byte[] b1,
                              String sep,
                              byte[] b2)

toString

public static String toString(byte[] b,
                              int off,
                              int len)
Parameters:
b - Presumed UTF-8 encoded byte array.
off -
len -
Returns:
String made from b

toBytes

public static byte[] toBytes(String s)
Converts a string to a UTF-8 byte array.

Parameters:
s -
Returns:
the byte array

toBytes

public static byte[] toBytes(boolean b)
Convert a boolean to a byte array.

Parameters:
b -
Returns:
b encoded in a byte array.

toBoolean

public static boolean toBoolean(byte[] b)
Parameters:
b -
Returns:
True or false.

toBytes

public static byte[] toBytes(long val)
Convert a long value to a byte array

Parameters:
val -
Returns:
the byte array

toLong

public static long toLong(byte[] bytes)
Converts a byte array to a long value

Parameters:
bytes -
Returns:
the long value

toLong

public static long toLong(byte[] bytes,
                          int offset)
Converts a byte array to a long value

Parameters:
bytes -
offset -
Returns:
the long value

toLong

public static long toLong(byte[] bytes,
                          int offset,
                          int length)
Converts a byte array to a long value

Parameters:
bytes -
offset -
length -
Returns:
the long value

toFloat

public static float toFloat(byte[] bytes)
Presumes float encoded as IEEE 754 floating-point "single format"

Parameters:
bytes -
Returns:
Float made from passed byte array.

toFloat

public static float toFloat(byte[] bytes,
                            int offset)
Presumes float encoded as IEEE 754 floating-point "single format"

Parameters:
bytes -
offset -
Returns:
Float made from passed byte array.

toBytes

public static byte[] toBytes(float f)
Parameters:
f -
Returns:
the float represented as byte []

toDouble

public static double toDouble(byte[] bytes)
Parameters:
bytes -
Returns:
Return double made from passed bytes.

toDouble

public static double toDouble(byte[] bytes,
                              int offset)
Parameters:
bytes -
offset -
Returns:
Return double made from passed bytes.

toBytes

public static byte[] toBytes(double d)
Parameters:
d -
Returns:
the double represented as byte []

toBytes

public static byte[] toBytes(int val)
Convert an int value to a byte array

Parameters:
val -
Returns:
the byte array

toInt

public static int toInt(byte[] bytes)
Converts a byte array to an int value

Parameters:
bytes -
Returns:
the int value

toInt

public static int toInt(byte[] bytes,
                        int offset)
Converts a byte array to an int value

Parameters:
bytes -
offset -
Returns:
the int value

toInt

public static int toInt(byte[] bytes,
                        int offset,
                        int length)
Converts a byte array to an int value

Parameters:
bytes -
offset -
length -
Returns:
the int value

toBytes

public static byte[] toBytes(short val)
Convert a short value to a byte array

Parameters:
val -
Returns:
the byte array

toShort

public static short toShort(byte[] bytes)
Converts a byte array to a short value

Parameters:
bytes -
Returns:
the short value

toShort

public static short toShort(byte[] bytes,
                            int offset)
Converts a byte array to a short value

Parameters:
bytes -
offset -
Returns:
the short value

toShort

public static short toShort(byte[] bytes,
                            int offset,
                            int length)
Converts a byte array to a short value

Parameters:
bytes -
offset -
length -
Returns:
the short value

toBytes

public static byte[] toBytes(char val)
Convert a char value to a byte array

Parameters:
val -
Returns:
the byte array

toChar

public static char toChar(byte[] bytes)
Converts a byte array to a char value

Parameters:
bytes -
Returns:
the char value

toChar

public static char toChar(byte[] bytes,
                          int offset)
Converts a byte array to a char value

Parameters:
bytes -
offset -
Returns:
the char value

toChar

public static char toChar(byte[] bytes,
                          int offset,
                          int length)
Converts a byte array to a char value

Parameters:
bytes -
offset -
length -
Returns:
the char value

toChars

public static char[] toChars(byte[] bytes)
Converts a byte array to a char array value

Parameters:
bytes -
Returns:
the char value

toChars

public static char[] toChars(byte[] bytes,
                             int offset)
Converts a byte array to a char array value

Parameters:
bytes -
offset -
Returns:
the char value

toChars

public static char[] toChars(byte[] bytes,
                             int offset,
                             int length)
Converts a byte array to a char array value

Parameters:
bytes -
offset -
length -
Returns:
the char value

vintToBytes

public static byte[] vintToBytes(long vint)
Parameters:
vint - Integer to make a vint of.
Returns:
Vint as bytes array.

bytesToVlong

public static long bytesToVlong(byte[] buffer)
Parameters:
buffer -
Returns:
vint bytes as an integer.

bytesToVint

public static int bytesToVint(byte[] buffer)
Parameters:
buffer -
Returns:
vint bytes as an integer.

readVLong

public static long readVLong(byte[] buffer,
                             int offset)
                      throws IOException
Reads a zero-compressed encoded long from input stream and returns it.

Parameters:
buffer - Binary array
offset - Offset into array at which vint begins.
Returns:
deserialized long from stream.
Throws:
IOException

compareTo

public static int compareTo(byte[] left,
                            byte[] right)
Parameters:
left -
right -
Returns:
0 if equal, < 0 if left is less than right, etc.

compareTo

public static int compareTo(byte[] b1,
                            int s1,
                            int l1,
                            byte[] b2,
                            int s2,
                            int l2)
Parameters:
b1 -
b2 -
s1 - Where to start comparing in the left buffer
s2 - Where to start comparing in the right buffer
l1 - How much to compare from the left buffer
l2 - How much to compare from the right buffer
Returns:
0 if equal, < 0 if left is less than right, etc.

equals

public static boolean equals(byte[] left,
                             byte[] right)
Parameters:
left -
right -
Returns:
True if equal

fromBytes

public static Object fromBytes(byte[] val,
                               org.apache.avro.Schema schema,
                               PersistentDatumReader<?> datumReader,
                               Object object)
                        throws IOException
Throws:
IOException

toBytes

public static byte[] toBytes(Object o,
                             org.apache.avro.Schema schema,
                             PersistentDatumWriter<?> datumWriter)
                      throws IOException
Throws:
IOException


Copyright © 2010-2013 The Apache Software Foundation. All Rights Reserved.