org.dellroad.stuff.string
Class ByteArrayEncoder

java.lang.Object
  extended by org.dellroad.stuff.string.ByteArrayEncoder

public final class ByteArrayEncoder
extends Object

Encodes byte[] arrays to and from hexadecimal strings.


Method Summary
static byte[] decode(String text)
          Decode a String back into a byte[] array.
static String encode(byte[] array)
          Encode a byte[] array as a String.
static String encode(byte[] array, int off, int len)
          Encode a byte[] array as a String.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

encode

public static String encode(byte[] array)
Encode a byte[] array as a String. Equivalent to:
encode(array, 0, array.length)

Parameters:
array - byte array, or null
Returns:
hexadecimal string (or null if array was null)

encode

public static String encode(byte[] array,
                            int off,
                            int len)
Encode a byte[] array as a String.

Parameters:
array - byte array, or null
off - offset into the array
len - number of bytes to convert
Returns:
hexadecimal string (or null if array was null)
Throws:
IndexOutOfBoundsException - if array bounds are exceeded
IllegalArgumentException - if len is greater than Integer.MAX_VALUE / 2

decode

public static byte[] decode(String text)
Decode a String back into a byte[] array. Any extra whitespace in the string is ignored.

Parameters:
text - string previously encoded by encode(byte[]), or null
Returns:
original byte[] array (or null if text was null)
Throws:
IllegalArgumentException - if any invalid non-whitespace characters are seen, or the number of hex digits is odd