Class ByteUtils

java.lang.Object
org.fisco.bcos.sdk.utils.ByteUtils

public class ByteUtils
extends java.lang.Object
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static byte[] EMPTY_BYTE_ARRAY  
    static byte[] ZERO_BYTE_ARRAY  
  • Constructor Summary

    Constructors 
    Constructor Description
    ByteUtils()  
  • Method Summary

    Modifier and Type Method Description
    static byte[] and​(byte[] b1, byte[] b2)  
    static byte[] appendByte​(byte[] bytes, byte b)
    Creates a copy of bytes and appends b to the end of it
    static short bigEndianToShort​(byte[] bs)  
    static short bigEndianToShort​(byte[] bs, int off)  
    static byte[] bigIntegerToBytes​(java.math.BigInteger value)  
    static byte[] bigIntegerToBytes​(java.math.BigInteger b, int numBytes)
    The regular BigInteger.toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
    static byte[] bigIntegerToBytesSigned​(java.math.BigInteger b, int numBytes)  
    static int byteArrayToInt​(byte[] b)
    Cast hex encoded value from byte[] to int null is parsed like byte[0]
    static long byteArrayToLong​(byte[] b)
    Cast hex encoded value from byte[] to long null is parsed like byte[0]
    static java.math.BigInteger bytesToBigInteger​(byte[] bb)
    Cast hex encoded value from byte[] to BigInteger null is parsed like byte[0]
    static int[] bytesToInts​(byte[] arr, boolean bigEndian)  
    static void bytesToInts​(byte[] b, int[] arr, boolean bigEndian)  
    static java.lang.String bytesToIp​(byte[] bytesIp)
    Converts 4 bytes IPv4 IP to String representation
    static byte[] calcPacketLength​(byte[] msg)
    Calculate packet length
    static byte[] copyToArray​(java.math.BigInteger value)
    Utility function to copy a byte array into a new byte array with given size.
    static java.util.Set<byte[]> difference​(java.util.Set<byte[]> setA, java.util.Set<byte[]> setB)  
    static byte[] encodeDataList​(java.lang.Object... args)
    encode the values and concatenate together
    static int firstNonZeroByte​(byte[] data)  
    static int getBit​(byte[] data, int pos)  
    static byte[] hexStringToBytes​(java.lang.String data)
    Converts string hex representation to data bytes Accepts following hex: - with or without 0x prefix
    static byte[] hostToBytes​(java.lang.String ip)
    Converts string representation of host/ip to 4-bytes byte[] IPv4
    static boolean increment​(byte[] bytes)
    increment byte array as a number until max is reached
    static byte[] intsToBytes​(int[] arr, boolean bigEndian)  
    static void intsToBytes​(int[] arr, byte[] b, boolean bigEndian)  
    static byte[] intToBytes​(int val)
    Converts int value into a byte array.
    static byte[] intToBytesNoLeadZeroes​(int val)
    Converts a int value into a byte array.
    static boolean isNullOrZeroArray​(byte[] array)  
    static boolean isSingleZero​(byte[] array)  
    static int length​(byte[]... bytes)  
    static byte[] longToBytes​(long val)
    Converts a long value into a byte array.
    static byte[] longToBytesNoLeadZeroes​(long val)
    Converts a long value into a byte array.
    static int matchingNibbleLength​(byte[] a, byte[] b)
    Returns the amount of nibbles that match each other from 0 ...
    static byte[] merge​(byte[]... arrays)  
    static java.lang.String nibblesToPrettyString​(byte[] nibbles)
    Turn nibbles to a pretty looking output string
    static int numberOfLeadingZeros​(byte[] bytes)
    Returns a number of zero bits preceding the highest-order ("leftmost") one-bit interpreting input array as a big-endian integer value
    static int numBytes​(java.lang.String val)
    Calculate the number of bytes need to encode the number
    static java.lang.String oneByteToHexString​(byte value)  
    static byte[] or​(byte[] b1, byte[] b2)  
    static byte[] parseBytes​(byte[] input, int offset, int len)
    Parses fixed number of bytes starting from offset in input array.
    static byte[] parseWord​(byte[] input, int idx)
    Parses 32-bytes word from given input.
    static byte[] parseWord​(byte[] input, int offset, int idx)
    Parses 32-bytes word from given input.
    static byte[] setBit​(byte[] data, int pos, int val)  
    static byte[] shortToBytes​(short n)  
    static byte[] stripLeadingZeroes​(byte[] data)  
    static byte[] trimLeadingBytes​(byte[] bytes, byte b)  
    static byte[] trimLeadingZeroes​(byte[] bytes)  
    static byte[] xor​(byte[] b1, byte[] b2)  
    static byte[] xorAlignRight​(byte[] b1, byte[] b2)
    XORs byte arrays of different lengths by aligning length of the shortest via adding zeros at beginning

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

  • Method Details

    • appendByte

      public static byte[] appendByte​(byte[] bytes, byte b)
      Creates a copy of bytes and appends b to the end of it
      Parameters:
      bytes - the original bytes
      b - the appended byte
      Returns:
      a appended bytes @
    • bigIntegerToBytes

      public static byte[] bigIntegerToBytes​(java.math.BigInteger b, int numBytes)
      The regular BigInteger.toByteArray() method isn't quite what we often need: it appends a leading zero to indicate that the number is positive and may need padding.
      Parameters:
      b - the integer to format into a byte array
      numBytes - the desired size of the resulting byte array
      Returns:
      numBytes byte long array.
    • bigIntegerToBytes

      public static byte[] bigIntegerToBytes​(java.math.BigInteger value)
    • bigIntegerToBytesSigned

      public static byte[] bigIntegerToBytesSigned​(java.math.BigInteger b, int numBytes)
    • bytesToBigInteger

      public static java.math.BigInteger bytesToBigInteger​(byte[] bb)
      Cast hex encoded value from byte[] to BigInteger null is parsed like byte[0]
      Parameters:
      bb - byte array contains the values
      Returns:
      unsigned positive BigInteger value.
    • matchingNibbleLength

      public static int matchingNibbleLength​(byte[] a, byte[] b)
      Returns the amount of nibbles that match each other from 0 ... amount will never be larger than smallest input
      Parameters:
      a - - first input
      b - - second input
      Returns:
      Number of bytes that match
    • longToBytes

      public static byte[] longToBytes​(long val)
      Converts a long value into a byte array.
      Parameters:
      val - - long value to convert
      Returns:
      byte[] of length 8, representing the long value
    • longToBytesNoLeadZeroes

      public static byte[] longToBytesNoLeadZeroes​(long val)
      Converts a long value into a byte array.
      Parameters:
      val - - long value to convert
      Returns:
      decimal value with leading byte that are zeroes striped
    • intToBytes

      public static byte[] intToBytes​(int val)
      Converts int value into a byte array.
      Parameters:
      val - - int value to convert
      Returns:
      byte[] of length 4, representing the int value
    • intToBytesNoLeadZeroes

      public static byte[] intToBytesNoLeadZeroes​(int val)
      Converts a int value into a byte array.
      Parameters:
      val - - int value to convert
      Returns:
      value with leading byte that are zeroes striped
    • calcPacketLength

      public static byte[] calcPacketLength​(byte[] msg)
      Calculate packet length
      Parameters:
      msg - byte[]
      Returns:
      byte-array with 4 elements
    • byteArrayToInt

      public static int byteArrayToInt​(byte[] b)
      Cast hex encoded value from byte[] to int null is parsed like byte[0]

      Limited to Integer.MAX_VALUE: 2^32-1 (4 bytes)

      Parameters:
      b - array contains the values
      Returns:
      unsigned positive int value.
    • byteArrayToLong

      public static long byteArrayToLong​(byte[] b)
      Cast hex encoded value from byte[] to long null is parsed like byte[0]

      Limited to Long.MAX_VALUE: 263-1 (8 bytes)

      Parameters:
      b - array contains the values
      Returns:
      unsigned positive long value.
    • nibblesToPrettyString

      public static java.lang.String nibblesToPrettyString​(byte[] nibbles)
      Turn nibbles to a pretty looking output string

      Example. [ 1, 2, 3, 4, 5 ] becomes '\x11\x23\x45'

      Parameters:
      nibbles - - getting byte of data [ 04 ] and turning it to a '\x04' representation
      Returns:
      pretty string of nibbles
    • oneByteToHexString

      public static java.lang.String oneByteToHexString​(byte value)
    • numBytes

      public static int numBytes​(java.lang.String val)
      Calculate the number of bytes need to encode the number
      Parameters:
      val - - number
      Returns:
      number of min bytes used to encode the number
    • encodeDataList

      public static byte[] encodeDataList​(java.lang.Object... args)
      encode the values and concatenate together
      Parameters:
      args - Object
      Returns:
      byte[]
    • firstNonZeroByte

      public static int firstNonZeroByte​(byte[] data)
    • stripLeadingZeroes

      public static byte[] stripLeadingZeroes​(byte[] data)
    • increment

      public static boolean increment​(byte[] bytes)
      increment byte array as a number until max is reached
      Parameters:
      bytes - byte[]
      Returns:
      boolean
    • copyToArray

      public static byte[] copyToArray​(java.math.BigInteger value)
      Utility function to copy a byte array into a new byte array with given size. If the src length is smaller than the given size, the result will be left-padded with zeros.
      Parameters:
      value - - a BigInteger with a maximum value of 2^256-1
      Returns:
      Byte array of given size with a copy of the src
    • setBit

      public static byte[] setBit​(byte[] data, int pos, int val)
    • getBit

      public static int getBit​(byte[] data, int pos)
    • and

      public static byte[] and​(byte[] b1, byte[] b2)
    • or

      public static byte[] or​(byte[] b1, byte[] b2)
    • xor

      public static byte[] xor​(byte[] b1, byte[] b2)
    • xorAlignRight

      public static byte[] xorAlignRight​(byte[] b1, byte[] b2)
      XORs byte arrays of different lengths by aligning length of the shortest via adding zeros at beginning
      Parameters:
      b1 - the first byte array
      b2 - the second byte array
      Returns:
      a byte array contains XORS of b1 and b2
    • merge

      public static byte[] merge​(byte[]... arrays)
      Parameters:
      arrays - - arrays to merge
      Returns:
      - merged array
    • isNullOrZeroArray

      public static boolean isNullOrZeroArray​(byte[] array)
    • isSingleZero

      public static boolean isSingleZero​(byte[] array)
    • difference

      public static java.util.Set<byte[]> difference​(java.util.Set<byte[]> setA, java.util.Set<byte[]> setB)
    • length

      public static int length​(byte[]... bytes)
    • bytesToInts

      public static int[] bytesToInts​(byte[] arr, boolean bigEndian)
    • bytesToInts

      public static void bytesToInts​(byte[] b, int[] arr, boolean bigEndian)
    • intsToBytes

      public static byte[] intsToBytes​(int[] arr, boolean bigEndian)
    • intsToBytes

      public static void intsToBytes​(int[] arr, byte[] b, boolean bigEndian)
    • bigEndianToShort

      public static short bigEndianToShort​(byte[] bs)
    • bigEndianToShort

      public static short bigEndianToShort​(byte[] bs, int off)
    • shortToBytes

      public static byte[] shortToBytes​(short n)
    • hexStringToBytes

      public static byte[] hexStringToBytes​(java.lang.String data)
      Converts string hex representation to data bytes Accepts following hex: - with or without 0x prefix
      Parameters:
      data - String like '0xa5e..' or just 'a5e..'
      Returns:
      decoded bytes array
    • hostToBytes

      public static byte[] hostToBytes​(java.lang.String ip)
      Converts string representation of host/ip to 4-bytes byte[] IPv4
      Parameters:
      ip - the ip string of host
      Returns:
      a 4-bytes byte[] IPv4
    • bytesToIp

      public static java.lang.String bytesToIp​(byte[] bytesIp)
      Converts 4 bytes IPv4 IP to String representation
      Parameters:
      bytesIp - the 4 bytes IPv4 IP
      Returns:
      a String representation of the IP
    • numberOfLeadingZeros

      public static int numberOfLeadingZeros​(byte[] bytes)
      Returns a number of zero bits preceding the highest-order ("leftmost") one-bit interpreting input array as a big-endian integer value
      Parameters:
      bytes - the byte array
      Returns:
      the number of leading zeros
    • parseBytes

      public static byte[] parseBytes​(byte[] input, int offset, int len)
      Parses fixed number of bytes starting from offset in input array. If input has not enough bytes return array will be right padded with zero bytes. I.e. if offset is higher than input.length then zero byte array of length len will be returned
      Parameters:
      input - the input bytes array
      offset - an offset in input array to start parsing from
      len - the length of zero byte array
      Returns:
      a fixed bytes array
    • parseWord

      public static byte[] parseWord​(byte[] input, int idx)
      Parses 32-bytes word from given input. Uses parseBytes(byte[], int, int) method, thus, result will be right-padded with zero bytes if there is not enough bytes in input
      Parameters:
      input - the input bytes array
      idx - an index of the word starting from 0
      Returns:
      a fixed bytes array
    • parseWord

      public static byte[] parseWord​(byte[] input, int offset, int idx)
      Parses 32-bytes word from given input. Uses parseBytes(byte[], int, int) method, thus, result will be right-padded with zero bytes if there is not enough bytes in input
      Parameters:
      input - the input bytes array
      idx - an index of the word starting from 0
      offset - an offset in input array to start parsing from
      Returns:
      a fixed bytes array
    • trimLeadingBytes

      public static byte[] trimLeadingBytes​(byte[] bytes, byte b)
    • trimLeadingZeroes

      public static byte[] trimLeadingZeroes​(byte[] bytes)