org.magnos.data
Class Bits

java.lang.Object
  extended by org.magnos.data.Bits

public class Bits
extends java.lang.Object

A utility for converting between primitive data types and an array of bytes.

Author:
Philip Diffenderfer

Constructor Summary
Bits()
           
 
Method Summary
static void free(java.nio.ByteBuffer buffer)
          Explicitly frees the given buffer from memory if it is a DirectBuffer.
static boolean getBoolean(byte b)
          Given an array of bytes, the first 4 bytes of the array are used to compute a floating-point value.
static byte getBooleanBytes(boolean x)
          Given a boolean this will convert it to its byte value.
static char getChar(byte[] b)
          Given an array of bytes, the first 2 bytes of the array are used to compute a char value.
static byte[] getCharBytes(char x)
          Given a char an array of bytes will be returned representing the char.
static void getCharBytes(char x, byte[] b)
          Given a char and an array of bytes, the first 2 bytes of the array are set to the bytes that make up the char.
static double getDouble(byte[] b)
          Given an array of bytes, the first 8 bytes of the array are used to compute a double value.
static byte[] getDoubleBytes(double x)
          Given a double an array of bytes will be returned representing the double.
static void getDoubleBytes(double x, byte[] b)
          Given a double and an array of bytes, the first 8 bytes of the array are set to the bytes that make up the double.
static float getFloat(byte[] b)
          Given an array of bytes, the first 4 bytes of the array are used to compute a floating-point value.
static byte[] getFloatBytes(float x)
          Given a float an array of bytes will be returned representing the float.
static void getFloatBytes(float x, byte[] b)
          Given a float and an array of bytes, the first 4 bytes of the array are set to the bytes that make up the float.
static int getInt(byte[] b)
          Given an array of bytes, the first 4 bytes of the array are used to compute an integer value.
static byte[] getIntBytes(int x)
          Given an integer an array of bytes will be returned representing the integer.
static void getIntBytes(int x, byte[] b)
          Given an integer and an array of bytes, the first 4 bytes of the array are set to the bytes that make up the integer.
static long getLong(byte[] b)
          Given an array of bytes, the first 8 bytes of the array are used to compute a long value.
static byte[] getLongBytes(long x)
          Given a long an array of bytes will be returned representing the long.
static void getLongBytes(long x, byte[] b)
          Given a long and an array of bytes, the first 8 bytes of the array are set to the bytes that make up the long.
static short getShort(byte[] b)
          Given an array of bytes, the first 2 bytes of the array are used to compute a short value.
static byte[] getShortBytes(short x)
          Given a short an array of bytes will be returned representing the short.
static void getShortBytes(short x, byte[] b)
          Given a short and an array of bytes, the first 2 bytes of the array are set to the bytes that make up the short.
static short getUByte(byte[] b)
          Given an array of bytes, the first byte of the array are used to compute a short value.
static byte[] getUByteBytes(short x)
          Given a short an array of bytes will be returned representing the short.
static void getUByteBytes(short x, byte[] b)
          Given a short and an array of bytes, the first byetshort of the array are set to the bytes that make up the short.
static long getUInt(byte[] b)
          Given an array of bytes, the first 4 bytes of the array are used to compute an integer value.
static byte[] getUIntBytes(long x)
          Given an integer an array of bytes will be returned representing the integer.
static void getUIntBytes(long x, byte[] b)
          Given an integer and an array of bytes, the first 4 bytes of the array are set to the bytes that make up the integer.
static int getUShort(byte[] b)
          Given an array of bytes, the first 2 bytes of the array are used to compute a short value.
static byte[] getUShortBytes(int x)
          Given a short an array of bytes will be returned representing the short.
static void getUShortBytes(int x, byte[] b)
          Given a short and an array of bytes, the first 2 bytes of the array are set to the bytes that make up the short.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Bits

public Bits()
Method Detail

getBoolean

public static boolean getBoolean(byte b)
Given an array of bytes, the first 4 bytes of the array are used to compute a floating-point value.

Parameters:
b - The array of bytes.
Returns:
A float taken from the bytes.

getBooleanBytes

public static byte getBooleanBytes(boolean x)
Given a boolean this will convert it to its byte value.

Parameters:
x - The boolean to convert.
Returns:
The byte representation of x.

getUByte

public static short getUByte(byte[] b)
Given an array of bytes, the first byte of the array are used to compute a short value.

Parameters:
b - The array of bytes.
Returns:
A short taken from the bytes.

getUByteBytes

public static void getUByteBytes(short x,
                                 byte[] b)
Given a short and an array of bytes, the first byetshort of the array are set to the bytes that make up the short.

Parameters:
x - The short to place in the byte array.
b - The byte array to place the bytes.

getUByteBytes

public static byte[] getUByteBytes(short x)
Given a short an array of bytes will be returned representing the short.

Parameters:
x - The short to return as a byte array.
Returns:
An array of bytes (length 1).

getChar

public static char getChar(byte[] b)
Given an array of bytes, the first 2 bytes of the array are used to compute a char value.

Parameters:
b - The array of bytes.
Returns:
A char taken from the bytes.

getCharBytes

public static void getCharBytes(char x,
                                byte[] b)
Given a char and an array of bytes, the first 2 bytes of the array are set to the bytes that make up the char.

Parameters:
x - The char to place in the byte array.
b - The byte array to place the bytes.

