Java Programming Tutorials

Java programming tutorials with many code examples!

Java Base64 MIME

Problem:

How to encode and decode Base64 MIME message in Java? In the following example we’ll show how to use Base64 util class available in Java 8 to do the work.

Solution:

In the previous post we’ve shown how to use java.util.Base64 class to encode arrays of bytes. In this post we’re going to show how to encode messages as MIME types, which can be used to send emails from programs.

Here we use Base64.getMimeEncoder() to create a MIME encoder and Base64.getMimeDecoder() for decoder:

package com.farenda.java.util;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class Base64MIMEEncoder {
    private static String MESSAGE
            = "Java SE 8 represents the single largest evolution"
            + " of the Java language in its history. A relatively"
            + " small number of features - lambda expressions, method"
            + " references, and functional interfaces - combine to offer"
            + " a programming model that fuses the object-oriented and"
            + " functional styles. Under the leadership of Brian Goetz,"
            + " this fusion has been accomplished in a way that encourages"
            + " best practices - immutability, statelessness,"
            + " compositionality - while preserving \"the feel of Java\""
            + " - readability, simplicity, universality.\n";

    public static void main(String[] args) {
        System.out.println("Original:\n" + MESSAGE);

        byte[] bytesToEncode = MESSAGE.getBytes(StandardCharsets.UTF_8);

        byte[] encoded = encode(bytesToEncode);

        decode(encoded);
    }

    private static byte[] encode(byte[] bytesToEncode) {
        Base64.Encoder encoder = Base64.getMimeEncoder();
        byte[] encoded = encoder.encode(bytesToEncode);
        System.out.println("Encoded:\n" + new String(encoded));
        return encoded;
    }

    private static void decode(byte[] encoded) {
        Base64.Decoder decoder = Base64.getMimeDecoder();
        byte[] decoded = decoder.decode(encoded);
        System.out.println("Decoded:\n"
                + new String(decoded, StandardCharsets.UTF_8));
    }
}

Here’s the result of running the above code:

Original:
Java SE 8 represents the single largest evolution of the Java language in its history. A relatively small number of features - lambda expressions, method references, and functional interfaces - combine to offer a programming model that fuses the object-oriented and functional styles. Under the leadership of Brian Goetz, this fusion has been accomplished in a way that encourages best practices - immutability, statelessness, compositionality - while preserving "the feel of Java" - readability, simplicity, universality.

Encoded:
SmF2YSBTRSA4IHJlcHJlc2VudHMgdGhlIHNpbmdsZSBsYXJnZXN0IGV2b2x1dGlvbiBvZiB0aGUg
SmF2YSBsYW5ndWFnZSBpbiBpdHMgaGlzdG9yeS4gQSByZWxhdGl2ZWx5IHNtYWxsIG51bWJlciBv
ZiBmZWF0dXJlcyAtIGxhbWJkYSBleHByZXNzaW9ucywgbWV0aG9kIHJlZmVyZW5jZXMsIGFuZCBm
dW5jdGlvbmFsIGludGVyZmFjZXMgLSBjb21iaW5lIHRvIG9mZmVyIGEgcHJvZ3JhbW1pbmcgbW9k
ZWwgdGhhdCBmdXNlcyB0aGUgb2JqZWN0LW9yaWVudGVkIGFuZCBmdW5jdGlvbmFsIHN0eWxlcy4g
VW5kZXIgdGhlIGxlYWRlcnNoaXAgb2YgQnJpYW4gR29ldHosIHRoaXMgZnVzaW9uIGhhcyBiZWVu
IGFjY29tcGxpc2hlZCBpbiBhIHdheSB0aGF0IGVuY291cmFnZXMgYmVzdCBwcmFjdGljZXMgLSBp
bW11dGFiaWxpdHksIHN0YXRlbGVzc25lc3MsIGNvbXBvc2l0aW9uYWxpdHkgLSB3aGlsZSBwcmVz
ZXJ2aW5nICJ0aGUgZmVlbCBvZiBKYXZhIiAtIHJlYWRhYmlsaXR5LCBzaW1wbGljaXR5LCB1bml2
ZXJzYWxpdHkuCg==
Decoded:
Java SE 8 represents the single largest evolution of the Java language in its history. A relatively small number of features - lambda expressions, method references, and functional interfaces - combine to offer a programming model that fuses the object-oriented and functional styles. Under the leadership of Brian Goetz, this fusion has been accomplished in a way that encourages best practices - immutability, statelessness, compositionality - while preserving "the feel of Java" - readability, simplicity, universality.

As you can see it correctly uses lines no longer than 76 characters followed by CRLF.

Share with the World!