Package org.cryptacular.util
Class ByteUtil
java.lang.Object
org.cryptacular.util.ByteUtil
Utilities for working with bytes.
- Author:
- Middleware Services
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic intreadInt(InputStream in) Reads 4-bytes from the input stream and converts to a 32-bit integer.static longreadLong(InputStream in) Reads 8-bytes from the input stream and converts to a 64-bit long integer.static byte[]toArray(ByteBuffer buffer) Converts a byte buffer into a byte array.static ByteBufferConverts a character sequence into bytes in the UTF-8 character set.static byte[]toBytes(int value) Converts an integer into a 4-byte big endian array.static voidtoBytes(int value, byte[] output, int offset) Converts an integer into a 4-byte big endian array.static byte[]toBytes(long value) Converts a long integer into an 8-byte big endian array.static voidtoBytes(long value, byte[] output, int offset) Converts an integer into an 8-byte big endian array.static byte[]toBytes(CharSequence cs) Converts a character sequence into bytes in the UTF-8 character set.static CharBuffertoCharBuffer(ByteBuffer buffer) Converts a byte buffer into a character buffer.static inttoInt(byte unsigned) Converts an unsigned byte into an integer.static inttoInt(byte[] data) Converts the big-endian representation of a 32-bit integer to the equivalent integer value.static longtoLong(byte[] data) Converts the big-endian representation of a 64-bit integer to the equivalent long value.static StringtoString(byte[] bytes) Converts a byte array into a string in the UTF-8 character set.static StringtoString(byte[] bytes, int offset, int length) Converts a portion of a byte array into a string in the UTF-8 character set.static StringtoString(ByteBuffer buffer) Converts a byte buffer into a string in the UTF-8 character set.static bytetoUnsignedByte(int b) Converts an integer into an unsigned byte.
-
Field Details
-
DEFAULT_CHARSET
Default character set for bytes is UTF-8. -
ASCII_CHARSET
ASCII character set.
-
-
Method Details
-
toInt
public static int toInt(byte[] data) Converts the big-endian representation of a 32-bit integer to the equivalent integer value.- Parameters:
data- 4-byte array in big-endian format.- Returns:
- Integer value.
-
toInt
public static int toInt(byte unsigned) Converts an unsigned byte into an integer.- Parameters:
unsigned- Unsigned byte.- Returns:
- Integer value.
-
readInt
Reads 4-bytes from the input stream and converts to a 32-bit integer.- Parameters:
in- Stream from which to read 4 bytes.- Returns:
- Integer value.
- Throws:
StreamException- on stream IO errors.
-
toLong
public static long toLong(byte[] data) Converts the big-endian representation of a 64-bit integer to the equivalent long value.- Parameters:
data- 8-byte array in big-endian format.- Returns:
- Long integer value.
-
readLong
Reads 8-bytes from the input stream and converts to a 64-bit long integer.- Parameters:
in- Stream from which to read 8 bytes.- Returns:
- Long integer value.
- Throws:
StreamException- on stream IO errors.
-
toBytes
public static byte[] toBytes(int value) Converts an integer into a 4-byte big endian array.- Parameters:
value- Integer value to convert.- Returns:
- 4-byte big-endian representation of integer value.
-
toBytes
public static void toBytes(int value, byte[] output, int offset) Converts an integer into a 4-byte big endian array.- Parameters:
value- Integer value to convert.output- Array into which bytes are placed.offset- Offset into output array at which output bytes start.
-
toBytes
public static byte[] toBytes(long value) Converts a long integer into an 8-byte big endian array.- Parameters:
value- Long integer value to convert.- Returns:
- 8-byte big-endian representation of long integer value.
-
toBytes
public static void toBytes(long value, byte[] output, int offset) Converts an integer into an 8-byte big endian array.- Parameters:
value- Long value to convert.output- Array into which bytes are placed.offset- Offset into output array at which output bytes start.
-
toString
Converts a byte array into a string in the UTF-8 character set.- Parameters:
bytes- Byte array to convert.- Returns:
- UTF-8 string representation of bytes.
-
toString
Converts a portion of a byte array into a string in the UTF-8 character set.- Parameters:
bytes- Byte array to convert.offset- Offset into byte array where string content begins.length- Total number of bytes to convert.- Returns:
- UTF-8 string representation of bytes.
-
toString
Converts a byte buffer into a string in the UTF-8 character set.- Parameters:
buffer- Byte buffer to convert.- Returns:
- UTF-8 string representation of bytes.
-
toCharBuffer
Converts a byte buffer into a character buffer.- Parameters:
buffer- Byte buffer to convert.- Returns:
- Character buffer containing UTF-8 string representation of bytes.
-
toByteBuffer
Converts a character sequence into bytes in the UTF-8 character set.- Parameters:
cs- CharSequence to convert.- Returns:
- Byte buffer containing byte representation of the character sequence.
-
toBytes
Converts a character sequence into bytes in the UTF-8 character set.- Parameters:
cs- Character sequence to convert.- Returns:
- Byte array containing byte representation of the character sequence.
-
toUnsignedByte
public static byte toUnsignedByte(int b) Converts an integer into an unsigned byte. All bits above 1 byte are truncated.- Parameters:
b- Integer value.- Returns:
- Unsigned byte as a byte.
-
toArray
Converts a byte buffer into a byte array.- Parameters:
buffer- Byte buffer to convert.- Returns:
- Byte array corresponding to bytes of buffer from current position to limit.
-