Home » Core Java » apache » commons » codec » binary » Base64OutputStream » org.apache.commons.codec.binary.Base64OutputStream Example

About Bhagvan Kommadi

Avatar photo
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.

org.apache.commons.codec.binary.Base64OutputStream Example

Base64OutputStream has constructors with parameter for encoding (default behavior) and decoding in a streaming fashion for unlimited size. The default line length is 76 characters for encoding. The default lineEnding is CRLF.

Base64InputStream has default behavior for decoding. There are other constructors for Base64OutputStream and Base64InputStream has different constructors to override default behavior.

Base64OutputStream takes byte streams instead of character streams. Character encodings need to be compatible with the lower 127 ASCII chart (ISO-8859-1,Windows-1252 and UTF-8.

Base64OutputStream class is dependant on commons-codec-1.2.jar and commons-io-2.4.jar.

Source Code Example

The example below shows the sample for Base64OutputStream implementation and usage.


package com.architectcorner.util.codec;

import java.io.OutputStream;
import java.util.Random;

import org.apache.commons.codec.binary.Base64OutputStream;
import org.apache.commons.io.output.NullOutputStream;

 * @author Bhagvan Kommadi
 * This class demonstrates the usage of Base64OutputStream
public class Base64OutputStreamExample

	 * @param args
	 * This method shows the implementation of Base64OutputStream
	public static void main(String[] args)
		// TODO Auto-generated method stub

		Random randomData = new Random(1024);
        byte[] byteStream = new byte[1024];

        OutputStream nullOutputStream = new NullOutputStream();
        Base64OutputStream base64OutputStream = new Base64OutputStream(nullOutputStream);


        	for (int i = 0; i < 2000; i++)

        catch(Exception exception)

        long t0 = System.currentTimeMillis();
        final int repetitions = 500000;

        	for (int i = 0; i < repetitions; i++)
        catch(Exception exception)

        long timetaken = System.currentTimeMillis() - t0;
        long totalNumberOfBytes = byteStream.length * (long) repetitions;

        double megabytesPerSec = (totalNumberOfBytes / 1024.0 / 1024) / (timetaken / 1000.0);

        System.out.println(timetaken + " ms");
        System.out.println(totalNumberOfBytes + " bytes");
        System.out.println(megabytesPerSec + " mb/sec");



21522 ms
512000000 bytes
22.68754065607286 mb/sec
Base64OutputStream can be used for encoding and decoding byte streams.


Base64OutputStream uses ISO-8859-1,Windows-1252 and UTF-8 encoding for bytestreams.

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

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

Inline Feedbacks
View all comments