Class ByteUtils


  • public class ByteUtils
    extends Object
    Clase que engloba mtodos para trabajar con bytes.
    Author:
    Vicente Caballero Navarro
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void bigIntegerToBytes​(BigInteger n, byte[] data, int[] offset)
      Write the bytes representing n into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
      static void boolToBytes​(boolean b, byte[] data, int[] offset)
      Write the bytes representing b into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
      static BigInteger bytesToBigInteger​(byte[] data, int[] offset)
      Return the BigInteger represented by the bytes in data staring at offset offset[0].
      static boolean bytesToBool​(byte[] data, int[] offset)
      Return the boolean represented by the bytes in data staring at offset offset[0].
      static double bytesToDouble​(byte[] data, int[] offset)
      Return the double represented by the bytes in data staring at offset offset[0].
      static int bytesToInt​(byte[] data, int[] offset)
      Return the int represented by the bytes in data staring at offset offset[0].
      static void bytesToInts​(int[] dst, int dst_offset, byte[] src, int src_offset, int length)
      Convert an array of bytess into an array of ints.
      static long bytesToLong​(byte[] data, int[] offset)
      Return the long represented by the bytes in data staring at offset offset[0].
      static short bytesToShort​(byte[] data, int[] offset)
      Return the short represented by the bytes in data staring at offset offset[0].
      static String bytesToString​(byte[] data, int[] offset)
      Return the String represented by the bytes in data staring at offset offset[0].
      static int byteToUnsignedInt​(byte b)
      Convert a byte into an unsigned integer.
      static void doubleToBytes​(double d, byte[] data, int[] offset)
      Write the bytes representing d into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
      static boolean equals​(byte[] b1, byte[] b2)
      Determines whether two arrays of bytes contain the same contents.
      static int getUnsigned​(byte b)
      DOCUMENT ME!
      static void intsToBytes​(byte[] dst, int dst_offset, int[] src, int src_offset, int length)
      Convert an array of ints into an array of bytes.
      static void intToBytes​(int i, byte[] data, int[] offset)
      Write the bytes representing i into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
      static void longToBytes​(long l, byte[] data, int[] offset)
      Write the bytes representing l into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
      static void memclr​(byte[] array, int offset, int length)
      Fill the given array with zeros.
      static boolean memcmp​(byte[] a, int a_offset, byte[] b, int b_offset, int length)
      Compare the contents of one array of bytes to another.
      static int memcpy​(byte[] dst, int dst_offset, byte[] src, int src_offset, int length)
      Copy contents of one array of bytes into another.
      static String print_bytes​(byte[] data)
      DOCUMENT ME!
      static String print_bytes​(byte[] data, int offset, int length)
      Produce a String representation for the specified array of bytes.
      static String print_bytes_exact​(byte[] data, int offset, int length)
      DOCUMENT ME!
      static int round_up​(int value, int multiple)
      Round a number up to a given multiple.
      static void shortToBytes​(short s, byte[] data, int[] offset)
      Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
      static void stringToBytes​(String s, byte[] data, int[] offset)
      Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
      static byte[] zero_pad​(byte[] original, int block_size)
      Return a new array equal to original except zero-padded to an integral mulitple of blocks.
    • Constructor Detail

      • ByteUtils

        public ByteUtils()
    • Method Detail

      • bytesToInt

        public static final int bytesToInt​(byte[] data,
                                           int[] offset)
        Return the int represented by the bytes in data staring at offset offset[0].
        Parameters:
        data - the array from which to read
        offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
        Returns:
        the value of the int decoded
      • intToBytes

        public static final void intToBytes​(int i,
                                            byte[] data,
                                            int[] offset)
        Write the bytes representing i into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
        Parameters:
        i - the int to encode
        data - The byte array to store into, or null.
        offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
      • bytesToShort

        public static final short bytesToShort​(byte[] data,
                                               int[] offset)
        Return the short represented by the bytes in data staring at offset offset[0].
        Parameters:
        data - the array from which to read
        offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
        Returns:
        the value of the short decoded
      • shortToBytes

        public static final void shortToBytes​(short s,
                                              byte[] data,
                                              int[] offset)
        Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
        Parameters:
        s - the short to encode
        data - The byte array to store into, or null.
        offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
      • bytesToLong

        public static final long bytesToLong​(byte[] data,
                                             int[] offset)
        Return the long represented by the bytes in data staring at offset offset[0].
        Parameters:
        data - the array from which to read
        offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
        Returns:
        the value of the long decoded
      • longToBytes

        public static final void longToBytes​(long l,
                                             byte[] data,
                                             int[] offset)
        Write the bytes representing l into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
        Parameters:
        l - the long to encode
        data - The byte array to store into, or null.
        offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
      • bytesToDouble

        public static final double bytesToDouble​(byte[] data,
                                                 int[] offset)
        Return the double represented by the bytes in data staring at offset offset[0].
        Parameters:
        data - the array from which to read
        offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
        Returns:
        the value of the double decoded
      • doubleToBytes

        public static final void doubleToBytes​(double d,
                                               byte[] data,
                                               int[] offset)
        Write the bytes representing d into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
        Parameters:
        d - the double to encode
        data - The byte array to store into, or null.
        offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
      • bytesToString

        public static final String bytesToString​(byte[] data,
                                                 int[] offset)
        Return the String represented by the bytes in data staring at offset offset[0]. This method relies on the user using the corresponding stringToBytes method to encode the String, so that it may properly retrieve the String length.
        Parameters:
        data - the array from which to read
        offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
        Returns:
        the value of the String decoded
      • stringToBytes

        public static final void stringToBytes​(String s,
                                               byte[] data,
                                               int[] offset)
        Write the bytes representing s into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
        Parameters:
        s - the String to encode
        data - The byte array to store into, or null.
        offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
      • bytesToBool

        public static final boolean bytesToBool​(byte[] data,
                                                int[] offset)
        Return the boolean represented by the bytes in data staring at offset offset[0].
        Parameters:
        data - the array from which to read
        offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
        Returns:
        the value of the boolean decoded
      • boolToBytes

        public static final void boolToBytes​(boolean b,
                                             byte[] data,
                                             int[] offset)
        Write the bytes representing b into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
        Parameters:
        b - the boolean to encode
        data - The byte array to store into, or null.
        offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
      • bytesToBigInteger

        public static final BigInteger bytesToBigInteger​(byte[] data,
                                                         int[] offset)
        Return the BigInteger represented by the bytes in data staring at offset offset[0].
        Parameters:
        data - the array from which to read
        offset - A single element array whose first element is the index in data from which to begin reading on function entry, and which on function exit has been incremented by the number of bytes read.
        Returns:
        the BigInteger decoded
      • bigIntegerToBytes

        public static final void bigIntegerToBytes​(BigInteger n,
                                                   byte[] data,
                                                   int[] offset)
        Write the bytes representing n into the byte array data, starting at index offset [0], and increment offset [0] by the number of bytes written; if data == null, increment offset [0] by the number of bytes that would have been written otherwise.
        Parameters:
        n - the BigInteger to encode
        data - The byte array to store into, or null.
        offset - A single element array whose first element is the index in data to begin writing at on function entry, and which on function exit has been incremented by the number of bytes written.
      • bytesToInts

        public static final void bytesToInts​(int[] dst,
                                             int dst_offset,
                                             byte[] src,
                                             int src_offset,
                                             int length)
        Convert an array of bytess into an array of ints.
        Parameters:
        dst - the array to write
        dst_offset - the start offset in dst, times 4. This measures the offset as if dst were an array of bytes (rather than ints).
        src - the array to read
        src_offset - the start offset in src
        length - the number of bytes to copy.
      • intsToBytes

        public static final void intsToBytes​(byte[] dst,
                                             int dst_offset,
                                             int[] src,
                                             int src_offset,
                                             int length)
        Convert an array of ints into an array of bytes.
        Parameters:
        dst - the array to write
        dst_offset - the start offset in dst
        src - the array to read
        src_offset - the start offset in src, times 4. This measures the offset as if src were an array of bytes (rather than ints).
        length - the number of bytes to copy.
      • byteToUnsignedInt

        public static final int byteToUnsignedInt​(byte b)
        Convert a byte into an unsigned integer.
        Parameters:
        b - the byte to cast
        Returns:
        a postiive int whose lowest byte contains the bits of b.
      • memcpy

        public static int memcpy​(byte[] dst,
                                 int dst_offset,
                                 byte[] src,
                                 int src_offset,
                                 int length)
        Copy contents of one array of bytes into another. If either array is null, simply return the length parameter directly.
        Parameters:
        dst - the array to write, or null
        dst_offset - the start offset in dst
        src - the array to read, or null
        src_offset - the start offset in src
        length - the number of bytes to copy.
        Returns:
        DOCUMENT ME!
      • memcmp

        public static boolean memcmp​(byte[] a,
                                     int a_offset,
                                     byte[] b,
                                     int b_offset,
                                     int length)
        Compare the contents of one array of bytes to another.
        Parameters:
        a - the first array
        a_offset - the start offset in a
        b - the second array
        b_offset - the start offset in b
        length - the number of bytes to compare.
        Returns:
        DOCUMENT ME!
      • memclr

        public static void memclr​(byte[] array,
                                  int offset,
                                  int length)
        Fill the given array with zeros.
        Parameters:
        array - the array to clear
        offset - the start offset
        length - the number of bytes to clear.
      • round_up

        public static int round_up​(int value,
                                   int multiple)
        Round a number up to a given multiple.
        Parameters:
        value - the number to be rounded
        multiple - the number to which to be rounded
        Returns:
        the smallest int greater than or equal to value which divides multiple exactly.
      • zero_pad

        public static byte[] zero_pad​(byte[] original,
                                      int block_size)
        Return a new array equal to original except zero-padded to an integral mulitple of blocks. If the original is already an integral multiple of blocks, just return it.
        Parameters:
        original - the array of bytes to be padded
        block_size - the size of the blocks
        Returns:
        an array whose size divides block_size exactly. The array is either original itself, or a copy whose first original.length bytes are equal to original.
      • equals

        public static boolean equals​(byte[] b1,
                                     byte[] b2)
        Determines whether two arrays of bytes contain the same contents.
        Parameters:
        b1 - The first array
        b2 - The second array
        Returns:
        true if both arrays are null, both empty, or both of the same length with equal contents.
      • print_bytes

        public static String print_bytes​(byte[] data,
                                         int offset,
                                         int length)
        Produce a String representation for the specified array of bytes. Print each byte as two hexadecimal digits.
        Parameters:
        data - The array to print
        offset - the start offset in data
        length - the number of bytes to print
        Returns:
        DOCUMENT ME!
      • print_bytes_exact

        public static String print_bytes_exact​(byte[] data,
                                               int offset,
                                               int length)
        DOCUMENT ME!
        Parameters:
        data - DOCUMENT ME!
        offset - DOCUMENT ME!
        length - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • print_bytes

        public static String print_bytes​(byte[] data)
        DOCUMENT ME!
        Parameters:
        data - DOCUMENT ME!
        Returns:
        DOCUMENT ME!
      • getUnsigned

        public static int getUnsigned​(byte b)
        DOCUMENT ME!
        Parameters:
        b - DOCUMENT ME!
        Returns:
        DOCUMENT ME!