Base64OutputStream

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

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.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button