- java.lang.Object
-
- com.ibm.jzos.ByteUtil
-
public class ByteUtil extends java.lang.Object
Helper methods for converting to/from raw byte array data- Author:
- Stephen Goetze
-
-
Constructor Summary
Constructors Constructor Description ByteUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
bytesAsInt(byte[] bytes)
Convert up to 4 bytes into an int (big endian).static int
bytesAsInt(byte[] bytes, int offset, int length)
Return an int from big endian bytes.static long
bytesAsLong(byte[] bytes)
Convert up to 8 bytes into an long (big endian).static long
bytesAsLong(byte[] bytes, int offset, int length)
Return a long from big endian bytes.static java.lang.String
bytesAsString(byte[] bytes, int offset, int length, java.lang.String encoding)
Return a String from a byte array using the supplied encoding.static void
dumpHex(java.lang.String label, byte[] bytes, int offset, int len, int bytesPerLine, java.io.Writer writer)
Dump a byte array in hex to Writer.static void
dumpHex(java.lang.String label, byte[] bytes, java.io.OutputStream ostream)
Dump a byte array in hex to an OutputStream.static void
dumpHex(java.lang.String label, byte[] bytes, java.io.OutputStream ostream, java.lang.String encoding)
Dump a byte array in hex to an OutputStream.static void
dumpHex(java.lang.String label, byte[] bytes, java.io.Writer writer)
Dump a byte array in hex to Writer.static byte[]
intAsBytes(int i)
Convert an int to four bytes (big-endian).static byte[]
longAsBytes(long l)
Convert a long to eight bytes (big-endian).static void
putInt(int i, byte[] bytes, int offset)
Convert an int to four bytes (big-endian) and put into a byte arraystatic void
putLong(long l, byte[] bytes, int offset)
Convert a long to eight bytes (big-endian) and put into a byte array.static void
putLong(long l, byte[] bytes, int offset, int len)
Convert a long to eight bytes (big-endian) and put into a byte array.static int
putString(java.lang.String str, byte[] bytes, int offset, int length, java.lang.String encoding)
Convert a String into a byte array using the supplied encoding.static java.lang.String
toHexString(byte[] bytes)
Return a hex string encoding of a byte array.static java.lang.String
toHexString(byte[] bytes, int offset, int len)
Return a hex string encoding of a byte array.static java.lang.String
toHexString(int i, int numDigits)
Return a hex string encoding of an integer to a given width.static long
unpackLong(byte[] bytes, int offset, int length, boolean isSigned)
Unpack length bytes from the supplied byte array starting at offset.
-
-
-
Method Detail
-
intAsBytes
public static byte[] intAsBytes(int i)
Convert an int to four bytes (big-endian).
-
longAsBytes
public static byte[] longAsBytes(long l)
Convert a long to eight bytes (big-endian).- Since:
- 2.1.0
-
putInt
public static void putInt(int i, byte[] bytes, int offset)
Convert an int to four bytes (big-endian) and put into a byte array
-
putLong
public static void putLong(long l, byte[] bytes, int offset, int len)
Convert a long to eight bytes (big-endian) and put into a byte array.
-
putLong
public static void putLong(long l, byte[] bytes, int offset)
Convert a long to eight bytes (big-endian) and put into a byte array.
-
putString
public static int putString(java.lang.String str, byte[] bytes, int offset, int length, java.lang.String encoding)
Convert a String into a byte array using the supplied encoding. If str < length, the bytes will be padded to length with spaces.- Parameters:
str
- the String to construct the byte array frombytes
- the byte[] to receive the Stringoffset
- the starting offset in the byte arraylength
- the number of bytes to create. Must be >= str.length()encoding
- the character encoding- Returns:
- the number of bytes put
- Throws:
java.io.UnsupportedEncodingException
- on invalid encoding- Since:
- 2.1.0
-
bytesAsInt
public static int bytesAsInt(byte[] bytes)
Convert up to 4 bytes into an int (big endian).
-
bytesAsInt
public static int bytesAsInt(byte[] bytes, int offset, int length)
Return an int from big endian bytes.
-
bytesAsLong
public static long bytesAsLong(byte[] bytes)
Convert up to 8 bytes into an long (big endian).
-
bytesAsLong
public static long bytesAsLong(byte[] bytes, int offset, int length)
Return a long from big endian bytes.
-
bytesAsString
public static java.lang.String bytesAsString(byte[] bytes, int offset, int length, java.lang.String encoding)
Return a String from a byte array using the supplied encoding.- Parameters:
bytes
- the byte array to construct the String fromoffset
- the offset in the byte arraylength
- the number of bytes to convert- Returns:
- String the resulting Java String
- Throws:
java.io.UnsupportedEncodingException
- on invalid encoding- Since:
- 2.1.0
-
unpackLong
public static long unpackLong(byte[] bytes, int offset, int length, boolean isSigned)
Unpack length bytes from the supplied byte array starting at offset. Each byte is assumed to contain two decimal digits (0-9). The high order digit is a decimal order of magnitude greater than the low order digit. If isSigned is true, the final byte is assumed to contain a decimal digit in the high order nibble and a 'sign' hex digit in the low order nibble as follows:- '0xA','0xC','0xE','0xF' - Positive
- '0xB','0xD' - Negative
- Any other value - illegal
- Returns:
- long result
-
dumpHex
public static void dumpHex(java.lang.String label, byte[] bytes, java.io.OutputStream ostream) throws java.io.IOException
Dump a byte array in hex to an OutputStream.- Parameters:
label
- a String to write as a header before the dumpbytes
- the byte array to dumpostream
- the java.io.OutputStream to write the dump to, using the default file.encoding- Throws:
java.io.IOException
-
dumpHex
public static void dumpHex(java.lang.String label, byte[] bytes, java.io.OutputStream ostream, java.lang.String encoding) throws java.io.IOException
Dump a byte array in hex to an OutputStream.- Parameters:
label
- a String to write as a header before the dumpbytes
- the byte array to dumpostream
- the java.io.OutputStream to write the dump toencoding
- the name of the encoding used to encode the OutputStream writer- Throws:
java.io.IOException
-
dumpHex
public static void dumpHex(java.lang.String label, byte[] bytes, java.io.Writer writer) throws java.io.IOException
Dump a byte array in hex to Writer.- Parameters:
label
- a String to write as a header before the dumpbytes
- the byte array to dumpwriter
- the java.io.Writer to write the dump to- Throws:
java.io.IOException
-
dumpHex
public static void dumpHex(java.lang.String label, byte[] bytes, int offset, int len, int bytesPerLine, java.io.Writer writer) throws java.io.IOException
Dump a byte array in hex to Writer.- Parameters:
label
- a String to write as a header before the dumpbytes
- the byte array to dumpoffset
- the 0-based offset into bytes to start dumpinglen
- the number of bytes to dumpbytesPerLine
- the number of bytes to dump per linewriter
- the java.io.Writer to write the dump to- Throws:
java.io.IOException
-
toHexString
public static java.lang.String toHexString(int i, int numDigits)
Return a hex string encoding of an integer to a given width.
-
toHexString
public static java.lang.String toHexString(byte[] bytes)
Return a hex string encoding of a byte array.- Since:
- 2.1.0
-
toHexString
public static java.lang.String toHexString(byte[] bytes, int offset, int len)
Return a hex string encoding of a byte array.- Since:
- 2.1.0
-
-