com.beaglebuddy.util

## Class Utility

- java.lang.Object
- - com.beaglebuddy.util.Utility

- Direct Known Subclasses:
    ID3v23FrameBodyUtility, ID3v24FrameBodyUtility

      public class Utility
      extends java.lang.Object

  This class is a utility class which contains general purpose methods for dealing with raw binary data.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `Utility()` default constructor.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static int` | `bytesToInt(byte[] buffer)` converts 4 bytes to an integer.
    `static int` | `bytesToInt(byte[] buffer, int index)` converts the 4 bytes starting at the specified index to an integer.
    `static short` | `bytesToShort(byte[] data)` converts 2 bytes to a short.
    `static short` | `bytesToShort(byte[] buffer, int index)` converts the 2 bytes starting at the specified index to a short.
    `static int` | `bytesToSynchsafeInt(byte[] buffer, int index)` converts the 4 bytes starting at the specified index to a synchsafe integer.
    `static short` | `bytesToSynchsafeShort(byte[] buffer, int index)` converts the 2 bytes starting at the specified index to a synchsafe short.
    `static java.lang.String` | `formateDate(java.util.Date date)` formats a date as YYYYMMDD.
    `static java.lang.String` | `getUTF16String(java.lang.String string)` if a string is UTF-16 encoded, then make sure it starts with a BOM (byte order mark) of U+FEFF.
    `static java.lang.String` | `getUTF8String(java.lang.String string)` if a string is UTF-8 encoded, then make sure it starts with a BOM (byte order mark) of U+EF BB BF.
    `static java.lang.String` | `hex(byte data)` formats a single byte as a 2 digit hex value, ie, 0xbb, where 0 < b < F. example: decimal: 12 -> hex: 0C example: decimal: 178 -> hex: B2
    `static java.lang.String` | `hex(byte[] data)` formats raw binary byte data as a sequence of bytes in the format 0xbb 0xbb ...
    `static java.lang.String` | `hex(byte[] data, int indent)` formats raw binary byte data as a sequence of bytes in the format 0xbb 0xbb ...
    `static byte[]` | `intToBytes(int n)` converts an integer value to a 4 byte array.
    `static int` | `littleEndianBytesToInt(byte[] bytes, int index)` converts the 4 little endian bytes starting at the specified index to an integer.
    `static java.lang.String` | `pad(int numSpaces)` gets a string consisting of the specified number of blank spaces which can be used for padding (left or right) in order to format some output (ie, make columns line up, etc).
    `static byte[]` | `shortToBytes(int n)` converts a short value to a 2 byte array.
    `static byte[]` | `synchsafeIntToBytes(int n)` converts a synchsafe integer value to a 4 byte array.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### Utility

          public Utility()

      default constructor. this is needed by ID3v2xFrameBodyUtility.

  - ### Method Detail

    - #### formateDate

          public static java.lang.String formateDate(java.util.Date date)

      formats a date as YYYYMMDD.
      Parameters:
        `date` - the date to format. If null, then the today's date is used.
      Returns:
        the specified date formatted as YYYYMMDD.

    - #### hex

          public static java.lang.String hex(byte data)

      formats a single byte as a 2 digit hex value, ie, 0xbb, where 0 < b < F.
      example: decimal: 12 -> hex: 0C
      example: decimal: 178 -> hex: B2
      Parameters:
        `data` - byte data whose value will be converted to hex.
      Returns:
        the hex representation of a byte.

    - #### hex

          public static java.lang.String hex(byte[] data)

      formats raw binary byte data as a sequence of bytes in the format 0xbb 0xbb ... with 16 bytes per line.
      Parameters:
        `data` - raw binary data to be printed.
      Returns:
        a string representation of the binary data as hexadecimal bytes.

    - #### hex

          public static java.lang.String hex(byte[] data,
                             int indent)

      formats raw binary byte data as a sequence of bytes in the format 0xbb 0xbb ... with 16 bytes per line.
      Parameters:
        `data` - raw binary data to be printed.
        `indent` - number of spaces to indent each line of output before displaying the hex bytes.
      Returns:
        a string representation of the binary data as hexadecimal bytes.

    - #### pad

          public static java.lang.String pad(int numSpaces)

      gets a string consisting of the specified number of blank spaces which can be used for padding (left or right) in order to format some output (ie, make columns line up, etc).
      Parameters:
        `numSpaces` - number of spaces to return.
      Returns:
        a string of spaces.

    - #### getUTF16String

          public static java.lang.String getUTF16String(java.lang.String string)

      if a string is UTF-16 encoded, then make sure it starts with a BOM (byte order mark) of U+FEFF.
      Parameters:
        `string` - string to be checked for proper UTF-16 encoding.
      Returns:
        a properly encoded UTF-16 encoded string which starts with a byte order mark.
      See Also:
        Byte Order mark

    - #### getUTF8String

          public static java.lang.String getUTF8String(java.lang.String string)

      if a string is UTF-8 encoded, then make sure it starts with a BOM (byte order mark) of U+EF BB BF.
      Parameters:
        `string` - string to be checked for proper UTF-8 encoding.
      Returns:
        a properly encoded UTF-8 encoded string which starts with a byte order mark.
      See Also:
        Byte Order mark

    - #### intToBytes

          public static byte[] intToBytes(int n)

      converts an integer value to a 4 byte array.
      Parameters:
        `n` - the integer value to be converted to a byte array.
      Returns:
        a 4 byte array holding the integer value.

    - #### bytesToInt

          public static int bytesToInt(byte[] buffer)

      converts 4 bytes to an integer.
      Parameters:
        `buffer` - the byte array from which the next four sequential bytes will be converted to an integer.
      Returns:
        the integer value obtained from converting the first four binary bytes of the given buffer.

    - #### littleEndianBytesToInt

          public static int littleEndianBytesToInt(byte[] bytes,
                                   int index)

      converts the 4 little endian bytes starting at the specified index to an integer.
      Parameters:
        `bytes` - the byte array from which the next four sequential bytes will be converted to an integer.
        `index` - specifies that the bytes at index, index + 1, index +2, and index + 3 will be converted to an integer.
      Returns:
        the integer value obtained from starting at the specified index in the byte array and converting the next four sequential bytes.

    - #### bytesToInt

          public static int bytesToInt(byte[] buffer,
                       int index)

      converts the 4 bytes starting at the specified index to an integer.
      Parameters:
        `buffer` - the byte array from which the next four sequential bytes will be converted to an integer.
        `index` - specifies that the bytes at index, index + 1, index +2, and index + 3 will be converted to an integer.
      Returns:
        the integer value obtained from starting at the specified index in the byte array and converting the next four sequential bytes.

    - #### bytesToSynchsafeInt

          public static int bytesToSynchsafeInt(byte[] buffer,
                                int index)

      converts the 4 bytes starting at the specified index to a synchsafe integer.
      Parameters:
        `buffer` - the byte array from which the next four sequential bytes will be converted to a synchsafe integer.
        `index` - specifies that the bytes at index, index + 1, index +2, and index + 3 will be converted to a synchsafe integer.
      Returns:
        the synchsafe integer value obtained from starting at the specified index in the byte array and converting the next four sequential bytes.
      See Also:
        "http://stackoverflow.com/questions/5223025/why-are-there-synchsafe-integer"

    - #### synchsafeIntToBytes

          public static byte[] synchsafeIntToBytes(int n)

      converts a synchsafe integer value to a 4 byte array. Synchsafe integers are integers that keep its highest bit (bit 7) zeroed, making seven bits out of eight available. Thus a 32 bit synchsafe integer can store 28 bits of information.
      Parameters:
        `n` - the synchsafe integer value to be converted to a byte array.
      Returns:
        a 4 byte array holding the synchsafe integer value.

    - #### shortToBytes

          public static byte[] shortToBytes(int n)

      converts a short value to a 2 byte array.
      Parameters:
        `n` - the short value to be converted to a byte array.
      Returns:
        a 2 byte array holding the short value.

    - #### bytesToShort

          public static short bytesToShort(byte[] data)

      converts 2 bytes to a short.
      Parameters:
        `data` - the byte array from which the next two sequential bytes will be converted to a short.
      Returns:
        the short value obtained from converting the first two binary bytes of the given data.

    - #### bytesToShort

          public static short bytesToShort(byte[] buffer,
                           int index)

      converts the 2 bytes starting at the specified index to a short.
      Parameters:
        `buffer` - the byte array from which the next to sequential bytes will be converted to a short.
        `index` - specifies that the bytes at index and at index + 1 will be converted to a short.
      Returns:
        the short value obtained from starting at the specified index in the byte array and converting the next two sequential bytes.

    - #### bytesToSynchsafeShort

          public static short bytesToSynchsafeShort(byte[] buffer,
                                    int index)

      converts the 2 bytes starting at the specified index to a synchsafe short.
      Parameters:
        `buffer` - the byte array from which the next to sequential bytes will be converted to a synchsafe short.
        `index` - specifies that the bytes at index and at index + 1 will be converted to a synchsafe short.
      Returns:
        the synchsafe short value obtained from starting at the specified index in the byte array and converting the next two sequential bytes.