com.ibm.jzos
Class ByteUtil

java.lang.Object
  extended by 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
ByteUtil()
           
 
Method Summary
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 array
static 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ByteUtil

public ByteUtil()
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).


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 from
bytes - the byte[] to receive the String
offset - the starting offset in the byte array
length - 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

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 from
offset - the offset in the byte array
length - the number of bytes to convert
Returns:
String the resulting Java String
Throws:
java.io.UnsupportedEncodingException - on invalid encoding

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:

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 dump
bytes - the byte array to dump
ostream - 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 dump
bytes - the byte array to dump
ostream - the java.io.OutputStream to write the dump to
encoding - 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 dump
bytes - the byte array to dump
writer - 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 dump
bytes - the byte array to dump
offset - the 0-based offset into bytes to start dumping
len - the number of bytes to dump
bytesPerLine - the number of bytes to dump per line
writer - 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.


toHexString

public static java.lang.String toHexString(byte[] bytes,
                                           int offset,
                                           int len)
Return a hex string encoding of a byte array.