org.apache.commons.codec.binary

Class BinaryCodec

Implemented Interfaces:
BinaryDecoder, BinaryEncoder, Decoder, Encoder

public class BinaryCodec
extends Object
implements BinaryDecoder, BinaryEncoder

Translates between byte arrays and strings of "0"s and "1"s.

Version:
$Id $

Author:
Apache Software Foundation

Since:
1.3

Field Summary

private static int[]
BITS
private static int
BIT_0
Mask for bit 0 of a byte.
private static int
BIT_1
Mask for bit 1 of a byte.
private static int
BIT_2
Mask for bit 2 of a byte.
private static int
BIT_3
Mask for bit 3 of a byte.
private static int
BIT_4
Mask for bit 4 of a byte.
private static int
BIT_5
Mask for bit 5 of a byte.
private static int
BIT_6
Mask for bit 6 of a byte.
private static int
BIT_7
Mask for bit 7 of a byte.
private static byte[]
EMPTY_BYTE_ARRAY
Empty byte array.
private static char[]
EMPTY_CHAR_ARRAY
Empty char array.

Method Summary

Object
decode(Object ascii)
Decodes a byte array where each byte represents an ascii '0' or '1'.
byte[]
decode(byte[] ascii)
Decodes a byte array where each byte represents an ascii '0' or '1'.
Object
encode(Object raw)
Converts an array of raw binary data into an array of ascii 0 and 1 chars.
byte[]
encode(byte[] raw)
Converts an array of raw binary data into an array of ascii 0 and 1 characters.
static byte[]
fromAscii(byte[] ascii)
Decodes a byte array where each byte represents an ascii '0' or '1'.
static byte[]
fromAscii(char[] ascii)
Decodes a byte array where each char represents an ascii '0' or '1'.
static byte[]
toAsciiBytes(byte[] raw)
Converts an array of raw binary data into an array of ascii 0 and 1 character bytes - each byte is a truncated char.
static char[]
toAsciiChars(byte[] raw)
Converts an array of raw binary data into an array of ascii 0 and 1 characters.
static String
toAsciiString(byte[] raw)
Converts an array of raw binary data into a String of ascii 0 and 1 characters.
byte[]
toByteArray(String ascii)
Decodes a String where each char of the String represents an ascii '0' or '1'.

Field Details

BITS

private static final int[] BITS


BIT_0

private static final int BIT_0
Mask for bit 0 of a byte.

Field Value:
1


BIT_1

private static final int BIT_1
Mask for bit 1 of a byte.

Field Value:
2


BIT_2

private static final int BIT_2
Mask for bit 2 of a byte.

Field Value:
4


BIT_3

private static final int BIT_3
Mask for bit 3 of a byte.

Field Value:
8


BIT_4

private static final int BIT_4
Mask for bit 4 of a byte.

Field Value:
16


BIT_5

private static final int BIT_5
Mask for bit 5 of a byte.

Field Value:
32


BIT_6

private static final int BIT_6
Mask for bit 6 of a byte.

Field Value:
64


BIT_7

private static final int BIT_7
Mask for bit 7 of a byte.

Field Value:
128


EMPTY_BYTE_ARRAY

private static final byte[] EMPTY_BYTE_ARRAY
Empty byte array.


EMPTY_CHAR_ARRAY

private static final char[] EMPTY_CHAR_ARRAY
Empty char array.

Method Details

decode

public Object decode(Object ascii)
            throws DecoderException
Decodes a byte array where each byte represents an ascii '0' or '1'.
Specified by:
decode in interface Decoder

Parameters:
ascii - each byte represents an ascii '0' or '1'

Returns:
the raw encoded binary where each bit corresponds to a byte in the byte array argument

Throws:
DecoderException - if argument is not a byte[], char[] or String

See Also:
org.apache.commons.codec.Decoder.decode(java.lang.Object)


decode

public byte[] decode(byte[] ascii)
Decodes a byte array where each byte represents an ascii '0' or '1'.
Specified by:
decode in interface BinaryDecoder

Parameters:
ascii - each byte represents an ascii '0' or '1'

Returns:
the raw encoded binary where each bit corresponds to a byte in the byte array argument

See Also:
Decoder.decode(Object)


encode

public Object encode(Object raw)
            throws EncoderException
Converts an array of raw binary data into an array of ascii 0 and 1 chars.
Specified by:
encode in interface Encoder

Parameters:
raw - the raw binary data to convert

Returns:
0 and 1 ascii character chars one for each bit of the argument

Throws:
EncoderException - if the argument is not a byte[]

See Also:
org.apache.commons.codec.Encoder.encode(java.lang.Object)


encode

public byte[] encode(byte[] raw)
Converts an array of raw binary data into an array of ascii 0 and 1 characters.
Specified by:
encode in interface BinaryEncoder

Parameters:
raw - the raw binary data to convert

Returns:
0 and 1 ascii character bytes one for each bit of the argument

See Also:
BinaryEncoder.encode(byte[])


fromAscii

public static byte[] fromAscii(byte[] ascii)
Decodes a byte array where each byte represents an ascii '0' or '1'.

Parameters:
ascii - each byte represents an ascii '0' or '1'

Returns:
the raw encoded binary where each bit corresponds to a byte in the byte array argument


fromAscii

public static byte[] fromAscii(char[] ascii)
Decodes a byte array where each char represents an ascii '0' or '1'.

Parameters:
ascii - each char represents an ascii '0' or '1'

Returns:
the raw encoded binary where each bit corresponds to a char in the char array argument


toAsciiBytes

public static byte[] toAsciiBytes(byte[] raw)
Converts an array of raw binary data into an array of ascii 0 and 1 character bytes - each byte is a truncated char.

Parameters:
raw - the raw binary data to convert

Returns:
an array of 0 and 1 character bytes for each bit of the argument

See Also:
BinaryEncoder.encode(byte[])


toAsciiChars

public static char[] toAsciiChars(byte[] raw)
Converts an array of raw binary data into an array of ascii 0 and 1 characters.

Parameters:
raw - the raw binary data to convert

Returns:
an array of 0 and 1 characters for each bit of the argument

See Also:
BinaryEncoder.encode(byte[])


toAsciiString

public static String toAsciiString(byte[] raw)
Converts an array of raw binary data into a String of ascii 0 and 1 characters.

Parameters:
raw - the raw binary data to convert

Returns:
a String of 0 and 1 characters representing the binary data

See Also:
BinaryEncoder.encode(byte[])


toByteArray

public byte[] toByteArray(String ascii)
Decodes a String where each char of the String represents an ascii '0' or '1'.

Parameters:
ascii - String of '0' and '1' characters

Returns:
the raw encoded binary where each bit corresponds to a byte in the byte array argument

See Also:
Decoder.decode(Object)


commons-codec version 1.3 - Copyright © 2002-2004 - Apache Software Foundation