com.jme.util.export
Class ByteUtils

java.lang.Object
  extended by com.jme.util.export.ByteUtils

public class ByteUtils
extends java.lang.Object

ByteUtils is a helper class for converting numeric primitives to and from byte representations.

Author:
Joshua Slack

Constructor Summary
ByteUtils()
           
 
Method Summary
static boolean convertBooleanFromBytes(byte[] byteArray)
           
static boolean convertBooleanFromBytes(byte[] byteArray, int offset)
           
static double convertDoubleFromBytes(byte[] bytes)
           
static double convertDoubleFromBytes(byte[] bytes, int offset)
           
static float convertFloatFromBytes(byte[] byteArray)
           
static float convertFloatFromBytes(byte[] byteArray, int offset)
           
static int convertIntFromBytes(byte[] byteArray)
           
static int convertIntFromBytes(byte[] byteArray, int offset)
           
static long convertLongFromBytes(byte[] bytes)
           
static long convertLongFromBytes(byte[] bytes, int offset)
           
static short convertShortFromBytes(byte[] byteArray)
           
static short convertShortFromBytes(byte[] byteArray, int offset)
           
static byte[] convertToBytes(boolean b)
           
static byte[] convertToBytes(double n)
           
static byte[] convertToBytes(float f)
           
static byte[] convertToBytes(int integer)
           
static byte[] convertToBytes(long n)
           
static byte[] convertToBytes(short value)
           
static byte[] getByteContent(java.io.InputStream inputStream)
          Takes an InputStream and returns the complete byte content of it
static boolean readBoolean(java.io.InputStream inputStream)
          Read in a boolean from an InputStream
static byte[] readData(byte[] store, int bytes, java.io.InputStream is)
          Properly reads in data from the given stream until the specified number of bytes have been read.
static double readDouble(java.io.InputStream inputStream)
          Read in a double from an InputStream
static float readFloat(java.io.InputStream inputStream)
          Read in a float from an InputStream
static int readInt(java.io.InputStream inputStream)
          Read in an integer from an InputStream
static long readLong(java.io.InputStream inputStream)
          Read in a long from an InputStream
static short readShort(java.io.InputStream inputStream)
          Read in a short from an InputStream
static byte[] rightAlignBytes(byte[] bytes, int width)
           
static void writeBoolean(java.io.OutputStream outputStream, boolean bVal)
          Writes a boolean out to an OutputStream.
static void writeDouble(java.io.OutputStream outputStream, double value)
          Writes a double out to an OutputStream.
static void writeFloat(java.io.OutputStream outputStream, float fVal)
          Writes an float out to an OutputStream.
static void writeInt(java.io.OutputStream outputStream, int integer)
          Writes an integer out to an OutputStream.
static void writeLong(java.io.OutputStream outputStream, long value)
          Writes a long out to an OutputStream.
static void writeShort(java.io.OutputStream outputStream, short value)
          Writes a short out to an OutputStream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteUtils

public ByteUtils()
Method Detail

getByteContent

public static byte[] getByteContent(java.io.InputStream inputStream)
                             throws java.io.IOException
Takes an InputStream and returns the complete byte content of it

Parameters:
inputStream - The input stream to read from
Returns:
The byte array containing the data from the input stream
Throws:
java.io.IOException - thrown if there is a problem reading from the input stream provided

writeShort

public static void writeShort(java.io.OutputStream outputStream,
                              short value)
                       throws java.io.IOException
Writes a short out to an OutputStream.

Parameters:
outputStream - The OutputStream the short will be written to
value - The short to write
Throws:
java.io.IOException - Thrown if there is a problem writing to the OutputStream

convertToBytes

public static byte[] convertToBytes(short value)

readShort

public static short readShort(java.io.InputStream inputStream)
                       throws java.io.IOException
Read in a short from an InputStream

Parameters:
inputStream - The InputStream used to read the short
Returns:
A short, which is the next 2 bytes converted from the InputStream
Throws:
java.io.IOException - Thrown if there is a problem reading from the InputStream

convertShortFromBytes

public static short convertShortFromBytes(byte[] byteArray)

convertShortFromBytes

public static short convertShortFromBytes(byte[] byteArray,
                                          int offset)

writeInt

public static void writeInt(java.io.OutputStream outputStream,
                            int integer)
                     throws java.io.IOException
Writes an integer out to an OutputStream.

Parameters:
outputStream - The OutputStream the integer will be written to
integer - The integer to write
Throws:
java.io.IOException - Thrown if there is a problem writing to the OutputStream

convertToBytes

public static byte[] convertToBytes(int integer)

readInt

public static int readInt(java.io.InputStream inputStream)
                   throws java.io.IOException
Read in an integer from an InputStream

