In Java Base64 Decode, Base64 is an encoding scheme in a binary-to-text format that denotes binary data, which is in the form of printable ASCII string format by translating into radix 64 depictions. These Base64 data can be encoded or decoded based on the user’s requirements and can be done with the help of certain methods. For that, importing java.util.Base64 package is an essential step. The main advantage of encoding and decoding these data is its privacy as well as security. In the following sections, a detailed description of each method will be addressed.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Declaration:
Below is the declaration for Base64 decode:
public static class Base64.Decoder extends Object
Now, let us see the working of the Base64 decode.
Following are the different methods of Java Base64 decode.
Example:
Code:
import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); // Encode byte array byte arr[] = {'a'}; byte arr2[] = enc.encode(arr); System.out.println("Array encoded is: "+ arr2); // decoder Base64.Decoder dec = Base64.getDecoder(); // Decode byte array String ds = new String(dec.decode(arr2)); System.out.println("Array decoded is:"+ds); } }
Output:
An encoded and decoded result of an array gets printed on executing the code.
Example:
Code:
import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); String s = enc.encodeToString("EduCBA".getBytes()); System.out.println("String encoded is: "+ s); // decoder Base64.Decoder dec = Base64.getDecoder(); // Decode string String ds = new String(dec.decode(s)); System.out.println("String decoded is:"+ds); }}
Output:
An encoded and decoded result of a string gets printed on executing the code.
Example:
Code:
import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); byte arr[] = {'1'}; byte arr2[] = enc.encode(arr); byte arr3[] = new byte[5]; System.out.println("Array encoded is: "+ arr2); // decoder Base64.Decoder dec = Base64.getDecoder(); System.out.println("Array decoded is:"+ dec.decode(arr2,arr3)); }}
Output:
An encoded and decoded result of a byte array gets printed on executing the code.
Example:
Code:
import java.nio.ByteBuffer; import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) { // encoder Base64.Encoder enc = Base64.getEncoder(); String st = "Happy weekend"; ByteBuffer buff= ByteBuffer.wrap(st.getBytes()); ByteBuffer buff2 = enc.encode(buff); System.out.print("Encoded: "); while(buff2.hasRemaining()){ char ch = (char) buff2.get(); System.out.print(ch); } buff2.clear(); // decoder Base64.Decoder dec = Base64.getDecoder(); ByteBuffer buff3 = dec.decode(buff2); System.out.print(" Decoded: "); while(buff3.hasRemaining()){ char ch3 = (char) buff3.get(); System.out.print(ch3); } buff2.clear(); } }
Output:
An encoded and decoded result of a bytebuffergets printed on executing the code.
Example
Code:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Base64; public class Base64DecodeExample { public static void main(String[] args) throws IOException { try (InputStream inpt = new FileInputStream("F:\\EduCBA\\April\\Edu.txt")) { Base64.Encoder enc = Base64.getEncoder(); OutputStream opst = enc.wrap(new FileOutputStream("F:\\EduCBA\\April\\Eduout.txt")); int b1; while ((b1 = inpt.read()) != -1) { opst.write(b1); } opst.close(); } catch (IOException ie) { System.err.printf("I/O exception", ie.getMessage()); } try (FileOutputStream fopst = new FileOutputStream("F:\\EduCBA\\April\\Eduou.txt")) { Base64.Decoder dec = Base64.getDecoder(); InputStream inpt2 = dec.wrap(new FileInputStream("F:\\EduCBA\\April\\Eduout.txt")); int b1; while ((b1 = inpt2.read()) != -1) fopst.write(b1); inpt2.close(); } catch (IOException ie) { System.err.printf("I/O exception", ie.getMessage()); } } }
Output:
In this program, create three create text files, Edu, Eduout, Eduou, in a location for storing data, encoded data, and decoded data respectively. The below figure is the input data.
The encoded and decoded data will be written into the two other files on executing the code, as shown below.
The above is the detailed content of Java Base64 Decode. For more information, please follow other related articles on the PHP Chinese website!