search
HomeJavajavaTutorialHow to implement base64 encoder in Java

How to implement base64 encoder in Java

Apr 28, 2023 pm 04:04 PM
javabase64

Introduction

What is Base64 encoding? Before answering this question, we need to understand the classification of files in computers. For computers, files can be divided into two categories, one is text files and the other is binary files.

For binary files, their content is represented in binary, which is not immediately understandable to humans. If you try to open a binary file with a text editor, you may see gibberish. This is because the encoding method of binary files is different from the encoding method of text files, so when the text editor tries to translate the binary files into text content, garbled characters will appear.

For text files, there are many encoding methods, such as the earliest ASCII encoding and the currently commonly used encoding methods such as UTF-8 and UTF-16. Even text files may see garbled characters if you open them using a different encoding.

So whether it is a text file or a binary file, the encoding format needs to be unified. In other words, what the encoding of writing looks like, then the encoding of data reading should also match it.

Base64 encoding is actually an encoding method that encodes binary data into visual ASCII characters.

Why is there such a requirement?

We know that the development of the computer world does not happen overnight. It is a process of slow growth. For character encoding, it only supports ASCII encoding at first, and later it was expanded to Unicode and so on. Therefore, for many applications, encoding formats other than ASCII encoding are not supported. So how to display non-ASCII code in these systems?

The solution is to perform encoding mapping to map non-ASCII characters to ASCII characters. Base64 is such an encoding method.

The common place to use Base64 is in web pages. Sometimes we need to display images on web pages, so we can base64 encode the images and then fill them into html.

Another application is to base64 encode the file and then send it as an email attachment.

JAVA’s support for base64

Since base64 encoding is so easy to use, let’s take a look at the base64 implementation in JAVA.

There is a corresponding base64 implementation in java, called java.util.Base64. This class is a tool class for Base64, which was introduced by JDK in version 1.8.

Base64 provides three getEncoder and getDecoder methods. By obtaining the corresponding Encoder and Decoder, you can then call the encoder's encode and decode methods to encode and decode the data, which is very convenient.

Let’s first take a look at the basic usage examples of Base64:

 // 使用encoder进行编码
 String encodedString = Base64.getEncoder().encodeToString("what is your name baby?".getBytes("utf-8"));
 System.out.println("Base64编码过后的字符串 :" + encodedString);

 // 使用encoder进行解码
 byte[] decodedBytes = Base64.getDecoder().decode(encodedString);

 System.out.println("解码过后的字符串: " + new String(decodedBytes, "utf-8"));

As a tool class, the Base64 tool class provided in the JDK is still very useful.

I won’t explain its use in detail here. This article mainly analyzes how Base64 is implemented in JDK.

Classification and implementation of Base64 in JDK

The Base64 class in JDK provides three encoder methods, namely getEncoder, getUrlEncoder and getMimeEncoder:

    public static Encoder getEncoder() {
         return Encoder.RFC4648;
    }

    public static Encoder getUrlEncoder() {
         return Encoder.RFC4648_URLSAFE;
    }

    public static Encoder getMimeEncoder() {
        return Encoder.RFC2045;
    }

Similarly, it Three corresponding decoder are also provided, namely getDecoder, getUrlDecoder, getMimeDecoder:

    public static Decoder getDecoder() {
         return Decoder.RFC4648;
    }

    public static Decoder getUrlDecoder() {
         return Decoder.RFC4648_URLSAFE;
    }

    public static Decoder getMimeDecoder() {
         return Decoder.RFC2045;
    }

As can be seen from the code, these three encodings correspond to RFC4648, RFC4648_URLSAFE and RFC2045 respectively.

These three are variants of base64 encoding. Let’s take a look at their differences:

##RFC 4648: base64url (URL- and filename-safe standard)
Encoding name Encoded characters Encoded characters Encoded characters
The 62nd digit The 63rd digit Complete character
RFC 2045: Base64 transfer encoding for MIME / = mandatory
RFC 4648: base64 (standard) / = optional
- _ = optional
You can see that the difference between base64 and Base64url is that the 62nd and 63rd encoded characters are different, and the difference between base64 for MIME and base64 is whether the completion character is mandatory.

In addition, for Basic and base64url, line separator characters will not be added, while base64 for MIME will add '\r' and '\n' as line separators after a line exceeds 76 characters.

Finally, if during the decoding process, characters that are not found in the Base64 mapping table are processed differently, base64 and Base64url will be rejected directly, while base64 for MIME will be ignored.

The difference between base64 and Base64url can be seen through the following two methods:

        private static final char[] toBase64 = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
        };
        private static final char[] toBase64URL = {
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', '_'
        };

For MIME, the maximum number of characters in a line and the newline character are defined:

        private static final int MIMELINEMAX = 76;
        private static final byte[] CRLF = new byte[] {'\r', '\n'};

Advanced usage of Base64

Generally, the length of the object we encode with Base64 is fixed. We only need to convert the input object into a byte array to call the encode or decode method.

But in some cases we need to convert stream data. At this time, we can use the two methods of wrapping Stream provided in Base64:

        public OutputStream wrap(OutputStream os) {
            Objects.requireNonNull(os);
            return new EncOutputStream(os, isURL ? toBase64URL : toBase64,
                                       newline, linemax, doPadding);
        }
        public InputStream wrap(InputStream is) {
            Objects.requireNonNull(is);
            return new DecInputStream(is, isURL ? fromBase64URL : fromBase64, isMIME);
        }

These two methods are respectively Corresponds to encoder and decoder.

The above is the detailed content of How to implement base64 encoder in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
JVM performance vs other languagesJVM performance vs other languagesMay 14, 2025 am 12:16 AM

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

Java Platform Independence: Examples of useJava Platform Independence: Examples of useMay 14, 2025 am 12:14 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

JVM Architecture: A Deep Dive into the Java Virtual MachineJVM Architecture: A Deep Dive into the Java Virtual MachineMay 14, 2025 am 12:12 AM

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVM: Is JVM related to the OS?JVM: Is JVM related to the OS?May 14, 2025 am 12:11 AM

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceJava: Write Once, Run Anywhere (WORA) - A Deep Dive into Platform IndependenceMay 14, 2025 am 12:05 AM

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

Java Platform Independence: Compatibility with different OSJava Platform Independence: Compatibility with different OSMay 13, 2025 am 12:11 AM

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

What features make java still powerfulWhat features make java still powerfulMay 13, 2025 am 12:05 AM

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

Top Java Features: A Comprehensive Guide for DevelopersTop Java Features: A Comprehensive Guide for DevelopersMay 13, 2025 am 12:04 AM

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.