🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
The Base64 class in Java, part of the java.util package, provides methods for encoding and decoding data using the Base64 encoding scheme, including URL encoding.
Table of Contents
- What is the
Base64Class? - Common Methods
- Real-World Use Cases
- Examples of Using the
Base64Class - Conclusion
1. What is the Base64 Class?
The Base64 class provides static methods for Base64 encoding and decoding, allowing you to encode binary data into a text format and decode it back, including support for URL-safe encoding.
2. Common Methods
Base64.getEncoder(): Returns a Base64 encoder.Base64.getDecoder(): Returns a Base64 decoder.Base64.getUrlEncoder(): Returns a URL-safe Base64 encoder.Base64.getUrlDecoder(): Returns a URL-safe Base64 decoder.encodeToString(byte[] src): Encodes the specified byte array into a Base64 encoded string.decode(String src): Decodes the specified Base64 encoded string.
3. Real-World Use Cases
- Data Transmission: Encoding binary data into a text format for safe transmission over text-based protocols like HTTP.
- URL Encoding: Encoding URLs or parameters to ensure they are safe for inclusion in web addresses.
- Storing Binary Data: Storing binary data in text files or databases where text format is required.
4. Examples of Using the Base64 Class
Example 1: Encoding a URL
This example demonstrates how to encode a URL to Base64.
import java.util.Base64;
public class Base64UrlEncodeExample {
public static void main(String[] args) {
String originalUrl = "https://www.example.com/?query=hello world";
String encodedUrl = Base64.getUrlEncoder().encodeToString(originalUrl.getBytes());
System.out.println("Encoded URL: " + encodedUrl);
}
}
Output:
Encoded URL: aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vP3F1ZXJ5PWhlbGxvIHdvcmxk
Example 2: Decoding a Base64 Encoded URL
This example shows how to decode a Base64 encoded URL.
import java.util.Base64;
public class Base64UrlDecodeExample {
public static void main(String[] args) {
String encodedUrl = "aHR0cHM6Ly93d3cuZXhhbXBsZS5jb20vP3F1ZXJ5PWhlbGxvIHdvcmxk";
byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedUrl);
String decodedUrl = new String(decodedBytes);
System.out.println("Decoded URL: " + decodedUrl);
}
}
Output:
Decoded URL: https://www.example.com/?query=hello world
Example 3: Encoding Data for Transmission
This example demonstrates encoding data for safe transmission.
import java.util.Base64;
public class DataTransmissionExample {
public static void main(String[] args) {
byte[] data = {1, 2, 3, 4, 5};
String encodedData = Base64.getUrlEncoder().encodeToString(data);
System.out.println("Encoded data: " + encodedData);
}
}
Output:
Encoded data: AQIDBAU=
Example 4: Decoding Transmitted Data
This example shows how to decode transmitted data.
import java.util.Base64;
import java.util.Arrays;
public class DecodeTransmissionExample {
public static void main(String[] args) {
String encodedData = "AQIDBAU";
byte[] decodedBytes = Base64.getUrlDecoder().decode(encodedData);
System.out.println("Decoded data: " + Arrays.toString(decodedBytes));
}
}
Output:
Decoded data: [1, 2, 3, 4, 5]
5. Conclusion
The Base64 class in Java provides convenient methods for encoding and decoding data in the Base64 format, including URL-safe encoding, making it useful for transmitting binary data over text-based protocols and ensuring URLs are safely encoded.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment