Class ArrayConverter

java.lang.Object
net.sourceforge.jiu.util.ArrayConverter

public class ArrayConverter extends Object
Helper class with static methods to convert between byte arrays and primitive types. Useful for serialization.
Since:
0.9.0
Author:
Marco Schmidt
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
     
    private static final int
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
     
  • Method Summary

    Modifier and Type
    Method
    Description
    private static void
    checkArray(byte[] array, int offset, int length)
    Makes sure that the arguments define a valid (existing) array interval.
    static void
    convertPacked2BitIntensityTo8Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
    Converts bytes with two four-bit-intensity samples to 8 byte intensity values, each stored in one byte.
    static void
    convertPacked4BitIntensityTo8Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
    Converts bytes with four two-bit-intensity samples to byte-sized intensity values.
    static void
    copyPackedBytes(byte[] src, int srcOffset, int srcBitOffset, byte[] dest, int destOffset, int destBitOffset, int numSamples)
    Copies a number of bit values from one byte array to another.
    static void
    decodePacked1Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
     
    static void
    decodePacked2Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
    Decodes bytes with four two-bit samples to single bytes.
    static void
    decodePacked4Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
    Decodes bytes with two four-bit samples to single bytes.
    static void
    decodePackedRGB565BigEndianToRGB24(byte[] src, int srcOffset, byte[] red, int redOffset, byte[] green, int greenOffset, byte[] blue, int blueOffset, int numPixels)
    Convert 16 bit RGB samples stored in big endian (BE) byte order with 5 bits for red and blue and 6 bits for green to 24 bit RGB byte samples.
    static void
    encodePacked2Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numSamples)
     
    static void
    encodePacked4Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numSamples)
     
    static void
    encodeRGB24ToPackedRGB565BigEndian(byte[] red, int redOffset, byte[] green, int greenOffset, byte[] blue, int blueOffset, byte[] dest, int destOffset, int numPixels)
    Convert 24 bit RGB pixels to 16 bit pixels stored in big endian (BE) byte order with 5 bits for red and blue and 6 bits for green.
    static int
    getIntBE(byte[] src, int srcOffset)
    Reads four consecutive bytes from the given array at the given position in big endian order and returns them as an int.
    static int
    getIntLE(byte[] src, int srcOffset)
    Reads four consecutive bytes from the given array at the given position in little endian order and returns them as an int.
    static short
    getShortBE(byte[] src, int srcOffset)
    Reads two consecutive bytes from the given array at the given position in big endian order and returns them as a short.
    static int
    getShortBEAsInt(byte[] src, int srcOffset)
     
    static short
    getShortLE(byte[] src, int srcOffset)
    Reads two consecutive bytes from the given array at the given position in little endian order and returns them as a short.
    static void
    setIntBE(byte[] dest, int destOffset, int newValue)
    Writes an int value into four consecutive bytes of a byte array, in big endian (network) byte order.
    static void
    setIntLE(byte[] dest, int destOffset, int newValue)
    Writes an int value into four consecutive bytes of a byte array, in little endian (Intel) byte order.
    static void
    setShortBE(byte[] dest, int destOffset, short newValue)
     
    static void
    setShortLE(byte[] dest, int destOffset, short newValue)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • ArrayConverter

      private ArrayConverter()
  • Method Details

    • checkArray

      private static void checkArray(byte[] array, int offset, int length) throws IllegalArgumentException
      Makes sure that the arguments define a valid (existing) array interval. This includes:
      • array is non-null
      • offset is >= 0 and smaller than array.length
      • length is > 0
      • offset + length is <= array.length
      Throws:
      IllegalArgumentException
    • convertPacked2BitIntensityTo8Bit

      public static void convertPacked2BitIntensityTo8Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
      Converts bytes with two four-bit-intensity samples to 8 byte intensity values, each stored in one byte. Two-bit values can be 0, 1, 2 or 3. These values will be scaled to the full [0;255] range so that 0 remains 0, 1 becomes 85, 2 becomes 170 and 3 becomes 255.

      A little discussion on how to implement this method was held in the German Java newsgroup de.comp.lang.java. The message I wrote to start the thread has the ID 1ef7du4vfqsd2pskb6jukut6pnhn87htt2@4ax.com. Read the thread at Google Groups.

      Parameters:
      src - byte array, each byte stores four two-bit intensity values
      srcOffset - index into src
      dest - byte array, each byte stores an eight-bit intensity values
      destOffset - index into dest
      numPackedBytes - number of bytes in src to be decoded
    • convertPacked4BitIntensityTo8Bit

      public static void convertPacked4BitIntensityTo8Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
      Converts bytes with four two-bit-intensity samples to byte-sized intensity values. Four-bit values can be from 0 to 15. These values will be scaled to the full [0;255] range so that 0 remains 0, 1 becomes 17, 2 becomes 34, ..., and 15 becomes 255. The most significant four bits in a byte become the left, the least significant four bits the right pixel.
      Parameters:
      src - byte array, each byte stores two four-bit intensity values
      srcOffset - index into src
      dest - byte array, each byte stores an eight-bit intensity values
      destOffset - index into dest
      numPackedBytes - number of bytes in src to be decoded
      Since:
      0.12.0
    • copyPackedBytes

      public static void copyPackedBytes(byte[] src, int srcOffset, int srcBitOffset, byte[] dest, int destOffset, int destBitOffset, int numSamples)
      Copies a number of bit values from one byte array to another.
      Parameters:
      src - array from which is copied
      srcOffset - index into the src array of the first byte from which is copied
      srcBitOffset - first bit within src[srcOffset] from which is copied (0 is left-most, 1 is second left-most, 7 is right-most)
      dest - array to which is copied
      destOffset - index into the dest array of the first byte to which is copied
      destBitOffset - first bit within dest[destOffset] to which is copied (0 is left-most, 1 is second left-most, 7 is right-most)
      numSamples - number of bits to be copied
    • decodePacked1Bit

      public static void decodePacked1Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
    • decodePacked2Bit

      public static void decodePacked2Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
      Decodes bytes with four two-bit samples to single bytes. The two most significant bits of a source byte become the first value, the two least significant bits the fourth value. The method expects numPackedBytes bytes at src[srcOffset] (these will be read and interpreted) and numPackedBytes * 4 at dest[destOffset] (where the decoded byte values will be stored.

      Parameters:
      src - byte array, each byte stores four two-bit values
      srcOffset - index into src
      dest - byte array, each byte stores a single decoded value (from 0 to 3)
      destOffset - index into dest
      numPackedBytes - number of bytes in src to be decoded
      Since:
      0.10.0
    • decodePacked4Bit

      public static void decodePacked4Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numPackedBytes)
      Decodes bytes with two four-bit samples to single bytes. The four most significant bits of a source byte become the first value, the least significant four bits the second value. The method expects numPackedBytes bytes at src[srcOffset] (these will be read and interpreted) and numPackedBytes * 2 at dest[destOffset] (where the decoded byte values will be stored.

      Parameters:
      src - byte array, each byte stores two four-bit values
      srcOffset - index into src
      dest - byte array, each byte stores a single decoded value
      destOffset - index into dest
      numPackedBytes - number of bytes in src to be decoded
    • decodePackedRGB565BigEndianToRGB24

      public static void decodePackedRGB565BigEndianToRGB24(byte[] src, int srcOffset, byte[] red, int redOffset, byte[] green, int greenOffset, byte[] blue, int blueOffset, int numPixels)
      Convert 16 bit RGB samples stored in big endian (BE) byte order with 5 bits for red and blue and 6 bits for green to 24 bit RGB byte samples.
      Since:
      0.10.0
    • encodePacked2Bit

      public static void encodePacked2Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numSamples)
    • encodePacked4Bit

      public static void encodePacked4Bit(byte[] src, int srcOffset, byte[] dest, int destOffset, int numSamples)
    • encodeRGB24ToPackedRGB565BigEndian

      public static void encodeRGB24ToPackedRGB565BigEndian(byte[] red, int redOffset, byte[] green, int greenOffset, byte[] blue, int blueOffset, byte[] dest, int destOffset, int numPixels)
      Convert 24 bit RGB pixels to 16 bit pixels stored in big endian (BE) byte order with 5 bits for red and blue and 6 bits for green.
      Since:
      0.10.0
    • getIntBE

      public static int getIntBE(byte[] src, int srcOffset)
      Reads four consecutive bytes from the given array at the given position in big endian order and returns them as an int.
      Parameters:
      src - the array from which bytes are read
      srcOffset - the index into the array from which the bytes are read
      Returns:
      int value taken from the array
    • getIntLE

      public static int getIntLE(byte[] src, int srcOffset)
      Reads four consecutive bytes from the given array at the given position in little endian order and returns them as an int.
      Parameters:
      src - the array from which bytes are read
      srcOffset - the index into the array from which the bytes are read
      Returns:
      short value taken from the array
    • getShortBE

      public static short getShortBE(byte[] src, int srcOffset)
      Reads two consecutive bytes from the given array at the given position in big endian order and returns them as a short.
      Parameters:
      src - the array from which two bytes are read
      srcOffset - the index into the array from which the two bytes are read
      Returns:
      short value taken from the array
    • getShortBEAsInt

      public static int getShortBEAsInt(byte[] src, int srcOffset)
    • getShortLE

      public static short getShortLE(byte[] src, int srcOffset)
      Reads two consecutive bytes from the given array at the given position in little endian order and returns them as a short.
      Parameters:
      src - the array from which two bytes are read
      srcOffset - the index into the array from which the two bytes are read
      Returns:
      short value taken from the array
    • setIntBE

      public static void setIntBE(byte[] dest, int destOffset, int newValue)
      Writes an int value into four consecutive bytes of a byte array, in big endian (network) byte order.
      Parameters:
      dest - the array to which bytes are written
      destOffset - index of the array to which the first byte is written
      newValue - the int value to be written to the array
    • setIntLE

      public static void setIntLE(byte[] dest, int destOffset, int newValue)
      Writes an int value into four consecutive bytes of a byte array, in little endian (Intel) byte order.
      Parameters:
      dest - the array to which bytes are written
      destOffset - index of the array to which the first byte is written
      newValue - the int value to be written to the array
    • setShortBE

      public static void setShortBE(byte[] dest, int destOffset, short newValue)
    • setShortLE

      public static void setShortLE(byte[] dest, int destOffset, short newValue)