Class ByteUtilities


  • public class ByteUtilities
    extends Object
    ByteUtilities.java - Byte manipulation functions.
    Since:
    MINA 2.0.0-M3
    Author:
    Apache MINA Project
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static byte[] asByteArray​(String hex)
      Converts a hex string representation to a byte array.
      static String asHex​(byte[] bytes)
      Returns a hexadecimal representation of the given byte array.
      static String asHex​(byte[] bytes, String separator)
      Returns a hexadecimal representation of the given byte array.
      static void changeByteEndianess​(byte[] b, int offset, int length)
      Invert two bytes in the given byte array starting at the given offset and repeating the inversion length/2 times.
      static void changeWordEndianess​(byte[] b, int offset, int length)
      Invert the endianness of words (4 bytes) in the given byte array starting at the given offset and repeating length/4 times.
      static byte[] encodeString​(String s, boolean useUnicode)
      Encodes the string to a byte array using UTF-16LE or the ASCII charset in function of the useUnicode argument.
      static byte[] getOEMStringAsByteArray​(String s)
      Converts an OEM string as defined in NTLM protocol (eg ASCII charset) to a byte array.
      static byte[] getUTFStringAsByteArray​(String s)
      Converts an UTF-16LE string as defined in NTLM protocol to a byte array.
      static void intToNetworkByteOrder​(int num, byte[] buf, int start, int count)
      Encodes an integer into up to 4 bytes in network byte order in the supplied buffer starting at start offset and writing count bytes.
      static byte[] intToNetworkByteOrder​(int num, int count)
      Encodes an integer into up to 4 bytes in network byte order.
      static boolean isFlagSet​(int flagSet, int testFlag)
      Returns true if the flag testFlag is set in the flags flagset.
      static int makeIntFromByte2​(byte[] b)
      Reads an int from 2 bytes of the given array at offset 0.
      static int makeIntFromByte2​(byte[] b, int offset)
      Reads an int from 2 bytes of the given array at the given offset.
      static int makeIntFromByte4​(byte[] b)
      Reads an int from 4 bytes of the given array at offset 0.
      static int makeIntFromByte4​(byte[] b, int offset)
      Reads an int from 4 bytes of the given array at the given offset.
      static int networkByteOrderToInt​(byte[] buf, int start, int count)
      Returns the integer represented by up to 4 bytes in network byte order.
      static byte[] writeInt​(int v)
      Write a 32 bit int as LITTLE_ENDIAN.
      static byte[] writeInt​(int v, byte[] b, int offset)
      Write a 32 bit int as LITTLE_ENDIAN to the given array b at offset offset.
      static byte[] writeShort​(short v)
      Write a 16 bit short as LITTLE_ENDIAN.
      static byte[] writeShort​(short v, byte[] b, int offset)
      Write a 16 bit short as LITTLE_ENDIAN to the given array b at offset offset.
    • Method Detail

      • networkByteOrderToInt

        public static int networkByteOrderToInt​(byte[] buf,
                                                int start,
                                                int count)
        Returns the integer represented by up to 4 bytes in network byte order.
        Parameters:
        buf - the buffer to read the bytes from
        start - The starting position
        count - The number of bytes to in the buffer
        Returns:
        the integer value
      • intToNetworkByteOrder

        public static byte[] intToNetworkByteOrder​(int num,
                                                   int count)
        Encodes an integer into up to 4 bytes in network byte order.
        Parameters:
        num - the int to convert to a byte array
        count - the number of reserved bytes for the write operation
        Returns:
        the resulting byte array
      • intToNetworkByteOrder

        public static void intToNetworkByteOrder​(int num,
                                                 byte[] buf,
                                                 int start,
                                                 int count)
        Encodes an integer into up to 4 bytes in network byte order in the supplied buffer starting at start offset and writing count bytes.
        Parameters:
        num - the int to convert to a byte array
        buf - the buffer to write the bytes to
        start - the offset from beginning for the write operation
        count - the number of reserved bytes for the write operation
      • writeShort

        public static final byte[] writeShort​(short v)
        Write a 16 bit short as LITTLE_ENDIAN.
        Parameters:
        v - the short to write
        Returns:
        the Short in a byte[]
      • writeShort

        public static final byte[] writeShort​(short v,
                                              byte[] b,
                                              int offset)
        Write a 16 bit short as LITTLE_ENDIAN to the given array b at offset offset.
        Parameters:
        v - the short to write
        b - the byte array to write to
        offset - the offset at which to start writing in the array
        Returns:
        the Short in a byte[]
      • writeInt

        public static final byte[] writeInt​(int v)
        Write a 32 bit int as LITTLE_ENDIAN.
        Parameters:
        v - the int to write
        Returns:
        the Int in a byte[]
      • writeInt

        public static final byte[] writeInt​(int v,
                                            byte[] b,
                                            int offset)
        Write a 32 bit int as LITTLE_ENDIAN to the given array b at offset offset.
        Parameters:
        v - the int to write
        b - the byte array to write to
        offset - the offset at which to start writing in the array
        Returns:
        the Int in a byte[]
      • changeWordEndianess

        public static final void changeWordEndianess​(byte[] b,
                                                     int offset,
                                                     int length)
        Invert the endianness of words (4 bytes) in the given byte array starting at the given offset and repeating length/4 times. eg: b0b1b2b3 -> b3b2b1b0
        Parameters:
        b - the byte array
        offset - the offset at which to change word start
        length - the number of bytes on which to operate (should be a multiple of 4)
      • changeByteEndianess

        public static final void changeByteEndianess​(byte[] b,
                                                     int offset,
                                                     int length)
        Invert two bytes in the given byte array starting at the given offset and repeating the inversion length/2 times. eg: b0b1 -@gt; b1b0
        Parameters:
        b - the byte array
        offset - the offset at which to change word start
        length - the number of bytes on which to operate (should be a multiple of 2)
      • getOEMStringAsByteArray

        public static final byte[] getOEMStringAsByteArray​(String s)
                                                    throws UnsupportedEncodingException
        Converts an OEM string as defined in NTLM protocol (eg ASCII charset) to a byte array.
        Parameters:
        s - the string to convert
        Returns:
        the result byte array
        Throws:
        UnsupportedEncodingException - if the string is not an OEM string
      • getUTFStringAsByteArray

        public static final byte[] getUTFStringAsByteArray​(String s)
                                                    throws UnsupportedEncodingException
        Converts an UTF-16LE string as defined in NTLM protocol to a byte array.
        Parameters:
        s - the string to convert
        Returns:
        the result byte array
        Throws:
        UnsupportedEncodingException - if the string is not an UTF-16LE string
      • encodeString

        public static final byte[] encodeString​(String s,
                                                boolean useUnicode)
                                         throws UnsupportedEncodingException
        Encodes the string to a byte array using UTF-16LE or the ASCII charset in function of the useUnicode argument.
        Parameters:
        s - the string to encode
        useUnicode - if true then string is encoded to UTF-16LE otherwise to ASCII
        Returns:
        the encoded string as a byte array
        Throws:
        UnsupportedEncodingException - if encoding fails
      • asHex

        public static String asHex​(byte[] bytes)
        Returns a hexadecimal representation of the given byte array.
        Parameters:
        bytes - the array to output to an hex string
        Returns:
        the hex representation as a string
      • asHex

        public static String asHex​(byte[] bytes,
                                   String separator)
        Returns a hexadecimal representation of the given byte array.
        Parameters:
        bytes - the array to output to an hex string
        separator - the separator to use between each byte in the output string. If null no char is inserted between each byte value.
        Returns:
        the hex representation as a string
      • asByteArray

        public static byte[] asByteArray​(String hex)
        Converts a hex string representation to a byte array.
        Parameters:
        hex - the string holding the hex values
        Returns:
        the resulting byte array
      • makeIntFromByte4

        public static final int makeIntFromByte4​(byte[] b)
        Reads an int from 4 bytes of the given array at offset 0.
        Parameters:
        b - the byte array to read
        Returns:
        the integer value
      • makeIntFromByte4

        public static final int makeIntFromByte4​(byte[] b,
                                                 int offset)
        Reads an int from 4 bytes of the given array at the given offset.
        Parameters:
        b - the byte array to read
        offset - the offset at which to start
        Returns:
        the int value
      • makeIntFromByte2

        public static final int makeIntFromByte2​(byte[] b)
        Reads an int from 2 bytes of the given array at offset 0.
        Parameters:
        b - the byte array to read
        Returns:
        the int value
      • makeIntFromByte2

        public static final int makeIntFromByte2​(byte[] b,
                                                 int offset)
        Reads an int from 2 bytes of the given array at the given offset.
        Parameters:
        b - the byte array to read
        offset - the offset at which to start
        Returns:
        the int value
      • isFlagSet

        public static final boolean isFlagSet​(int flagSet,
                                              int testFlag)
        Returns true if the flag testFlag is set in the flags flagset.
        Parameters:
        flagSet - the flagset to test
        testFlag - the flag we search the presence of
        Returns:
        true if testFlag is present in the flagset, false otherwise.