Site icon

Base64 withoutPadding Encoding of string or byte array (Java8)

Problem Statement:

Base 64 Encoder class:

Important points mentioned about padding in RFC 4648:

Base64 Encode withoutPadding of byte or string – Java8

package org.learn;

import java.util.Base64;

public class Base64EncodingWithoutPadDemo {

    public static void main(String[] args) {
        String input = "I Love Soccer";
        byte[] bytesInput = input.getBytes();

        System.out.println("Input String : "+input);

        //Encode String to  Base64
        Base64.Encoder base64Encoder = Base64.getEncoder();
        String encodedString = base64Encoder.encodeToString(bytesInput);
        System.out.println("Encoded String withPadding :" + encodedString);

        //Encode String withoutPadding to Base64
        base64Encoder = Base64.getEncoder().withoutPadding();
        encodedString = base64Encoder.encodeToString(bytesInput);
        System.out.println("Encoded String withoutPadding: " + encodedString);
    }
}

O/P: Base64 encoding withoutPadding of byte array (Java8)

Input String : I Love Soccer
Encoded String withPadding :SSBMb3ZlIFNvY2Nlcg==
Encoded String withoutPadding: SSBMb3ZlIFNvY2Nlcg

Observations: withoutPadding encoding of byte array:

Exit mobile version