Java Base64 Decode
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
How does Base64 Decode Work in Java?
Now, let us see the working of the Base64 decode.
- First, create an encoder object and encode the string/ bytes/ bytebuffer based on the requirement. It can be done using the method Base64.getEncoder().
- Once it is done, create a new array for the encoded data.
- To decode the data, create a decoder object, and create another array for storing decoded data.
- Decode the encoded data using the method decode().
- Print the result available in the decoder array.
Methods of Java Base64 decode
Following are the different methods of Java Base64 decode.
1. public byte[] decode ( byte[] arr )
- Decodes each and every byte from the input array with the help of the Base64 encoding scheme.
- Results will be written into a newly allocated byte array.
- The byte array returned will be having a length similar to the resulting bytes.
- Here, arr is the input byte array that has to be decoded.
- An IllegalArgumentException will be thrown if the input byte array is not in a valid format of Base64.
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.
2. public byte[] decode ( Stringstr )
- Input string which is in the Base64 format, will be decoded with the help of the Base64 encoding scheme.
- Results will be written into a newly allocated byte array.
- Execution of this method will create an effect of executing the method decode( src.getBytes ( StandardCharsets.ISO_8859_1 ) ).
- Here, str is the input string that has to be decoded.
- An IllegalArgumentException will be thrown if the input string is not in a valid format of Base64.
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.
3. public int decode ( byte[] arr, byte[] arr2)
- Decodes each and every byte from the input array with the help of the Base64 encoding scheme.
- Results will be written into a byte array arr2 that starts the offset from 0.
- Here, arr is the input byte array that has to be decoded, and arr2 is the output array. Make sure that arr2 has enough space to accommodate the decoded input bytes.
- An IllegalArgumentException will be thrown if the input byte array is not in a valid format of Base64 or arr2 do not have enough space for writing the decoded input bytes.
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.
4. public ByteBufferdecode ( ByteBufferbuff )
- Decodes each and every byte from the input byte buffer with the help of the Base64 encoding scheme.
- Results will be written into a newly allocated bytebuffer.
- Here, the buff is the inputbytebuffer that has to be decoded.
- An IllegalArgumentException will be thrown if the input bytebufferis not in a valid format of Base64.
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.
5. public InputStreamwrap ( InputStreaminpt )
- An input stream will be returned in order to decode the byte stream, which is Base64 encoded.
- Here, input is the input stream.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
