Home All Groups
commons-codec / org / apache / commons / codec / binary / BinaryCodec.html /
org.apache.commons.codec.binary

Class BinaryCodec

  • All Implemented Interfaces:
    BinaryDecoder, BinaryEncoder, Decoder, Encoder

    public class BinaryCodec
    extends 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
    Version:
    $Id: BinaryCodec.java 1429868 2013-01-07 16:08:05Z ggregory $
    • Constructor Summary

      Constructors 
      Constructor and Description
      BinaryCodec() 
    • Method Summary

      Methods 
      Modifier and TypeMethod and Description
      byte[]decode(byte[] ascii)
      Decodes a byte array where each byte represents an ASCII '0' or '1'.
      Objectdecode(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.
      Objectencode(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'.
      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 StringtoAsciiString(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'.
    • Constructor Detail

      • BinaryCodec

        public BinaryCodec()
    • Method Detail

      • 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[])
      • 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:
        Decoder.decode(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)
      • 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)
      • 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
      • 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
      • 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[])

Copyright © 2002–2013 The Apache Software Foundation. All rights reserved.