Class BufferUtils

Object
com.wowza.util.BufferUtils

public class BufferUtils extends Object

BufferUtils: utilities for converting between binary data and Java primitive types. Faster than Java runtime equivalents

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String[]
     
    static final String[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    byteArrayToInt(byte[] b)
    Convert byte array to int
    static int
    byteArrayToInt(byte[] b, int offset)
    Conver byte array to int with offset
    static int
    byteArrayToInt(byte[] b, int offset, int count)
    Convert byte array to int with offset.
    static int
    byteArrayToInt(byte[] b, int offset, int count, boolean isReverse)
    Convert byte array to int with offset.
    static long
    byteArrayToLong(byte[] b)
    Convert byte array to long
    static long
    byteArrayToLong(byte[] b, int offset)
    Conver byte array to long with offset
    static long
    byteArrayToLong(byte[] b, int offset, int count)
    Convert byte array to long with offset.
    static long
    byteArrayToLong(byte[] b, int offset, int count, boolean isReverse)
    Convert byte array to long with offset.
    static int
    byteArrayToShort(byte[] b)
    Convert byte array to int
    static int
    byteArrayToShort(byte[] b, int offset)
    Conver byte array to int with offset
    static int
    byteArrayToShort(byte[] b, int offset, int count)
    Convert byte array to int with offset.
    static int
    byteArrayToShort(byte[] b, int offset, int count, boolean isReverse)
    Convert byte array to int with offset.
    static String
    Convert a byte array to a String (UTF-8 encoding assumed)
    static String
    byteArrayToString(byte[] b, int offset, int count)
    Convert a byte array to a String (UTF-8 encoding assumed)
    static String
    byteArrayToString(byte[] b, int offset, int count, String charsetName)
    Convert a byte array to a String
    static byte[]
    Decode a string as a byte array
    static int
    doCRC32(int crc, byte[] buffer, int offset, int len)
    Calculate an IEEE CRC32 value for MPEG transport stream from a starting crc value
    static String
    encodeHexString(byte[] bytes)
    Encode a byte array as a string
    static String
    encodeHexString(byte[] bytes, int offset, int len)
    Encode a byte array as a string
    static int
     
    static int
    indexOf(byte[] source, byte[] pattern)
    Finds the first occurrence of a byte pattern in a byte buffer.
    static int
    indexOfDifferent(byte[] buffer1, byte[] buffer2)
    Compare two byte buffers, and return the index of the first byte that is different.
    static byte[]
    intToByteArray(int value)
    Convert a int value to a byte array in network order
    static void
    intToByteArray(int value, byte[] buffer, int offset, int size)
    Convert a int value to a byte array in network order
    static void
    intToByteArray(int value, byte[] buffer, int offset, int size, boolean isReverse)
    Convert a int value to a byte array in network order
    static byte[]
    intToByteArray(int value, int size)
    Convert a int value to a byte array in network order
    static byte[]
    longToByteArray(long value)
    Convert a long value to a byte array in network order
    static void
    longToByteArray(long value, byte[] buffer, int offset, int size)
    Convert a long value to a byte array in network order
    static void
    longToByteArray(long value, byte[] buffer, int offset, int size, boolean isReverse)
    Convert a long value to a byte array in network order
    static byte[]
    longToByteArray(long value, int size)
    Convert a long value to a byte array in network order
    static boolean
    patternMatches(byte[] source, int offset, byte... pattern)
     
    static boolean
    startsWith(byte[] source, byte[] pattern)
     

    Methods inherited from class java.lang.Object

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

    • hexadecimal

      public static final String[] hexadecimal
    • alphas

      public static final String[] alphas
  • Constructor Details

    • BufferUtils

      public BufferUtils()
  • Method Details

    • encodeHexString

      public static String encodeHexString(byte[] bytes)
      Encode a byte array as a string
      Parameters:
      bytes - byte array
      Returns:
      string
    • encodeHexString

      public static String encodeHexString(byte[] bytes, int offset, int len)
      Encode a byte array as a string
      Parameters:
      bytes - byte array
      offset - offset
      len - length
      Returns:
      string
    • decodeHexString

      public static byte[] decodeHexString(String hexStr)
      Decode a string as a byte array
      Parameters:
      hexStr - string
      Returns:
      byte array
    • byteArrayToString

      public static String byteArrayToString(byte[] b)
      Convert a byte array to a String (UTF-8 encoding assumed)
      Parameters:
      b - byte array
      Returns:
      resultant string
    • byteArrayToString

      public static String byteArrayToString(byte[] b, int offset, int count)
      Convert a byte array to a String (UTF-8 encoding assumed)
      Parameters:
      b - byte array
      offset - offset
      count - len
      Returns:
      resultant string
    • byteArrayToString

      public static String byteArrayToString(byte[] b, int offset, int count, String charsetName)
      Convert a byte array to a String
      Parameters:
      b - byte array
      offset - offset
      count - len
      charsetName - character set name
      Returns:
      resultant string
    • byteArrayToLong

      public static long byteArrayToLong(byte[] b)
      Convert byte array to long
      Parameters:
      b - byte array (8 bytes)
      Returns:
      long value
    • byteArrayToLong

      public static long byteArrayToLong(byte[] b, int offset)
      Conver byte array to long with offset
      Parameters:
      b - byte array (8 bytes)
      offset - offset
      Returns:
      long value
    • byteArrayToLong

      public static long byteArrayToLong(byte[] b, int offset, int count)
      Convert byte array to long with offset. Count is the number of bytes. Can be less than 8. If less than 8 just fills lower bits in value.
      Parameters:
      b - byte array
      offset - offset
      count - number of bytes
      Returns:
      long value
    • byteArrayToLong

      public static long byteArrayToLong(byte[] b, int offset, int count, boolean isReverse)
      Convert byte array to long with offset. Count is the number of bytes. Can be less than 8. If less than 8 just fills lower bits in value. The reverse flag allows data to be in reverse order.
      Parameters:
      b - byte array
      offset - offset
      count - number of bytes
      isReverse - is data in reverse order
      Returns:
      long value
    • byteArrayToInt

      public static int byteArrayToInt(byte[] b)
      Convert byte array to int
      Parameters:
      b - byte array (4 bytes)
      Returns:
      int value
    • byteArrayToInt

      public static int byteArrayToInt(byte[] b, int offset)
      Conver byte array to int with offset
      Parameters:
      b - byte array (4 bytes)
      offset - offset
      Returns:
      int value
    • byteArrayToInt

      public static int byteArrayToInt(byte[] b, int offset, int count)
      Convert byte array to int with offset. Count is the number of bytes. Can be less than 4. If less than 4 just fills lower bits in value.
      Parameters:
      b - byte array
      offset - offset
      count - number of bytes
      Returns:
      int value
    • byteArrayToInt

      public static int byteArrayToInt(byte[] b, int offset, int count, boolean isReverse)
      Convert byte array to int with offset. Count is the number of bytes. Can be less than 4. If less than 4 just fills lower bits in value. The reverse flag allows data to be in reverse order.
      Parameters:
      b - byte array
      offset - offset
      count - number of bytes
      isReverse - is data in reverse order
      Returns:
      int value
    • byteArrayToShort

      public static int byteArrayToShort(byte[] b)
      Convert byte array to int
      Parameters:
      b - byte array (2 bytes)
      Returns:
      int value
    • byteArrayToShort

      public static int byteArrayToShort(byte[] b, int offset)
      Conver byte array to int with offset
      Parameters:
      b - byte array (2 bytes)
      offset - offset
      Returns:
      int value
    • byteArrayToShort

      public static int byteArrayToShort(byte[] b, int offset, int count)
      Convert byte array to int with offset. Count is the number of bytes. Can be less than 2. If less than 2 just fills lower bits in value.
      Parameters:
      b - byte array
      offset - offset
      count - number of bytes
      Returns:
      int value
    • byteArrayToShort

      public static int byteArrayToShort(byte[] b, int offset, int count, boolean isReverse)
      Convert byte array to int with offset. Count is the number of bytes. Can be less than 2. If less than 2 just fills lower bits in value. The reverse flag allows data to be in reverse order.
      Parameters:
      b - byte array
      offset - offset
      count - number of bytes
      isReverse - is data in reverse order
      Returns:
      int value
    • intToByteArray

      public static byte[] intToByteArray(int value)
      Convert a int value to a byte array in network order
      Parameters:
      value - value
      Returns:
      4-byte array with value
    • intToByteArray

      public static byte[] intToByteArray(int value, int size)
      Convert a int value to a byte array in network order
      Parameters:
      value - value
      size - size of resultant byte array
      Returns:
      size-byte array with value
    • intToByteArray

      public static void intToByteArray(int value, byte[] buffer, int offset, int size)
      Convert a int value to a byte array in network order
      Parameters:
      value - value
      buffer - destination byte array
      offset - starting offset in byte array
      size - number of bytes to write
    • intToByteArray

      public static void intToByteArray(int value, byte[] buffer, int offset, int size, boolean isReverse)
      Convert a int value to a byte array in network order
      Parameters:
      value - value
      buffer - destination byte array
      offset - starting offset in byte array
      size - number of bytes to write
      isReverse - is data in reverse order
    • longToByteArray

      public static byte[] longToByteArray(long value)
      Convert a long value to a byte array in network order
      Parameters:
      value - value
      Returns:
      8-byte array with value
    • longToByteArray

      public static byte[] longToByteArray(long value, int size)
      Convert a long value to a byte array in network order
      Parameters:
      value - value
      size - size of resultant byte array
      Returns:
      size-byte array with value
    • longToByteArray

      public static void longToByteArray(long value, byte[] buffer, int offset, int size)
      Convert a long value to a byte array in network order
      Parameters:
      value - value
      buffer - destination byte array
      offset - starting offset in byte array
      size - number of bytes to write
    • longToByteArray

      public static void longToByteArray(long value, byte[] buffer, int offset, int size, boolean isReverse)
      Convert a long value to a byte array in network order
      Parameters:
      value - value
      buffer - destination byte array
      offset - starting offset in byte array
      size - number of bytes to write
      isReverse - is data in reverse order
    • getUnsignedShort

      public static int getUnsignedShort(ByteBuffer buffer)
    • doCRC32

      public static int doCRC32(int crc, byte[] buffer, int offset, int len)
      Calculate an IEEE CRC32 value for MPEG transport stream from a starting crc value
      Parameters:
      crc - starting crc value
      buffer - buffer
      offset - offset
      len - len
      Returns:
      crc value
    • indexOf

      public static int indexOf(byte[] source, byte[] pattern)
      Finds the first occurrence of a byte pattern in a byte buffer.

      Note: If we want to improve the performance, we could implement the Knuth-Morris-Pratt algorithm. But I don't need the speed nor do I have time today!

      Parameters:
      source - The source byte buffer
      pattern - the pattern we're looking for.
      Returns:
      the index of source where the pattern is found, else -1
    • patternMatches

      public static boolean patternMatches(byte[] source, int offset, byte... pattern)
      Parameters:
      source - byte array, which is to be searched
      offset - offset that the pattern will be matched from
      pattern - variable number of bytes to look for from index
      Returns:
      Return true if the input pattern matches from offset in the byte array passed in. Otherwise, return false.
    • indexOfDifferent

      public static int indexOfDifferent(byte[] buffer1, byte[] buffer2)
      Compare two byte buffers, and return the index of the first byte that is different.
      Parameters:
      buffer1 - byte array1
      buffer2 - byte array2
      Returns:
      index where buffers differ, or -1.
    • startsWith

      public static boolean startsWith(byte[] source, byte[] pattern)