uk.org.retep.util.io
Class BitUtils

java.lang.Object
  extended by uk.org.retep.util.io.BitUtils

public final class BitUtils
extends java.lang.Object

A set of methods to convert between byte[] and primitive types.

Author:
peter

Method Summary
static boolean getBoolean(byte[] b, int off)
          Return a boolean value
static char getChar(byte[] b, int off)
          Return a char value, encoded with 2 bytes
static double getDouble(byte[] b, int off)
          Return a double value
static float getFloat(byte[] b, int off)
          Return a float value
static int getInt(byte[] b, int off)
          Return a integer value
static int getInt3(byte[] b, int off)
          Return a integer value encoded with just 3 bytes instead of 4.
static long getLong(byte[] b, int off)
          Return a long value
static short getShort(byte[] b, int off)
          Return a short value
static java.util.UUID getUUID(byte[] b, int off)
          Return a UUID value.
static void setBoolean(byte[] b, int off, boolean val)
          Set a boolean value
static void setChar(byte[] b, int off, char val)
          Set a char value
static void setDouble(byte[] b, int off, double val)
          Set a double value
static void setFloat(byte[] b, int off, float val)
          Set a float value
static void setInt(byte[] b, int off, int val)
          Set a integer value
static void setInt3(byte[] b, int off, int val)
          Set a integer value using only 3 bytes for storage.
static void setLong(byte[] b, int off, long val)
          Set a long value
static void setShort(byte[] b, int off, short val)
          Set a short value
static void setUUID(byte[] b, int off, java.util.UUID val)
          Set a UUID value occupying 16 bytes from the offset
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getBoolean

public static boolean getBoolean(byte[] b,
                                 int off)
Return a boolean value

Parameters:
b - byte[]
off - offset in array
Returns:
boolean
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getChar

public static char getChar(byte[] b,
                           int off)
Return a char value, encoded with 2 bytes

Parameters:
b - byte[]
off - offset in array
Returns:
char
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getShort

public static short getShort(byte[] b,
                             int off)
Return a short value

Parameters:
b - byte[]
off - offset in array
Returns:
short
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getInt

public static int getInt(byte[] b,
                         int off)
Return a integer value

Parameters:
b - byte[]
off - offset in array
Returns:
int
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getInt3

public static int getInt3(byte[] b,
                          int off)
Return a integer value encoded with just 3 bytes instead of 4.

This is used when the value is known not to require all four bytes and space within the stream is an issue.

Parameters:
b - byte[]
off - offset in array
Returns:
int
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getFloat

public static float getFloat(byte[] b,
                             int off)
Return a float value

Parameters:
b - byte[]
off - offset in array
Returns:
float
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getLong

public static long getLong(byte[] b,
                           int off)
Return a long value

Parameters:
b - byte[]
off - offset in array
Returns:
long
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getDouble

public static double getDouble(byte[] b,
                               int off)
Return a double value

Parameters:
b - byte[]
off - offset in array
Returns:
double
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

getUUID

public static java.util.UUID getUUID(byte[] b,
                                     int off)
Return a UUID value. A UUID occupies 16 bytes from the offset.

Parameters:
b - byte[]
off - offset in array
Returns:
double
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setBoolean

public static void setBoolean(byte[] b,
                              int off,
                              boolean val)
Set a boolean value

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setChar

public static void setChar(byte[] b,
                           int off,
                           char val)
Set a char value

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setShort

public static void setShort(byte[] b,
                            int off,
                            short val)
Set a short value

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setInt

public static void setInt(byte[] b,
                          int off,
                          int val)
Set a integer value

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setInt3

public static void setInt3(byte[] b,
                           int off,
                           int val)
Set a integer value using only 3 bytes for storage.

This is used when the value is known not to require all four bytes and space within the stream is an issue.

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setFloat

public static void setFloat(byte[] b,
                            int off,
                            float val)
Set a float value

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setLong

public static void setLong(byte[] b,
                           int off,
                           long val)
Set a long value

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setDouble

public static void setDouble(byte[] b,
                             int off,
                             double val)
Set a double value

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value

setUUID

public static void setUUID(byte[] b,
                           int off,
                           java.util.UUID val)
Set a UUID value occupying 16 bytes from the offset

Parameters:
b - byte[]
off - offset in array
val - value to store
Throws:
java.lang.ArrayIndexOutOfBoundsException - if offset is out of bounds or there is not enough elements at offset for the value


Copyright © 1998-2010 Retep Development Group. All Rights Reserved.