Class ByteArray


  • public final class ByteArray
    extends Object
    Util class for methods working with byte array.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String bytesToHRString​(byte[] bytes)
      Decodes bytes to human readable UTF-8 string.
      static int bytesToInt​(byte[] bytes)
      Converts byte array to Integer.
      static long bytesToLong​(byte[] bytes)
      Converts byte array to long.
      static byte copyBitsRange​(byte src, int fromBit, int length)
      Copies range of bits from passed byte and align to right.
      static byte[] cutBytes​(byte[] bytes, int count)
      Cuts 'count' number of bytes from the beginning of given byte array.
      static String encodeBase64​(io.netty.buffer.ByteBuf buffer)
      Encode input ByteBuf with Base64 to string format.
      static byte[] fileToBytes​(String name)
      Parses file to array of bytes.
      static byte[] getAllBytes​(io.netty.buffer.ByteBuf buffer)
      Helper method missing from netty ByteBuf methods.
      static byte[] getBytes​(io.netty.buffer.ByteBuf buffer, int length)
      Helper method missing from netty ByteBuf methods.
      static byte[] readAllBytes​(io.netty.buffer.ByteBuf buffer)
      Helper method missing from netty ByteBuf methods.
      static byte[] readBytes​(io.netty.buffer.ByteBuf buffer, int length)
      Helper method missing from netty ByteBuf methods.
      static byte[] subByte​(byte[] bytes, int startIndex, int length)
      Returns a new byte array from given byte array, starting at start index with the size of the length parameter.
    • Method Detail

      • readBytes

        public static byte[] readBytes​(io.netty.buffer.ByteBuf buffer,
                                       int length)
        Helper method missing from netty ByteBuf methods. Directly returns byte array part of the given buffer, starting at reader index, with given length. Increases reader index of the buffer by 'length'.
        Parameters:
        buffer - ByteBuf from which the bytes are going to be taken
        length - length of the returned byte array
        Returns:
        byte array
      • readAllBytes

        public static byte[] readAllBytes​(io.netty.buffer.ByteBuf buffer)
        Helper method missing from netty ByteBuf methods. Directly returns all readable bytes from buffer as byte array. Adjusts reader index of the buffer by length of readable bytes in the buffer.
        Parameters:
        buffer - byteBuf from which the bytes are going to be taken
        Returns:
        byte array
      • getBytes

        public static byte[] getBytes​(io.netty.buffer.ByteBuf buffer,
                                      int length)
        Helper method missing from netty ByteBuf methods. Directly returns byte array part of the given buffer, starting at reader index, with given length. Does not modify reader or writer index of the buffer.
        Parameters:
        buffer - ByteBuf from which the bytes are going to be taken
        length - length of the returned byte array
        Returns:
        byte array
      • getAllBytes

        public static byte[] getAllBytes​(io.netty.buffer.ByteBuf buffer)
        Helper method missing from netty ByteBuf methods. Directly returns all readable bytes from buffer as byte array. Does not modify writer or reader index of the buffer.
        Parameters:
        buffer - byteBuf from which the bytes are going to be taken
        Returns:
        byte array
      • subByte

        public static byte[] subByte​(byte[] bytes,
                                     int startIndex,
                                     int length)
        Returns a new byte array from given byte array, starting at start index with the size of the length parameter. Byte array given as parameter stays untouched.
        Parameters:
        bytes - original byte array
        startIndex - beginning index, inclusive
        length - how many bytes should be in the sub-array
        Returns:
        a new byte array that is a sub-array of the original
      • bytesToInt

        public static int bytesToInt​(byte[] bytes)
        Converts byte array to Integer. If there are less bytes in the array as required (4), the method will push adequate number of zero bytes prepending given byte array.
        Parameters:
        bytes - array to be converted to int
        Returns:
        int
      • bytesToLong

        public static long bytesToLong​(byte[] bytes)
        Converts byte array to long. If there are less bytes in the array as required (Long.Size), the method will push adequate number of zero bytes prepending given byte array.
        Parameters:
        bytes - array to be converted to long
        Returns:
        long
      • cutBytes

        public static byte[] cutBytes​(byte[] bytes,
                                      int count)
        Cuts 'count' number of bytes from the beginning of given byte array.
        Parameters:
        bytes - array to be cut, cannot be null
        count - how many bytes needed to be cut, needs to be greater than 0
        Returns:
        bytes array without first 'count' bytes
      • fileToBytes

        public static byte[] fileToBytes​(String name)
                                  throws IOException
        Parses file to array of bytes.
        Parameters:
        name - path to file to by parsed
        Returns:
        parsed array of bytes
        Throws:
        IOException
      • copyBitsRange

        public static byte copyBitsRange​(byte src,
                                         int fromBit,
                                         int length)
        Copies range of bits from passed byte and align to right.
        Parameters:
        src - source byte to copy from
        fromBit - bit from which will copy (inclusive) - numbered from 0
        length - of bits to by copied, valid values are 1 through 8
        Returns:
        copied value aligned to right
      • bytesToHRString

        public static String bytesToHRString​(byte[] bytes)
        Decodes bytes to human readable UTF-8 string. If bytes are not valid UTF-8, they are represented as raw binary.
        Parameters:
        bytes - bytes to be decoded to string
        Returns:
        String representation of passed bytes
      • encodeBase64

        public static String encodeBase64​(io.netty.buffer.ByteBuf buffer)
        Encode input ByteBuf with Base64 to string format.
        Parameters:
        buffer - Input ByteBuf
        Returns:
        String representation of encoded ByteBuf.