Package org.apache.mina.proxy.utils
Class ByteUtilities
- java.lang.Object
-
- org.apache.mina.proxy.utils.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 theuseUnicode
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 atstart
offset and writingcount
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 flagtestFlag
is set in theflags
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 arrayb
at offsetoffset
.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 arrayb
at offsetoffset
.
-
-
-
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 fromstart
- The starting positioncount
- 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 arraycount
- 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 atstart
offset and writingcount
bytes.- Parameters:
num
- the int to convert to a byte arraybuf
- the buffer to write the bytes tostart
- the offset from beginning for the write operationcount
- 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 arrayb
at offsetoffset
.- Parameters:
v
- the short to writeb
- the byte array to write tooffset
- 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 arrayb
at offsetoffset
.- Parameters:
v
- the int to writeb
- the byte array to write tooffset
- 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 arrayoffset
- the offset at which to change word startlength
- 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 arrayoffset
- the offset at which to change word startlength
- 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 theuseUnicode
argument.- Parameters:
s
- the string to encodeuseUnicode
- 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 stringseparator
- 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 readoffset
- 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 readoffset
- the offset at which to start- Returns:
- the int value
-
isFlagSet
public static final boolean isFlagSet(int flagSet, int testFlag)
Returns true if the flagtestFlag
is set in theflags
flagset.- Parameters:
flagSet
- the flagset to testtestFlag
- the flag we search the presence of- Returns:
- true if testFlag is present in the flagset, false otherwise.
-
-