org.apache.commons.codec.binary.BinaryCodec Example

Q

What is org.apache.commons.codec.binary.BinaryCodec class? How to use org.apache.commons.codec.binary.BinaryCodec class?

✍: FYIcenter.com

A

org.apache.commons.codec.binary.BinaryCodec class is a Java class offered in commons-codec.jar that converts between byte arrays and strings of "0"s and "1"s.

Two commonly used instance methods in org.apache.commons.codec.binary.BinaryCodec class are:

1. The encoding method:

public byte[] encode(byte[] raw)

Converts an array of raw binary data into an array of ASCII 0 and 1 characters.
The last byte from the input is converted into 8 bytes first.

Parameters:
    raw - the raw binary data to convert
Returns:
    0 and 1 ASCII character bytes, one for each bit of the argument.

2. The decoding method:

public byte[] decode(byte[] ascii)

Decodes a byte array where each byte represents an ASCII '0' or '1'.
The last 8 bytes from the input is converted into 1 byte first.

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

Here is a simple example of org.apache.commons.codec.binary.Base64 class:

// Copyright (c) 2016 FYIcenter.com
import org.apache.commons.codec.binary.BinaryCodec;

// Example of using the BinaryCodec class
public class BinaryCodecExample {
   public static void main(String[] args) throws Exception {
      BinaryCodec codec = new BinaryCodec();
      
      System.out.println("BinaryCodec encode() Example:");
      byte[] inputBytes = new byte[4];
      inputBytes[0] = (byte) 0xff;
      inputBytes[1] = (byte) 0xf0;
      inputBytes[2] = (byte) 0x0f;
      inputBytes[3] = (byte) 0x00;
      byte[] outputBytes = codec.encode(inputBytes);
      String outputString = new String(outputBytes,"UTF8");
      String expectedString = "00000000000011111111000011111111";
      System.out.println("   Input bytes: "+(inputBytes[0]&0xff)
                                      +", "+(inputBytes[1]&0xff)
                                      +", "+(inputBytes[2]&0xff)
                                      +", "+(inputBytes[3]&0xff));
      System.out.println("   Encoded string: "+outputString);
      System.out.println("   Expected string: "+expectedString);

      System.out.println("BinaryCodec decode() Example:");
      String inputString = "00000000000011111111000011111111";
      inputBytes = inputString.getBytes("UTF8");
      outputBytes = codec.decode(inputBytes);
      System.out.println("   Input string: "+inputString);
      System.out.println("   Encoded bytes: "+(outputBytes[0]&0xff)
                                      +", "+(outputBytes[1]&0xff)
                                      +", "+(outputBytes[2]&0xff)
                                      +", "+(outputBytes[3]&0xff));
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>\local\jdk-1.8.0\bin\javac 
   -cp C:\local\commons-codec-1.10\commons-codec-1.10.jar 
   BinaryCodecExample.java

C:\fyicenter>\local\jdk-1.8.0\bin\java
   -cp .;C:\local\commons-codec-1.10\commons-codec-1.10.jar 
   BinaryCodecExample

BinaryCodec encode() Example:
   Input bytes: 255, 240, 15, 0
   Encoded string: 00000000000011111111000011111111
   Expected string: 00000000000011111111000011111111
BinaryCodec decode() Example:
   Input string: 00000000000011111111000011111111
   Encoded bytes: 255, 240, 15, 0

 

Using commons-codec.jar in Java Programs

⇒⇒FAQ for Apache commons-codec.jar

2017-04-08, 407👍, 0💬