Skip to content

Java Base64

Java Base64 is like the secret language of computers, allowing us to take any kind of data - from text to images and everything in between - and encode it into a series of characters that can be easily shared and understood by different systems. It's like putting a message into a special code that only those who know the secret can decipher!

Think of it as a digital translator, converting our data into a universal format that can travel across the web without getting lost in translation. Whether you're sending files over email, storing sensitive information in a database, or even just saving space in your code, Java Base64 has got your back.

But wait, there's more! Not only can Java Base64 encode our data, but it can also decode it back to its original form, ensuring that our precious information remains intact throughout its journey. It's like having your own personal decoder ring for the digital age!

Basic Encoding

java
import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        // Data to be encoded
        String originalData = "Hello, Base64!";

        // Encode the data
        String encodedData = Base64.getEncoder().encodeToString(originalData.getBytes());

        // Print the encoded data
        System.out.println("Encoded data: " + encodedData);
    }
}

In our example, we're using Java's Base64 class to encode the string "Hello, Base64!". We convert the string to bytes, encode it using the Base64.getEncoder() method, and then print out the resulting encoded data.

Basic Decoding

But wait, there's more! Base64 isn't just for encoding; it can also decode data back to its original form. Let's see how:

java
import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        // Encoded data to be decoded
        String encodedData = "SGVsbG8sIEJhc2U2NCENCg==";

        // Decode the data
        byte[] decodedBytes = Base64.getDecoder().decode(encodedData);
        String decodedData = new String(decodedBytes);

        // Print the decoded data
        System.out.println("Decoded data: " + decodedData);
    }
}

In this snippet, we're decoding the Base64-encoded string "SGVsbG8sIEJhc2U2NCENCg==". We use the Base64.getDecoder() method to get a decoder, decode the encoded data, and then convert the resulting bytes back into a string.

And just like that, we've unlocked the secrets of Base64 encoding and decoding in Java! With its ability to represent binary data in a safe and efficient manner, Base64 is a valuable tool in your programming arsenal.

Base64 URL Encoding

java
import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        String originalText = "Hello, world!";
        byte[] originalBytes = originalText.getBytes();
        
        String encodedString = Base64.getUrlEncoder().encodeToString(originalBytes);
        System.out.println("Encoded string: " + encodedString);
    }
}

In this example, we start with a simple text message, "Hello, world!". We convert it into a byte array and then use the Base64.getUrlEncoder() method to obtain a Base64 encoder. Finally, we encode the byte array into a Base64-encoded string using encodeToString().

Base64 URL Decoding

java
import java.util.Base64;

public class Base64Example {
    public static void main(String[] args) {
        String encodedString = "SGVsbG8sIHdvcmxkIQ%3D%3D";
        
        byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedString);
        String decodedString = new String(decodedBytes);
        
        System.out.println("Decoded string: " + decodedString);
    }
}

In this example, we start with a Base64-encoded string, "SGVsbG8sIHdvcmxkIQ%3D%3D". We use the Base64.getUrlDecoder() method to obtain a Base64 decoder and then decode the string back into its original byte array. Finally, we convert the byte array back into a string to reveal the decoded message.

MIME Type encoding and decoding

Before we go into encoding and decoding, let's talk about MIME types. MIME (Multipurpose Internet Mail Extensions) types are a standard way of indicating the type and format of content being transmitted over the internet. They help browsers and servers understand how to handle different types of data, whether it's plain text, HTML, images, or other multimedia content.

Encoding a MIME Type

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

public class MimeTypeEncodingExample {
    public static void main(String[] args) {
        String mimeType = "text/plain";
        
        // Encode MIME type using Base64
        byte[] encodedBytes = Base64.getEncoder().encode(mimeType.getBytes(StandardCharsets.UTF_8));
        String encodedMimeType = new String(encodedBytes);
        
        System.out.println("Encoded MIME type: " + encodedMimeType);
    }
}
  1. We import the necessary classes: StandardCharsets for UTF-8 encoding and Base64 for encoding.
  2. We define the MIME type we want to encode, in this case, "text/plain".
  3. We use Base64.getEncoder().encode() to encode the MIME type into a byte array using Base64 encoding.
  4. We create a new String from the encoded byte array to obtain the encoded MIME type.
  5. Finally, we print the encoded MIME type.

Decoding an Encoded MIME Type

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

public class MimeTypeDecodingExample {
    public static void main(String[] args) {
        String encodedMimeType = "dGV4dC9wbGFpbg==";
        
        // Decode MIME type using Base64
        byte[] decodedBytes = Base64.getDecoder().decode(encodedMimeType.getBytes(StandardCharsets.UTF_8));
        String decodedMimeType = new String(decodedBytes);
        
        System.out.println("Decoded MIME type: " + decodedMimeType);
    }
}
  1. We import the necessary classes: StandardCharsets for UTF-8 encoding and Base64 for decoding.
  2. We have the encoded MIME type, "dGV4dC9wbGFpbg==", which was obtained from the previous encoding example.
  3. We use Base64.getDecoder().decode() to decode the Base64-encoded MIME type back into a byte array.
  4. We create a new String from the decoded byte array to obtain the original MIME type.
  5. Finally, we print the decoded MIME type.

Encoding and decoding MIME types using Base64 encoding ensures that they can be safely transmitted over the internet without losing any information. It's a commonly used technique for encoding binary data into a text-based format that is easily transferable.

Waytojava is designed to make learning easier. We simplify examples for better understanding. We regularly check tutorials, references, and examples to correct errors, but it's important to remember that humans can make mistakes.