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
Base64OutputStream
can be used for encoding and decoding byte streams.Conclusion
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