Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java Base64 MIME

farenda 2015-09-16 0

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!
Categories Java Tags java, java-util
Previous: Java Base64
Next: Java Base64 URL

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored