Base64 in Java

Encode and decode Base64 in Java using java.util.Base64. Covers basic, URL-safe, and MIME encoders with practical code examples and best practices.

Language

Detailed Explanation

Java 8 introduced the java.util.Base64 class, which provides three encoder/decoder pairs for different use cases: Basic, URL-safe, and MIME. Before Java 8, developers relied on sun.misc.BASE64Encoder (not part of the public API) or third-party libraries like Apache Commons Codec.

Basic encoding and decoding:

import java.util.Base64;

// Encoding
String original = "Hello, World!";
String encoded = Base64.getEncoder().encodeToString(original.getBytes("UTF-8"));
// Result: "SGVsbG8sIFdvcmxkIQ=="

// Decoding
byte[] decodedBytes = Base64.getDecoder().decode(encoded);
String decoded = new String(decodedBytes, "UTF-8");
// Result: "Hello, World!"

The three encoder types:

  1. Basic (Base64.getEncoder()): Standard Base64 with no line separators. Output uses A-Z, a-z, 0-9, +, / and = for padding. Best for general-purpose encoding.

  2. URL-safe (Base64.getUrlEncoder()): Uses - instead of + and _ instead of /. No line separators. Ideal for encoding data that will appear in URLs or filenames.

  3. MIME (Base64.getMimeEncoder()): Inserts line separators (\r\n) every 76 characters, as required by MIME (RFC 2045). Used for email attachments and similar protocols.

// URL-safe encoding
String urlSafe = Base64.getUrlEncoder().encodeToString(data);

// MIME encoding with line breaks
String mimeEncoded = Base64.getMimeEncoder().encodeToString(largeData);

// Without padding (useful for JWTs)
String noPadding = Base64.getUrlEncoder().withoutPadding().encodeToString(data);

Streaming for large data:

// Wrap an OutputStream for encoding on-the-fly
OutputStream encodedStream = Base64.getEncoder().wrap(outputStream);
encodedStream.write(largeByteArray);
encodedStream.close();

The wrap() method is particularly useful for large files because it avoids loading the entire file into memory at once. It processes data as a stream, making it suitable for multi-gigabyte files.

Common mistake: Using String.getBytes() without specifying a charset. The default charset varies by platform (UTF-8 on Linux/macOS, Windows-1252 on older Windows), which can produce different Base64 output for the same string on different machines. Always specify "UTF-8" explicitly.

Use Case

Encoding binary payloads in a Spring Boot REST API that communicates with a legacy system requiring all data fields to be ASCII-safe strings within XML.

Try It — Base64 Encoder

Open full tool