Java Base64 Class

Introduction

The Base64 class in Java, part of the java.util package, provides methods for encoding and decoding data using the Base64 encoding scheme, including URL encoding.

Table of Contents

  1. What is the Base64 Class?
  2. Common Methods
  3. Real-World Use Cases
  4. Examples of Using the Base64 Class
  5. Conclusion

1. What is the Base64 Class?

The Base64 class provides static methods for Base64 encoding and decoding, allowing you to encode binary data into a text format and decode it back, including support for URL-safe encoding.

2. Common Methods

  • Base64.getEncoder(): Returns a Base64 encoder.
  • Base64.getDecoder(): Returns a Base64 decoder.
  • Base64.getUrlEncoder(): Returns a URL-safe Base64 encoder.
  • Base64.getUrlDecoder(): Returns a URL-safe Base64 decoder.
  • encodeToString(byte[] src): Encodes the specified byte array into a Base64 encoded string.
  • decode(String src): Decodes the specified Base64 encoded string.

3. Real-World Use Cases

  • Data Transmission: Encoding binary data into a text format for safe transmission over text-based protocols like HTTP.
  • URL Encoding: Encoding URLs or parameters to ensure they are safe for inclusion in web addresses.
  • Storing Binary Data: Storing binary data in text files or databases where text format is required.

4. Examples of Using the Base64 Class

Example 1: Encoding a URL

This example demonstrates how to encode a URL to Base64.

import java.util.Base64;

public class Base64UrlEncodeExample {
    public static void main(String[] args) {
        String originalUrl = "https://www.example.com/?query=hello world";
        String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
        System.out.println("Encoded URL: " + encodedUrl);
    }
}

Output:

Encoded URL: aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vP3F1ZXJ5PWhlbGxvIHdvcmxk

Example 2: Decoding a Base64 Encoded URL

This example shows how to decode a Base64 encoded URL.

import java.util.Base64;

public class Base64UrlDecodeExample {
    public static void main(String[] args) {
        String encodedUrl = "aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vP3F1ZXJ5PWhlbGxvIHdvcmxk";
        byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedUrl);
        String decodedUrl = new String(decodedBytes);
        System.out.println("Decoded URL: " + decodedUrl);
    }
}

Output:

Decoded URL: https://www.example.com/?query=hello world

Example 3: Encoding Data for Transmission

This example demonstrates encoding data for safe transmission.

import java.util.Base64;

public class DataTransmissionExample {
    public static void main(String[] args) {
        byte[] data = {1, 2, 3, 4, 5};
        String encodedData = Base64.getUrlEncoder().encodeToString(data);
        System.out.println("Encoded data: " + encodedData);
    }
}

Output:

Encoded data: AQIDBAU=

Example 4: Decoding Transmitted Data

This example shows how to decode transmitted data.

import java.util.Base64;
import java.util.Arrays;

public class DecodeTransmissionExample {
    public static void main(String[] args) {
        String encodedData = "AQIDBAU";
        byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedData);
        System.out.println("Decoded data: " + Arrays.toString(decodedBytes));
    }
}

Output:

Decoded data: [1, 2, 3, 4, 5]

5. Conclusion

The Base64 class in Java provides convenient methods for encoding and decoding data in the Base64 format, including URL-safe encoding, making it useful for transmitting binary data over text-based protocols and ensuring URLs are safely encoded.

Comments