Base64

org.apache.commons.codec.binary.base64 Example

Base64 class is used for  Base64 encoding and decoding as defined by RFC 2045.  There are various constructors with  URL-Safe mode, Line Length and Line Separator parameters. The URL Safe parameter is used to encode operations. Decoding handles URL safe mode on and off.  Bytestreams are used directly by Base64 class. Character streams are not used.

ThreadSafe Base64 class has static methods and non static methods for encoding and decoding. Encoding and decoding is based on character encodings ISO 8859-1, Windows-1252 and UTF-8.

Base64 class is dependant on commons-codec-1.2.jar

Source Code Example

The example below shows the sample for  Base64 class  implementation and usage.

Base64Example.java:

package com.architectcorner.util.codec;

import java.util.Random;

import org.apache.commons.codec.binary.Base64;

/**
 * @author Bhagvan Kommadi
 * Base64 Example demonstrates the usage of base64 
 * encoding and decoding
 *
 */
public class Base64Example {

	/**
	 * This method shows the encoding and decoding of string and binary data
	 */
	public static void main(String[] args) {
		
		
		String encodedString = "This is Base64 encoding and decoding example";
		Base64 base64 = new Base64();
		
		String encodedVersion = new String(base64.encode(encodedString.getBytes()));
		
		System.out.println("Encoded Version is " + encodedVersion);
		
		String decodedVersion = new String(base64.decode(encodedVersion.getBytes()));
		
		System.out.println("Decoded version is "+ decodedVersion);
		
		
		Base64 binaryBase64 = new Base64();
		Random binaryRandomData = new Random();
		byte[] binaryRandomBytes = new byte[32];
		binaryRandomData.nextBytes(binaryRandomBytes);
		
		
		String dataInternalVersion =  new String(binaryBase64.encodeBase64(binaryRandomBytes));
		
		System.out.println("Encoded version of binary data is " + dataInternalVersion);
		
		String decodedData = new String(binaryBase64.decodeBase64(dataInternalVersion));
		
				
	}

}

Output

Encoded Version is VGhpcyBpcyBCYXNlNjQgZW5jb2RpbmcgYW5kIGRlY29kaW5nIGV4YW1wbGU=
Decoded version is This is Base64 encoding and decoding example
Encoded version of binary data is mZ7gLei4/uu2r70nxAuktZCgfAjdvrwV0dHSnqarSC0=
Tip
Base64 class can be used for encoding and decoding text and binary data.

Conclusion

Base64 class has both static and non static methods for base64 encoding and decoding for text and binary data.It can be used for chunking texts for encoding text.

Download
You can download the source code of the example here: Base64Example.zip

Bhagvan Kommadi

Bhagvan Kommadi is the Founder of Architect Corner & has around 20 years’ experience in the industry, ranging from large scale enterprise development to helping incubate software product start-ups. He has done Masters in Industrial Systems Engineering at Georgia Institute of Technology (1997) and Bachelors in Aerospace Engineering from Indian Institute of Technology, Madras (1993). He is member of IFX forum,Oracle JCP and participant in Java Community Process. He founded Quantica Computacao, the first quantum computing startup in India. Markets and Markets have positioned Quantica Computacao in ‘Emerging Companies’ section of Quantum Computing quadrants. Bhagvan has engineered and developed simulators and tools in the area of quantum technology using IBM Q, Microsoft Q# and Google QScript. He has reviewed the Manning book titled : "Machine Learning with TensorFlow”. He is also the author of Packt Publishing book - "Hands-On Data Structures and Algorithms with Go".He is member of IFX forum,Oracle JCP and participant in Java Community Process. He is member of the MIT Technology Review Global Panel.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pankaj B
Pankaj B
3 years ago

hello tried your example , library are missing ,can you kindly share org.apache.commons.codec.binary jar

Back to top button