Class ByteUtils

java.lang.Object
com.jme3.export.binary.ByteUtils

public class ByteUtils extends Object
ByteUtils is a helper class for converting numeric primitives to and from byte representations.
  • Method Details

    • getByteContent

      public static byte[] getByteContent(InputStream inputStream) throws 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:
      IOException - thrown if there is a problem reading from the input stream provided
    • writeShort

      public static void writeShort(OutputStream outputStream, short value) throws IOException
      Writes a short out to an OutputStream.
      Parameters:
      outputStream - The OutputStream the short will be written to
      value - The short to write
      Throws:
      IOException - Thrown if there is a problem writing to the OutputStream
    • convertToBytes

      public static byte[] convertToBytes(short value)
    • readShort

      public static short readShort(InputStream inputStream) throws 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:
      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(OutputStream outputStream, int integer) throws IOException
      Writes an integer out to an OutputStream.
      Parameters:
      outputStream - The OutputStream the integer will be written to
      integer - The integer to write
      Throws:
      IOException - Thrown if there is a problem writing to the OutputStream
    • convertToBytes

      public static byte[] convertToBytes(int integer)
    • readInt

      public static int readInt(InputStream inputStream) throws 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:
      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(OutputStream outputStream, long value) throws IOException
      Writes a long out to an OutputStream.
      Parameters:
      outputStream - The OutputStream the long will be written to
      value - The long to write
      Throws:
      IOException - Thrown if there is a problem writing to the OutputStream
    • convertToBytes

      public static byte[] convertToBytes(long n)
    • readLong

      public static long readLong(InputStream inputStream) throws 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:
      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(OutputStream outputStream, double value) throws IOException
      Writes a double out to an OutputStream.
      Parameters:
      outputStream - The OutputStream the double will be written to
      value - The double to write
      Throws:
      IOException - Thrown if there is a problem writing to the OutputStream
    • convertToBytes

      public static byte[] convertToBytes(double n)
    • readDouble

      public static double readDouble(InputStream inputStream) throws 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:
      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(OutputStream outputStream, float fVal) throws IOException
      Writes a float out to an OutputStream.
      Parameters:
      outputStream - The OutputStream the float will be written to
      fVal - The float to write
      Throws:
      IOException - Thrown if there is a problem writing to the OutputStream
    • convertToBytes

      public static byte[] convertToBytes(float f)
    • readFloat

      public static float readFloat(InputStream inputStream) throws 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:
      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(OutputStream outputStream, boolean bVal) throws IOException
      Writes a boolean out to an OutputStream.
      Parameters:
      outputStream - The OutputStream the boolean will be written to
      bVal - The boolean to write
      Throws:
      IOException - Thrown if there is a problem writing to the OutputStream
    • convertToBytes

      public static byte[] convertToBytes(boolean b)
    • readBoolean

      public static boolean readBoolean(InputStream inputStream) throws 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:
      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, InputStream is) throws 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. Array length must be greater than bytes param.
      bytes - the number of bytes to read.
      is - the stream to read from
      Returns:
      the store array for chaining purposes
      Throws:
      IOException - if an error occurs while reading from the stream
      ArrayIndexOutOfBoundsException - if bytes greater than the length of the store.
    • rightAlignBytes

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