getCharBytes

public static byte[] getCharBytes(char x)
Given a char an array of bytes will be returned representing the char.

Parameters:
x - The char to return as a byte array.
Returns:
An array of bytes (length 2).

getShort

public static short getShort(byte[] b)
Given an array of bytes, the first 2 bytes of the array are used to compute a short value.

Parameters:
b - The array of bytes.
Returns:
A short taken from the bytes.

getShortBytes

public static void getShortBytes(short x,
                                 byte[] b)
Given a short and an array of bytes, the first 2 bytes of the array are set to the bytes that make up the short.

Parameters:
x - The short to place in the byte array.
b - The byte array to place the bytes.

getShortBytes

public static byte[] getShortBytes(short x)
Given a short an array of bytes will be returned representing the short.

Parameters:
x - The short to return as a byte array.
Returns:
An array of bytes (length 2).

getUShort

public static int getUShort(byte[] b)
Given an array of bytes, the first 2 bytes of the array are used to compute a short value.

Parameters:
b - The array of bytes.
Returns:
A short taken from the bytes.

getUShortBytes

public static void getUShortBytes(int x,
                                  byte[] b)
Given a short and an array of bytes, the first 2 bytes of the array are set to the bytes that make up the short.

Parameters:
x - The short to place in the byte array.
b - The byte array to place the bytes.

getUShortBytes

public static byte[] getUShortBytes(int x)
Given a short an array of bytes will be returned representing the short.

Parameters:
x - The short to return as a byte array.
Returns:
An array of bytes (length 2).

getInt

public static int getInt(byte[] b)
Given an array of bytes, the first 4 bytes of the array are used to compute an integer value.

Parameters:
b - The array of bytes.
Returns:
An integer taken from the bytes.

getIntBytes

public static void getIntBytes(int x,
                               byte[] b)
Given an integer and an array of bytes, the first 4 bytes of the array are set to the bytes that make up the integer.

Parameters:
x - The integer to place in the byte array.
b - The byte array to place the bytes.

getIntBytes

public static byte[] getIntBytes(int x)
Given an integer an array of bytes will be returned representing the integer.

Parameters:
x - The integer to return as a byte array.
Returns:
An array of bytes (length 4).

getUInt

public static long getUInt(byte[] b)
Given an array of bytes, the first 4 bytes of the array are used to compute an integer value.

Parameters:
b - The array of bytes.
Returns:
An integer taken from the bytes.

getUIntBytes

public static void getUIntBytes(long x,
                                byte[] b)
Given an integer and an array of bytes, the first 4 bytes of the array are set to the bytes that make up the integer.

Parameters:
x - The integer to place in the byte array.
b - The byte array to place the bytes.

getUIntBytes

public static byte[] getUIntBytes(long x)
Given an integer an array of bytes will be returned representing the integer.

Parameters:
x - The integer to return as a byte array.
Returns:
An array of bytes (length 4).

getLong

public static long getLong(byte[] b)
Given an array of bytes, the first 8 bytes of the array are used to compute a long value.

Parameters:
b - The array of bytes.
Returns:
A long taken from the bytes.

getLongBytes

public static void getLongBytes(long x,
                                byte[] b)
Given a long and an array of bytes, the first 8 bytes of the array are set to the bytes that make up the long.

Parameters:
x - The long to place in the byte array.
b - The byte array to place the bytes.

getLongBytes

public static byte[] getLongBytes(long x)
Given a long an array of bytes will be returned representing the long.

Parameters:
x - The long to return as a byte array.
Returns:
An array of bytes (length 8).

getFloat

public static float getFloat(byte[] b)
Given an array of bytes, the first 4 bytes of the array are used to compute a floating-point value.

Parameters:
b - The array of bytes.
Returns:
A float taken from the bytes.

getFloatBytes

public static void getFloatBytes(float x,
                                 byte[] b)
Given a float and an array of bytes, the first 4 bytes of the array are set to the bytes that make up the float.

Parameters:
x - The float to place in the byte array.
b - The byte array to place the bytes.

getFloatBytes

public static byte[] getFloatBytes(float x)
Given a float an array of bytes will be returned representing the float.

Parameters:
x - The float to return as a byte array.
Returns:
An array of bytes (length 4).

getDouble

public static double getDouble(byte[] b)
Given an array of bytes, the first 8 bytes of the array are used to compute a double value.

Parameters:
b - The array of bytes.
Returns:
A double taken from the bytes.

getDoubleBytes

public static void getDoubleBytes(double x,
                                  byte[] b)
Given a double and an array of bytes, the first 8 bytes of the array are set to the bytes that make up the double.

Parameters:
x - The double to place in the byte array.
b - The byte array to place the bytes.

getDoubleBytes

public static byte[] getDoubleBytes(double x)
Given a double an array of bytes will be returned representing the double.

Parameters:
x - The double to return as a byte array.
Returns:
An array of bytes (length 8).

free

public static void free(java.nio.ByteBuffer buffer)
Explicitly frees the given buffer from memory if it is a DirectBuffer. The data in a DirectBuffer lies outside of JVM space, a DirectBuffer is either a DirectByteBuffer or a MappedByteBuffer. The given buffer should never be used again, if it is - serious errors can occur.

Parameters:
buffer - The buffer to forcably free from memory if possible.