Sitelet https://github.com/javaevolved/javaevolved.github.io/issues/177
Skip to content

[Pattern] Standard Base64 encoding and decoding #177

Description

@brunoborges

Category

security

Slug

standard-base64

Title

Standard Base64 encoding and decoding

Difficulty

beginner

Since JDK

8

Summary

Use java.util.Base64 instead of internal JDK classes or third-party codecs.

Old code label

Internal JDK API

Old code

String encoded = new sun.misc.BASE64Encoder()
        .encode(data);

Modern code label

Java 8+

Modern code

String encoded = Base64.getEncoder()
        .encodeToString(data);

Explanation

The standard Base64 API provides basic, URL-safe, and MIME variants without relying on unsupported internal packages.

Why the modern way wins

🛡 Supported API — Avoids inaccessible sun.misc implementation classes.
🧩 Complete variants — Includes basic, URL-safe, and MIME encoders.
🚀 No dependency — Encoding and decoding are built into the JDK.

Metadata

Metadata

Assignees

No one assigned

    Labels

    slugNew or updated pattern snippet (category/slug.json)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions