Package com.wowza.util
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 String[]
alphas
static String[]
hexadecimal
-
Constructor Summary
Constructors Constructor Description BufferUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
byteArrayToInt(byte[] b)
Convert byte array to intstatic int
byteArrayToInt(byte[] b, int offset)
Conver byte array to int with offsetstatic 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 longstatic long
byteArrayToLong(byte[] b, int offset)
Conver byte array to long with offsetstatic 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 intstatic int
byteArrayToShort(byte[] b, int offset)
Conver byte array to int with offsetstatic 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
byteArrayToString(byte[] b)
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 Stringstatic byte[]
decodeHexString(String hexStr)
Decode a string as a byte arraystatic int
doCRC32(int crc, byte[] buffer, int offset, int len)
Calculate an IEEE CRC32 value for MPEG transport stream from a starting crc valuestatic String
encodeHexString(byte[] bytes)
Encode a byte array as a stringstatic String
encodeHexString(byte[] bytes, int offset, int len)
Encode a byte array as a stringstatic int
getUnsignedShort(java.nio.ByteBuffer buffer)
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 orderstatic void
intToByteArray(int value, byte[] buffer, int offset, int size)
Convert a int value to a byte array in network orderstatic void
intToByteArray(int value, byte[] buffer, int offset, int size, boolean isReverse)
Convert a int value to a byte array in network orderstatic byte[]
intToByteArray(int value, int size)
Convert a int value to a byte array in network orderstatic byte[]
longToByteArray(long value)
Convert a long value to a byte array in network orderstatic void
longToByteArray(long value, byte[] buffer, int offset, int size)
Convert a long value to a byte array in network orderstatic void
longToByteArray(long value, byte[] buffer, int offset, int size, boolean isReverse)
Convert a long value to a byte array in network orderstatic byte[]
longToByteArray(long value, int size)
Convert a long value to a byte array in network orderstatic boolean
patternMatches(byte[] source, int offset, byte... pattern)
static boolean
startsWith(byte[] source, byte[] pattern)
-
-
-
Method Detail
-
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 arrayoffset
- offsetlen
- 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 arrayoffset
- offsetcount
- 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 arrayoffset
- offsetcount
- lencharsetName
- 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 arrayoffset
- offsetcount
- 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 arrayoffset
- offsetcount
- number of bytesisReverse
- 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 arrayoffset
- offsetcount
- 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 arrayoffset
- offsetcount
- number of bytesisReverse
- 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 arrayoffset
- offsetcount
- 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 arrayoffset
- offsetcount
- number of bytesisReverse
- 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
- valuesize
- 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
- valuebuffer
- destination byte arrayoffset
- starting offset in byte arraysize
- 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
- valuebuffer
- destination byte arrayoffset
- starting offset in byte arraysize
- number of bytes to writeisReverse
- 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
- valuesize
- 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
- valuebuffer
- destination byte arrayoffset
- starting offset in byte arraysize
- 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
- valuebuffer
- destination byte arrayoffset
- starting offset in byte arraysize
- number of bytes to writeisReverse
- is data in reverse order
-
getUnsignedShort
public static int getUnsignedShort(java.nio.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 valuebuffer
- bufferoffset
- offsetlen
- 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 bufferpattern
- 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 searchedoffset
- offset that the pattern will be matched frompattern
- 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 array1buffer2
- byte array2- Returns:
- index where buffers differ, or -1.
-
startsWith
public static boolean startsWith(byte[] source, byte[] pattern)
-
-