Class BytesUtils


  • public class BytesUtils
    extends java.lang.Object
    BytesUtils is a utility class. It provides conversion among byte array and other type including integer, long, float, boolean, double and string.
    It also provide other usable function as follows:
    reading function which receives InputStream.
    concat function to join a list of byte array to one.
    get and set one bit in a byte array.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte boolToByte​(boolean x)  
      static byte[] boolToBytes​(boolean x)
      convert boolean to byte[1].
      static byte[] boolToBytes​(boolean x, byte[] desc, int offset)
      boolean convert to byte array, then write four bytes to parameter desc start from index:offset.
      static boolean bytesToBool​(byte[] b)
      byte array to boolean.
      static boolean bytesToBool​(byte[] b, int offset)
      convert one-bytes byte array cut from parameters to boolean.
      static double bytesToDouble​(byte[] bytes)
      convert byte array to double.
      static double bytesToDouble​(byte[] bytes, int offset)
      convert eight-bytes byte array cut from parameters to double.
      static float bytesToFloat​(byte[] b)
      convert byte[4] to float.
      static float bytesToFloat​(byte[] b, int offset)
      convert four-bytes byte array cut from parameters to float.
      static int bytesToInt​(byte[] bytes)
      byte[4] convert to integer.
      static int bytesToInt​(byte[] bytes, int offset)
      convert four-bytes byte array cut from parameters to integer.
      static int bytesToInt​(byte[] result, int pos, int width)
      given a byte array, read width bits from specified position bits and convert it to an integer.
      static long bytesToLong​(byte[] byteNum)
      convert byte array to long with default length 8.
      static long bytesToLong​(byte[] byteNum, int len)
      specify the input byte array length.
      static long bytesToLong​(byte[] result, int pos, int width)
      given a byte array, read width bits from specified pos bits and convert it to an long.
      static long bytesToLongFromOffset​(byte[] byteNum, int len, int offset)
      convert eight-bytes byte array cut from parameters to long.
      static short bytesToShort​(byte[] b)
      we modify the order of serialization for fitting ByteBuffer.getShort()
      static java.lang.String bytesToString​(byte[] byteStr)
      convert byte array to string using UTF-8 encoding.
      static boolean byteToBool​(byte b)  
      static byte[] concatByteArray​(byte[] a, byte[] b)
      join two byte arrays to one.
      static byte[] concatByteArrayList​(java.util.List<byte[]> list)
      join a list of byte arrays into one array.
      static byte[] doubleToBytes​(double data)
      convert double to byte array.
      static void doubleToBytes​(double d, byte[] bytes, int offset)
      convert double to byte into the given byte array started from offset.
      static byte[] floatToBytes​(float x)
      convert float to byte array.
      static void floatToBytes​(float x, byte[] desc, int offset)
      float convert to boolean, then write four bytes to parameter desc start from index:offset.
      static int getByteN​(byte data, int offset)
      get one bit in input byte.
      static int getIntN​(int data, int offset)
      get one bit in input integer.
      static int getLongN​(long data, int offset)
      get one bit in input long.
      static byte[] intToBytes​(int i)
      integer convert to byte[4].
      static byte[] intToBytes​(int i, byte[] desc, int offset)
      integer convert to byte array, then write four bytes to parameter desc start from index:offset.
      static void intToBytes​(int srcNum, byte[] result, int pos, int width)
      convert an integer to a byte array which length is width, then copy this array to the parameter result from pos.
      static byte[] intToTwoBytes​(int i)
      divide int to two bytes.
      static byte[] longToBytes​(long num)
      long to byte array with default converting length 8.
      static byte[] longToBytes​(long num, byte[] desc, int offset)
      long convert to byte array, then write four bytes to parameter desc start from index:offset.
      static void longToBytes​(long srcNum, byte[] result, int pos, int width)
      convert a long to a byte array which length is width, then copy this array to the parameter result from pos.
      static byte[] longToBytes​(long num, int len)
      specify the result array length.
      static boolean readBool​(java.io.InputStream in)
      read 1-byte array from an InputStream and convert it to a integer number.
      static double readDouble​(java.io.InputStream in)
      read 8-byte array from an InputStream and convert it to a double number.
      static float readFloat​(java.io.InputStream in)
      read 4-byte array from an InputStream and convert it to a float number.
      static int readInt​(java.io.InputStream in)
      read 4-byte array from an InputStream and convert it to a integer number.
      static long readLong​(java.io.InputStream in)
      read 8-byte array from an InputStream and convert it to a long number.
      static byte[] safeReadInputStreamToBytes​(int count, java.io.InputStream in)
      read bytes specified length from InputStream safely.
      static byte setByteN​(byte data, int offset, int value)
      set one bit in input byte.
      static int setIntN​(int data, int offset, int value)
      set one bit in input integer.
      static long setLongN​(long data, int offset, int value)
      set one bit in input long.
      static byte[] shortToBytes​(short number)
      we modify the order of serialization for fitting ByteBuffer.putShort()
      static byte[] stringToBytes​(java.lang.String str)
      convert string to byte array using UTF-8 encoding.
      static byte[] subBytes​(byte[] src, int start, int length)
      cut out specified length byte array from parameter start from input byte array src and return.
      static int twoBytesToInt​(byte[] ret)
      concatenate two bytes to int.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • intToBytes

        public static byte[] intToBytes​(int i)
        integer convert to byte[4].
        Parameters:
        i - integer to convert
        Returns:
        byte[4] for integer
      • intToBytes

        public static byte[] intToBytes​(int i,
                                        byte[] desc,
                                        int offset)
        integer convert to byte array, then write four bytes to parameter desc start from index:offset.
        Parameters:
        i - integer to convert
        desc - byte array be written
        offset - position in desc byte array that conversion result should start
        Returns:
        byte array
      • intToBytes

        public static void intToBytes​(int srcNum,
                                      byte[] result,
                                      int pos,
                                      int width)
        convert an integer to a byte array which length is width, then copy this array to the parameter result from pos.
        Parameters:
        srcNum - input integer variable. All but the lowest width bits are 0.
        result - byte array to convert
        pos - start position
        width - bit-width
      • intToTwoBytes

        public static byte[] intToTwoBytes​(int i)
        divide int to two bytes.
        Parameters:
        i - int
        Returns:
        two bytes in byte[] structure
      • twoBytesToInt

        public static int twoBytesToInt​(byte[] ret)
        concatenate two bytes to int.
        Parameters:
        ret - byte[]
        Returns:
        int value
      • bytesToInt

        public static int bytesToInt​(byte[] bytes)
        byte[4] convert to integer.
        Parameters:
        bytes - input byte[]
        Returns:
        integer
      • bytesToInt

        public static int bytesToInt​(byte[] bytes,
                                     int offset)
        convert four-bytes byte array cut from parameters to integer.
        Parameters:
        bytes - source bytes which length should be greater than 4
        offset - position in parameter byte array that conversion result should start
        Returns:
        integer
      • bytesToInt

        public static int bytesToInt​(byte[] result,
                                     int pos,
                                     int width)
        given a byte array, read width bits from specified position bits and convert it to an integer.
        Parameters:
        result - input byte array
        pos - bit offset rather than byte offset
        width - bit-width
        Returns:
        integer variable
      • floatToBytes

        public static byte[] floatToBytes​(float x)
        convert float to byte array.
        Parameters:
        x - float
        Returns:
        byte[4]
      • floatToBytes

        public static void floatToBytes​(float x,
                                        byte[] desc,
                                        int offset)
        float convert to boolean, then write four bytes to parameter desc start from index:offset.
        Parameters:
        x - float
        desc - byte array be written
        offset - position in desc byte array that conversion result should start
      • bytesToFloat

        public static float bytesToFloat​(byte[] b)
        convert byte[4] to float.
        Parameters:
        b - byte[4]
        Returns:
        float
      • bytesToFloat

        public static float bytesToFloat​(byte[] b,
                                         int offset)
        convert four-bytes byte array cut from parameters to float.
        Parameters:
        b - source bytes which length should be greater than 4
        offset - position in parameter byte array that conversion result should start
        Returns:
        float
      • doubleToBytes

        public static byte[] doubleToBytes​(double data)
        convert double to byte array.
        Parameters:
        data - double
        Returns:
        byte[8]
      • doubleToBytes

        public static void doubleToBytes​(double d,
                                         byte[] bytes,
                                         int offset)
        convert double to byte into the given byte array started from offset.
        Parameters:
        d - input double
        bytes - target byte[]
        offset - start pos
      • bytesToDouble

        public static double bytesToDouble​(byte[] bytes)
        convert byte array to double.
        Parameters:
        bytes - byte[8]
        Returns:
        double
      • bytesToDouble

        public static double bytesToDouble​(byte[] bytes,
                                           int offset)
        convert eight-bytes byte array cut from parameters to double.
        Parameters:
        bytes - source bytes which length should be greater than 8
        offset - position in parameter byte array that conversion result should start
        Returns:
        double
      • boolToBytes

        public static byte[] boolToBytes​(boolean x)
        convert boolean to byte[1].
        Parameters:
        x - boolean
        Returns:
        byte[]
      • boolToByte

        public static byte boolToByte​(boolean x)
      • byteToBool

        public static boolean byteToBool​(byte b)
      • boolToBytes

        public static byte[] boolToBytes​(boolean x,
                                         byte[] desc,
                                         int offset)
        boolean convert to byte array, then write four bytes to parameter desc start from index:offset.
        Parameters:
        x - input boolean
        desc - byte array be written
        offset - position in desc byte array that conversion result should start
        Returns:
        byte[1]
      • bytesToBool

        public static boolean bytesToBool​(byte[] b)
        byte array to boolean.
        Parameters:
        b - input byte[1]
        Returns:
        boolean
      • bytesToBool

        public static boolean bytesToBool​(byte[] b,
                                          int offset)
        convert one-bytes byte array cut from parameters to boolean.
        Parameters:
        b - source bytes which length should be greater than 1
        offset - position in parameter byte array that conversion result should start
        Returns:
        boolean
      • longToBytes

        public static byte[] longToBytes​(long num)
        long to byte array with default converting length 8. It means the length of result byte array is 8.
        Parameters:
        num - long variable to be converted
        Returns:
        byte[8]
      • longToBytes

        public static byte[] longToBytes​(long num,
                                         int len)
        specify the result array length. then, convert long to Big-Endian byte from low to high.
        e.g.
        the binary presentation of long number 1000L is {6 bytes equal 0000000} 00000011 11101000
        if len = 2, it will return byte array :{00000011 11101000}(Big-Endian) if len = 1, it will return byte array :{11101000}.
        Parameters:
        num - long variable to be converted
        len - length of result byte array
        Returns:
        byte array which length equals with parameter len
      • longToBytes

        public static byte[] longToBytes​(long num,
                                         byte[] desc,
                                         int offset)
        long convert to byte array, then write four bytes to parameter desc start from index:offset.
        Parameters:
        num - input long variable
        desc - byte array be written
        offset - position in desc byte array that conversion result should start
        Returns:
        byte array
      • longToBytes

        public static void longToBytes​(long srcNum,
                                       byte[] result,
                                       int pos,
                                       int width)
        convert a long to a byte array which length is width, then copy this array to the parameter result from pos.
        Parameters:
        srcNum - input long variable. All but the lowest width bits are 0.
        result - byte array to convert
        pos - start position
        width - bit-width
      • bytesToLong

        public static long bytesToLong​(byte[] byteNum)
        convert byte array to long with default length 8. namely.
        Parameters:
        byteNum - input byte array
        Returns:
        long
      • bytesToLong

        public static long bytesToLong​(byte[] byteNum,
                                       int len)
        specify the input byte array length. then, convert byte array to long value from low to high.
        e.g.
        the input byte array is {00000011 11101000}. if len = 2, return 1000 if len = 1, return 232(only calculate the low byte).
        Parameters:
        byteNum - byte array to be converted
        len - length of input byte array to be converted
        Returns:
        long
      • bytesToLong

        public static long bytesToLong​(byte[] result,
                                       int pos,
                                       int width)
        given a byte array, read width bits from specified pos bits and convert it to an long.
        Parameters:
        result - input byte array
        pos - bit offset rather than byte offset
        width - bit-width
        Returns:
        long variable
      • bytesToLongFromOffset

        public static long bytesToLongFromOffset​(byte[] byteNum,
                                                 int len,
                                                 int offset)
        convert eight-bytes byte array cut from parameters to long.
        Parameters:
        byteNum - source bytes which length should be greater than 8
        len - length of input byte array to be converted
        offset - position in parameter byte array that conversion result should start
        Returns:
        long
      • stringToBytes

        public static byte[] stringToBytes​(java.lang.String str)
        convert string to byte array using UTF-8 encoding.
        Parameters:
        str - input string
        Returns:
        byte array
      • bytesToString

        public static java.lang.String bytesToString​(byte[] byteStr)
        convert byte array to string using UTF-8 encoding.
        Parameters:
        byteStr - input byte array
        Returns:
        string
      • concatByteArray

        public static byte[] concatByteArray​(byte[] a,
                                             byte[] b)
        join two byte arrays to one.
        Parameters:
        a - one of byte array
        b - another byte array
        Returns:
        byte array after joining
      • concatByteArrayList

        public static byte[] concatByteArrayList​(java.util.List<byte[]> list)
        join a list of byte arrays into one array.
        Parameters:
        list - a list of byte array to join
        Returns:
        byte array after joining
      • subBytes

        public static byte[] subBytes​(byte[] src,
                                      int start,
                                      int length)
        cut out specified length byte array from parameter start from input byte array src and return.
        Parameters:
        src - input byte array
        start - start index of src
        length - cut off length
        Returns:
        byte array
      • getIntN

        public static int getIntN​(int data,
                                  int offset)
        get one bit in input integer. the offset is from low to high and start with 0
        e.g.
        data:1000(00000000 00000000 00000011 11101000), if offset is 4, return 0(111 "0" 1000) if offset is 9, return 1(00000 "1" 1 11101000).
        Parameters:
        data - input int variable
        offset - bit offset
        Returns:
        0 or 1
      • setIntN

        public static int setIntN​(int data,
                                  int offset,
                                  int value)
        set one bit in input integer. the offset is from low to high and start with index 0
        e.g.
        data:1000({00000000 00000000 00000011 11101000}), if offset is 4, value is 1, return 1016({00000000 00000000 00000011 111 "1" 1000}) if offset is 9, value is 0 return 488({00000000 00000000 000000 "0" 1 11101000}) if offset is 0, value is 0 return 1000(no change).
        Parameters:
        data - input int variable
        offset - bit offset
        value - value to set
        Returns:
        int variable
      • getByteN

        public static int getByteN​(byte data,
                                   int offset)
        get one bit in input byte. the offset is from high to low and start with 0
        e.g.
        data:16(00010000), if offset is 3, return 1(000 "1" 0000) if offset is 0, return 0("0" 0010000).
        Parameters:
        data - input byte variable
        offset - bit offset
        Returns:
        0/1
      • setByteN

        public static byte setByteN​(byte data,
                                    int offset,
                                    int value)
        set one bit in input byte. the offset is from high to low and start with index 0
        e.g.
        data:16(00010000), if offset is 3, value is 0, return 0({000 "0" 0000}) if offset is 6, value is 1, return 18({00010010}) if offset is 0, value is 0, return 16(no change).
        Parameters:
        data - input byte variable
        offset - bit offset
        value - value to set
        Returns:
        byte variable
      • getLongN

        public static int getLongN​(long data,
                                   int offset)
        get one bit in input long. the offset is from low to high and start with 0.
        Parameters:
        data - input long variable
        offset - bit offset
        Returns:
        0/1
      • setLongN

        public static long setLongN​(long data,
                                    int offset,
                                    int value)
        set one bit in input long. the offset is from low to high and start with index 0.
        Parameters:
        data - input long variable
        offset - bit offset
        value - value to set
        Returns:
        long variable
      • readDouble

        public static double readDouble​(java.io.InputStream in)
                                 throws java.io.IOException
        read 8-byte array from an InputStream and convert it to a double number.
        Parameters:
        in - InputStream
        Returns:
        double
        Throws:
        java.io.IOException - cannot read double from InputStream
      • readFloat

        public static float readFloat​(java.io.InputStream in)
                               throws java.io.IOException
        read 4-byte array from an InputStream and convert it to a float number.
        Parameters:
        in - InputStream
        Returns:
        float
        Throws:
        java.io.IOException - cannot read float from InputStream
      • readBool

        public static boolean readBool​(java.io.InputStream in)
                                throws java.io.IOException
        read 1-byte array from an InputStream and convert it to a integer number.
        Parameters:
        in - InputStream
        Returns:
        boolean
        Throws:
        java.io.IOException - cannot read boolean from InputStream
      • readInt

        public static int readInt​(java.io.InputStream in)
                           throws java.io.IOException
        read 4-byte array from an InputStream and convert it to a integer number.
        Parameters:
        in - InputStream
        Returns:
        integer
        Throws:
        java.io.IOException - cannot read int from InputStream
      • readLong

        public static long readLong​(java.io.InputStream in)
                             throws java.io.IOException
        read 8-byte array from an InputStream and convert it to a long number.
        Parameters:
        in - InputStream
        Returns:
        long
        Throws:
        java.io.IOException - cannot read long from InputStream
      • safeReadInputStreamToBytes

        public static byte[] safeReadInputStreamToBytes​(int count,
                                                        java.io.InputStream in)
                                                 throws java.io.IOException
        read bytes specified length from InputStream safely.
        Parameters:
        count - number of byte to read
        in - InputStream
        Returns:
        byte array
        Throws:
        java.io.IOException - cannot read from InputStream
      • shortToBytes

        public static byte[] shortToBytes​(short number)
        we modify the order of serialization for fitting ByteBuffer.putShort()
        Parameters:
        number - input short number
        Returns:
        Bytes
      • bytesToShort

        public static short bytesToShort​(byte[] b)
        we modify the order of serialization for fitting ByteBuffer.getShort()
        Parameters:
        b - bytes
        Returns:
        short number