Package org.apache.commons.codec.binary
Class BinaryCodec
- java.lang.Object
-
- org.apache.commons.codec.binary.BinaryCodec
-
- All Implemented Interfaces:
BinaryDecoder,BinaryEncoder,Decoder,Encoder
public class BinaryCodec extends java.lang.Object implements BinaryDecoder, BinaryEncoder
Converts between byte arrays and strings of "0"s and "1"s.This class is immutable and thread-safe.
TODO: may want to add more bit vector functions like and/or/xor/nand TODO: also might be good to generate boolean[] from byte[] et cetera.- Since:
- 1.3
-
-
Field Summary
Fields Modifier and Type Field Description private static intBIT_0Mask for bit 0 of a byte.private static intBIT_1Mask for bit 1 of a byte.private static intBIT_2Mask for bit 2 of a byte.private static intBIT_3Mask for bit 3 of a byte.private static intBIT_4Mask for bit 4 of a byte.private static intBIT_5Mask for bit 5 of a byte.private static intBIT_6Mask for bit 6 of a byte.private static intBIT_7Mask for bit 7 of a byte.private static int[]BITSprivate static byte[]EMPTY_BYTE_ARRAYEmpty byte array.private static char[]EMPTY_CHAR_ARRAYEmpty char array.
-
Constructor Summary
Constructors Constructor Description BinaryCodec()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]decode(byte[] ascii)Decodes a byte array where each byte represents an ASCII '0' or '1'.java.lang.Objectdecode(java.lang.Object ascii)Decodes a byte array where each byte represents an ASCII '0' or '1'.byte[]encode(byte[] raw)Converts an array of raw binary data into an array of ASCII 0 and 1 characters.java.lang.Objectencode(java.lang.Object raw)Converts an array of raw binary data into an array of ASCII 0 and 1 chars.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 char array where each char represents an ASCII '0' or '1'.private static booleanisEmpty(byte[] array)Returnstrueif the given array isnullor empty (size 0.)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 java.lang.StringtoAsciiString(byte[] raw)Converts an array of raw binary data into a String of ASCII 0 and 1 characters.byte[]toByteArray(java.lang.String ascii)Decodes a String where each char of the String represents an ASCII '0' or '1'.
-
-
-
Field Detail
-
EMPTY_CHAR_ARRAY
private static final char[] EMPTY_CHAR_ARRAY
Empty char array.
-
EMPTY_BYTE_ARRAY
private static final byte[] EMPTY_BYTE_ARRAY
Empty byte array.
-
BIT_0
private static final int BIT_0
Mask for bit 0 of a byte.- See Also:
- Constant Field Values
-
BIT_1
private static final int BIT_1
Mask for bit 1 of a byte.- See Also:
- Constant Field Values
-
BIT_2
private static final int BIT_2
Mask for bit 2 of a byte.- See Also:
- Constant Field Values
-
BIT_3
private static final int BIT_3
Mask for bit 3 of a byte.- See Also:
- Constant Field Values
-
BIT_4
private static final int BIT_4
Mask for bit 4 of a byte.- See Also:
- Constant Field Values
-
BIT_5
private static final int BIT_5
Mask for bit 5 of a byte.- See Also:
- Constant Field Values
-
BIT_6
private static final int BIT_6
Mask for bit 6 of a byte.- See Also:
- Constant Field Values
-
BIT_7
private static final int BIT_7
Mask for bit 7 of a byte.- See Also:
- Constant Field Values
-
BITS
private static final int[] BITS
-
-
Method Detail
-
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 char 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
-
isEmpty
private static boolean isEmpty(byte[] array)
Returnstrueif the given array isnullor empty (size 0.)- Parameters:
array- the source array- Returns:
trueif the given array isnullor empty (size 0.)
-
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 java.lang.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[])
-
decode
public byte[] decode(byte[] ascii)
Decodes a byte array where each byte represents an ASCII '0' or '1'.- Specified by:
decodein interfaceBinaryDecoder- 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)
-
decode
public java.lang.Object decode(java.lang.Object ascii) throws DecoderExceptionDecodes a byte array where each byte represents an ASCII '0' or '1'.- Specified by:
decodein interfaceDecoder- 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:
Decoder.decode(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:
encodein interfaceBinaryEncoder- 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[])
-
encode
public java.lang.Object encode(java.lang.Object raw) throws EncoderExceptionConverts an array of raw binary data into an array of ASCII 0 and 1 chars.- Specified by:
encodein interfaceEncoder- 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:
Encoder.encode(Object)
-
toByteArray
public byte[] toByteArray(java.lang.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)
-
-