What is the fastest Java library for Base64 encoding images?

java base64 decode
base64 encode out of memory
base64 padding
java util base64 vs apache base64
length of base64 encoded input string is not a multiple of 4
base64url encode java
apache base64 encode example
btoa in java

I am currently using a ByteArrayOutputStream to convert BufferedImage to byte[] and then the open source class Base64Coder to convert the byte[] to char[] and then appending to a String. This is one part of a multi-step process for encoding frame sets of videos and putting them in XML friendly format. Don't ask why I am doing this, that is just what needs to be done.

I am seeing that the Base64 encoding takes up ~75% CPU time of the entire process and seeing as I just grabbed this random class off a google search, I'm certain there is something more efficient out there to encode the images. What are my options guys?


This is quite an old question, but it still turned up in Google as one of the top hits…

This has been answered comprehensively here: http://java-performance.info/base64-encoding-and-decoding-performance/

Taking the summary from there:

Let's summarize the codec properties in one table. This table is sorted by the relative performance of all these codecs (faster on top).

Name        Max encoding    Max decoding    How much we can Supports    byte[] -> byte[]
            len             len             encode with -Xmx8G
Java 8      1.62 G          2 G             1.16 G                      Yes
javax.xml   1.62 G          2 G             1.07 G                      No
MiGBase64   1.62 G          0.36 G          1.07 G                      Yes
IHarder     1.62 G          0.72 G          1.23 G                      Yes
Apache      0.81 G          0.72 G          0.8 G                       Yes
Guava       1.62 G          2 G             1.07 G                      No
Sun.misc    0.79 G          1.05 G          0.78 G                      No

If you looking for a fast and reliable Base64 codec - do not look outside JDK. There is a new codec in Java 8: java.util.Base64 and there is also one hidden from many eyes (from Java 6): javax.xml.bind.DatatypeConverter. Both are fast, reliable and do not suffer from integer overflows.

2 out of 4 3rd party codecs described here are very fast: MiGBase64 and IHarder. Unfortunately, if you will need to process hundreds of megabytes at a time, only Google Guava will allow you to decode 2G of data at a time (360MB in case of MiGBase64 / 720M in case of IHarder and Apache Commons). Unfortunately, Guava does not support byte[] -> byte[] encoding.

Do not try to call String.getBytes(Charset) on huge strings if your charset is a multibyte one - you may get the whole gamma of integer overflow related exceptions.

Base64 encoding and decoding performance, JDK Base64 implementations. Surprisingly, there was no Base64 implementation in the core JDK classes before Java 6. Some web forums  It is best to only base64 encode tiny images. If you try to base64 larger images you will end up with a great deal of code in your html and lose out on the performance benefits. How I use base64 on this site. As I mentioned above I use base64 a great deal. Every single page on this site uses base64 extensively.


Try commons-codec library at http://commons.apache.org/codec/ and definitely let us know the results. This is a standard and widely used library.

The class you are looking for is org.apache.commons.codec.binary.Base64 http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html

Image to Base64 String Conversion, A quick overview of converting an image to Base64 String and back. the file content to a byte array and use Java 8 Base64 class to encode it:. I have an image that is Base64 encoded. What is the best way to decode that in Java? Hopefully using only the libraries included with Sun Java 6.


The problem with @PaulWagland solution is that almost all the encoders will do the allocation for the encoded byte array (or variants) for you. That is they are not garbage free.

I don't really recommend the following unless you know what you are doing.

Ideally what you want to do is have a massive byte[] set to the maximum size you expect and then reuse this byte[] either with a threadlocal or some sort of pooling.

Unfortunately Base64.java has the method you want hidden:

private int decode0(byte[] src, int sp, int sl, byte[] dst) {
...
}

(I'm not going to paste the code from the JDK but I'm sure you can easily find it).

Thus if you really want to go fast you would use that method on cached byte[] arrays.

Ideally though you would want to rewrite it to use ByteBuffers.

Alternatively as a stop gap you could use Base64.Decode#wrap but the problem with that method is that it will create a wrapping InputStream which is probably better than allocating new arrays but still not garbage free. You will also need to wrap your ByteBuffer/byte[] array in its own InputStream.

IMO its flaw that the Base64 encoder/decoder doesn't have what the CharsetEncoder has which is:

CharsetEncoder.encode(CharBuffer in, ByteBuffer out, boolean endOfInput)

Ridiculously fast base64 encoding and decoding – Daniel Lemire's , When writing HTML, I find it handy to encode my images directly in the HTML is that our good results have been integrated in Klomp's base64 library. for “how a runtime-interpreted language like Java can be faster than a  In this quick tutorial, we're going to cover how to encode image file to a Base64 String, then decode it to retrieve the original image using Apache Common IO and Java 8 native Base64 features. This operation could be applied for any binary files or binary arrays.


Encoding and Decoding Base64 Strings in Java, In this tutorial, we'll be encoding and decoding Base64 Strings in Java with examples. From the latest cat image in your feed to the voice message you listened to It's a great library with many useful features often used in production - and Difference Between ArrayList and LinkedList in Java - Code and Performance. With Java 8, Base64 has finally got its due. Java 8 now has inbuilt encoder and decoder for Base64 encoding. In Java 8, we can use three types of Base64 encoding. Simple − Output is mapped to a set of characters lying in A-Za-z0-9+/. The encoder does not add any line feed in output, and the decoder rejects any character other than A-Za-z0-9+/.


Base64 Encoding and Decoding Example in Java 8 and before , Decoder to decode a base64 encoded byte array or String in Java 8. The JDK 8 API also provides different types of Base64 encoder e.g. basic, URL and MIME to support It's also faster than Sun and Apache's encoder for most of the input. The implementation of this class supports the following types of Base64 as specified in RFC 4648 and RFC 2045. Basic. Uses "The Base64 Alphabet" as specified in Table 1 of RFC 4648 and RFC 2045 for encoding and decoding operation. The encoder does not add any line feed (line separator) character.


Java Web Services: Up and Running: Up and Running, Before considering MTOM, let's take a quick look at base64 encoding of binary data, which might A service that provides images as responses import java.io. Base64 is a C library to convert from plain to base64 and vice versa suitable for embedded systems. base64-encoding base64-decoding Updated Jun 10, 2020