public class HexCodec extends Codec
[0x01, 0x23, 0xEF]
is encoded as the
string "0123EF"
).
When converting to a string, you may add an optional separator every N
characters (which must be even). This is normally used to create line
breaks, but may be anything: you can create a comma-delimited list with
the separator "\",\""
(although you'll have to prepend and
append the opening and closing quotes).
Conversion to byte arrays always ignores whitespace, and also ignores the
specified separator (if one exists). Any other characters throw an
IllegalArgumentException
.
Conversion to byte arrays also ignores any trailing nibble: only an even number of characters will be processed.
EMPTY_ARRAY
Constructor and Description |
---|
HexCodec()
Creates an instance that produces unbroken strings of hex digits.
|
HexCodec(int lineLength,
String separator)
Creates an instance that produces strings with separators inserted every
lineLength characters, and ignores the specified separator
when reading strings. |
Modifier and Type | Method and Description |
---|---|
void |
decode(InputStream in,
OutputStream out)
Decodes a stream according to the rules of the codec.
|
void |
encode(InputStream in,
OutputStream out)
Encodes a stream according to the rules of the codec.
|
byte[] |
toBytes(String str)
Decodes the passed string and returns it as a byte array.
|
String |
toString(byte[] data)
Encodes the passed array and returns it as a string.
|
decode, decode, encode, encode, nextNonWhitespace, skipIfSeparator, wrapIfNeeded
public HexCodec()
public HexCodec(int lineLength, String separator)
lineLength
characters, and ignores the specified separator
when reading strings.
Note: the separator will be encoded using UTF-8.
public void encode(InputStream in, OutputStream out)
Codec
public void decode(InputStream in, OutputStream out)
Codec
public String toString(byte[] data)
public byte[] toBytes(String str)