Parameters:
inputStream - The InputStream used to read the integer
Returns:
An int, which is the next 4 bytes converted from the InputStream
Throws:
java.io.IOException - Thrown if there is a problem reading from the InputStream

convertIntFromBytes

public static int convertIntFromBytes(byte[] byteArray)

convertIntFromBytes

public static int convertIntFromBytes(byte[] byteArray,
                                      int offset)

writeLong

public static void writeLong(java.io.OutputStream outputStream,
                             long value)
                      throws java.io.IOException
Writes a long out to an OutputStream.

Parameters:
outputStream - The OutputStream the long will be written to
value - The long to write
Throws:
java.io.IOException - Thrown if there is a problem writing to the OutputStream

convertToBytes

public static byte[] convertToBytes(long n)

readLong

public static long readLong(java.io.InputStream inputStream)
                     throws java.io.IOException
Read in a long from an InputStream

Parameters:
inputStream - The InputStream used to read the long
Returns:
A long, which is the next 8 bytes converted from the InputStream
Throws:
java.io.IOException - Thrown if there is a problem reading from the InputStream

convertLongFromBytes

public static long convertLongFromBytes(byte[] bytes)

convertLongFromBytes

public static long convertLongFromBytes(byte[] bytes,
                                        int offset)

writeDouble

public static void writeDouble(java.io.OutputStream outputStream,
                               double value)
                        throws java.io.IOException
Writes a double out to an OutputStream.

Parameters:
outputStream - The OutputStream the double will be written to
value - The double to write
Throws:
java.io.IOException - Thrown if there is a problem writing to the OutputStream

convertToBytes

public static byte[] convertToBytes(double n)

readDouble

public static double readDouble(java.io.InputStream inputStream)
                         throws java.io.IOException
Read in a double from an InputStream

Parameters:
inputStream - The InputStream used to read the double
Returns:
A double, which is the next 8 bytes converted from the InputStream
Throws:
java.io.IOException - Thrown if there is a problem reading from the InputStream

convertDoubleFromBytes

public static double convertDoubleFromBytes(byte[] bytes)

convertDoubleFromBytes

public static double convertDoubleFromBytes(byte[] bytes,
                                            int offset)

writeFloat

public static void writeFloat(java.io.OutputStream outputStream,
                              float fVal)
                       throws java.io.IOException
Writes an float out to an OutputStream.

Parameters:
outputStream - The OutputStream the float will be written to
fVal - The float to write
Throws:
java.io.IOException - Thrown if there is a problem writing to the OutputStream

convertToBytes

public static byte[] convertToBytes(float f)

readFloat

public static float readFloat(java.io.InputStream inputStream)
                       throws java.io.IOException
Read in a float from an InputStream

Parameters:
inputStream - The InputStream used to read the float
Returns:
A float, which is the next 4 bytes converted from the InputStream
Throws:
java.io.IOException - Thrown if there is a problem reading from the InputStream

convertFloatFromBytes

public static float convertFloatFromBytes(byte[] byteArray)

convertFloatFromBytes

public static float convertFloatFromBytes(byte[] byteArray,
                                          int offset)

writeBoolean

public static void writeBoolean(java.io.OutputStream outputStream,
                                boolean bVal)
                         throws java.io.IOException
Writes a boolean out to an OutputStream.

Parameters:
outputStream - The OutputStream the boolean will be written to
bVal - The boolean to write
Throws:
java.io.IOException - Thrown if there is a problem writing to the OutputStream

convertToBytes

public static byte[] convertToBytes(boolean b)

readBoolean

public static boolean readBoolean(java.io.InputStream inputStream)
                           throws java.io.IOException
Read in a boolean from an InputStream

Parameters:
inputStream - The InputStream used to read the boolean
Returns:
A boolean, which is the next byte converted from the InputStream (iow, byte != 0)
Throws:
java.io.IOException - Thrown if there is a problem reading from the InputStream

convertBooleanFromBytes

public static boolean convertBooleanFromBytes(byte[] byteArray)

convertBooleanFromBytes

public static boolean convertBooleanFromBytes(byte[] byteArray,
                                              int offset)

readData

public static byte[] readData(byte[] store,
                              int bytes,
                              java.io.InputStream is)
                       throws java.io.IOException
Properly reads in data from the given stream until the specified number of bytes have been read.

Parameters:
store - the byte array to store in. Should have a length > bytes
bytes - the number of bytes to read.
is - the stream to read from
Returns:
the store array for chaining purposes
Throws:
java.io.IOException - if an error occurs while reading from the stream
java.lang.ArrayIndexOutOfBoundsException - if bytes greater than the length of the store.

rightAlignBytes

public static byte[] rightAlignBytes(byte[] bytes,
                                     int width)