com.digi.xbee.api.utils

## Class ByteUtils

- java.lang.Object
- - com.digi.xbee.api.utils.ByteUtils

-     public class ByteUtils
      extends Object

  Utility class containing methods to work with bytes and byte arrays and several data type conversions.

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `ByteUtils()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `static int` | `byteArrayToInt(byte[] byteArray)` Converts the given byte array (4 bytes length max) into an integer.
    `static long` | `byteArrayToLong(byte[] byteArray)` Converts the given byte array (8 bytes length max) into a long.
    `static short` | `byteArrayToShort(byte[] byteArray)` Converts the given byte array (2 bytes length max) to short.
    `static String` | `byteArrayToString(byte[] value)` Converts the given byte array into a string.
    `static int` | `byteToInt(byte b)` Converts the given byte into an integer.
    `static byte[]` | `intToByteArray(int value)` Converts the given integer value into a byte array.
    `static boolean` | `isBitEnabled(int containerInteger, int bitPosition)` Returns whether the specified bit of the given integer is set to 1 or not.
    `static byte[]` | `longToByteArray(long value)` Converts the given long value into a byte array.
    `static byte[]` | `newByteArray(byte[] data, int finalSize)` Generates a new byte array of the given size using the given data and filling with ASCII zeros (0x48) the remaining space.
    `static boolean` | `readBooleanFromByte(byte containerByte, int bitOffset)` Reads a boolean value from the given byte at the given bit position.
    `static byte[]` | `readBytes(int numBytes, ByteArrayInputStream inputStream)` Reads the given amount of bytes from the given byte array input stream.
    `static int` | `readIntegerFromByte(byte containerByte, int bitOffset, int bitLength)` Reads an integer value from the given byte using the given bit offset and the given bit size.
    `static String` | `readString(ByteArrayInputStream inputStream)` Reads a null-terminated string from the given byte array input stream.
    `static byte[]` | `readUntilCR(ByteArrayInputStream inputStream)` Reads from the given byte array input stream until a CR character is found or the end of stream is reached.
    `static byte[]` | `shortToByteArray(short value)` Converts the given short value into a byte array.
    `static byte[]` | `stringToByteArray(String value)` Converts the given string into a byte array.
    `static byte[]` | `swapByteArray(byte[] source)` Swaps the given byte array order.

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

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

- - ### Constructor Detail

    - #### ByteUtils

          public ByteUtils()

  - ### Method Detail

    - #### byteArrayToInt

          public static int byteArrayToInt(byte[] byteArray)

      Converts the given byte array (4 bytes length max) into an integer.
      Parameters:
        `byteArray` - Byte array to convert to integer (4 bytes length max).
      Returns:
        Converted integer value.
      Throws:
        `NullPointerException` - if `byteArray == null`.
      See Also:
        `intToByteArray(int)`

    - #### byteArrayToLong

          public static long byteArrayToLong(byte[] byteArray)

      Converts the given byte array (8 bytes length max) into a long.
      Parameters:
        `byteArray` - Byte array to convert to long (8 bytes length max).
      Returns:
        Converted long value.
      Throws:
        `NullPointerException` - if `b == null`.
      See Also:
        `longToByteArray(long)`

    - #### byteArrayToShort

          public static short byteArrayToShort(byte[] byteArray)

      Converts the given byte array (2 bytes length max) to short.
      Parameters:
        `byteArray` - Byte array to convert to short (2 bytes length max).
      Returns:
        Converted short value.
      Throws:
        `NullPointerException` - if `byteArray == null`.
      See Also:
        `shortToByteArray(short)`

    - #### byteArrayToString

          public static String byteArrayToString(byte[] value)

      Converts the given byte array into a string.
      Parameters:
        `value` - Byte array to convert to string.
      Returns:
        Converted String.
      Throws:
        `NullPointerException` - if `value == null`.

    - #### byteToInt

          public static int byteToInt(byte b)

      Converts the given byte into an integer.
      Parameters:
        `b` - Byte to convert to integer.
      Returns:
        Converted byte into integer.

    - #### intToByteArray

          public static byte[] intToByteArray(int value)

      Converts the given integer value into a byte array.
      Parameters:
        `value` - Integer value to convert to byte array.
      Returns:
        Byte array of the given integer (4 bytes length).
      See Also:
        `byteArrayToInt(byte[])`

    - #### isBitEnabled

          public static boolean isBitEnabled(int containerInteger,
                             int bitPosition)

      Returns whether the specified bit of the given integer is set to 1 or not.
      Parameters:
        `containerInteger` - Integer to check the given bit position enablement state.
        `bitPosition` - Position of the bit to check its enablement state.
      Returns:
        `true` if the given bit position is set to `1` in the `containerInteger`, `false` otherwise.

    - #### longToByteArray

          public static byte[] longToByteArray(long value)

      Converts the given long value into a byte array.
      Parameters:
        `value` - Long value to convert to byte array.
      Returns:
        Byte array of the given long value (8 bytes length).
      See Also:
        `byteArrayToLong(byte[])`

    - #### newByteArray

          public static byte[] newByteArray(byte[] data,
                            int finalSize)

      Generates a new byte array of the given size using the given data and filling with ASCII zeros (0x48) the remaining space.

      If new size is lower than current, array is truncated.

      Parameters:
        `data` - Data to use in the new array.
        `finalSize` - Final size of the array.
      Returns:
        Final byte array of the given size containing the given data and replacing with zeros the remaining space.
      Throws:
        `NullPointerException` - if `data == null`.

    - #### readBooleanFromByte

          public static boolean readBooleanFromByte(byte containerByte,
                                    int bitOffset)

      Reads a boolean value from the given byte at the given bit position.
      Parameters:
        `containerByte` - Byte to read boolean value from.
        `bitOffset` - Offset inside the byte to read the boolean value.
      Returns:
        The read boolean value.

    - #### readBytes

          public static byte[] readBytes(int numBytes,
                         ByteArrayInputStream inputStream)

      Reads the given amount of bytes from the given byte array input stream.
      Parameters:
        `numBytes` - Number of bytes to read.
        `inputStream` - Byte array input stream to read bytes from.
      Returns:
        An array with the read bytes.
      Throws:
        `IllegalArgumentException` - if `numBytes < 0`.
        `NullPointerException` - if `inputStream == null`.
      See Also:
        `readString(ByteArrayInputStream)`, `readUntilCR(ByteArrayInputStream)`

    - #### readIntegerFromByte

          public static int readIntegerFromByte(byte containerByte,
                                int bitOffset,
                                int bitLength)

      Reads an integer value from the given byte using the given bit offset and the given bit size.
      Parameters:
        `containerByte` - Byte to read the integer from.
        `bitOffset` - Offset inside the byte to start reading integer value.
        `bitLength` - Size in bits of the integer value to read.
      Returns:
        The integer read value.

    - #### readString

          public static String readString(ByteArrayInputStream inputStream)

      Reads a null-terminated string from the given byte array input stream.
      Parameters:
        `inputStream` - Byte array input stream to read string from.
      Returns:
        The read string from the given `ByteArrayInputStream`.
      Throws:
        `NullPointerException` - if `inputStream == null`.
      See Also:
        `readBytes(int, ByteArrayInputStream)`, `readUntilCR(ByteArrayInputStream)`

    - #### readUntilCR

          public static byte[] readUntilCR(ByteArrayInputStream inputStream)

      Reads from the given byte array input stream until a CR character is found or the end of stream is reached. Read bytes are returned.
      Parameters:
        `inputStream` - Byte array input stream to read from.
      Returns:
        An array with the read bytes.
      Throws:
        `NullPointerException` - if `inputStream == null`.
      See Also:
        `readBytes(int, ByteArrayInputStream)`, `readString(ByteArrayInputStream)`

    - #### shortToByteArray

          public static byte[] shortToByteArray(short value)

      Converts the given short value into a byte array.
      Parameters:
        `value` - Short value to convert to byte array.
      Returns:
        Byte array of the given short (2 bytes length).
      See Also:
        `byteArrayToShort(byte[])`

    - #### stringToByteArray

          public static byte[] stringToByteArray(String value)

      Converts the given string into a byte array.
      Parameters:
        `value` - String to convert to byte array.
      Returns:
        Byte array of the given string.
      Throws:
        `NullPointerException` - if `value == null`.
      See Also:
        `byteArrayToString(byte[])`

    - #### swapByteArray

          public static byte[] swapByteArray(byte[] source)

      Swaps the given byte array order.
      Parameters:
        `source` - Byte array to swap.
      Returns:
        The swapped byte array.

© Copyright 2014–2014 Digi International Inc. All rights reserved.