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

About Bhagvan Kommadi

Bhagvan Kommadi
Bhagvan Kommadi is the Founder of Architect Corner & has around 19 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.

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.

Base64OutputStreamExample.java

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];
        randomData.nextBytes(byteStream);


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

        try
        {

        	for (int i = 0; i < 2000; i++)
        	{
        		base64OutputStream.write(byteStream);
        	}
        	Thread.sleep(100);

        }
        catch(Exception exception)
        {
        	System.out.println(exception.getMessage());
        }

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

        	for (int i = 0; i < repetitions; i++)
        	{
        		base64OutputStream.write(byteStream);
        	}
        	base64OutputStream.close();
        }
        catch(Exception exception)
        {
        	System.out.println(exception.getMessage());
        }

        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");
    }

}

Output

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

Conclusion

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

Download
You can download the source code of the example here:  Base64OutputStreamExample.zip
(No Ratings Yet)
Start the discussion Views Tweet it!

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

 

Leave a Reply

avatar
  Subscribe  
Notify of