Java Tutorial
- Java - Home
- Java - Overview
- Java - History
- Java - Features
- Java vs C++
- Java Virtual Machine (JVM)
- Java - JDK vs JRE vs JVM
- Java - Hello World Program
- Java - Environment Setup
- Java - Basic Syntax
- Java - Variable Types
- Java - Data Types
- Java - Type Casting
- Java - Unicode System
- Java - Basic Operators
- Java - Comments
Java Control Statements
- Java - Loop Control
- Java - Decision Making
- Java - If-else
- Java - Switch
- Java - For Loops
- Java - For-Each Loops
- Java - While Loops
- Java - do-while Loops
- Java - Break
- Java - Continue
Object Oriented Programming
- Java - OOPs Concepts
- Java - Object & Classes
- Java - Class Attributes
- Java - Class Methods
- Java - Methods
- Java - Variables Scope
- Java - Constructors
- Java - Access Modifiers
- Java - Inheritance
- Java - Aggregation
- Java - Polymorphism
- Java - Overriding
- Java - Method Overloading
- Java - Dynamic Binding
- Java - Static Binding
- Java - Instance Initializer Block
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java - Inner Classes
- Java - Static Class
- Java - Anonymous Class
- Java - Singleton Class
- Java - Wrapper Classes
- Java - Enums
- Java - Enum Constructor
- Java - Enum Strings
Java Built-in Classes
Java File Handling
- Java - Files
- Java - Create a File
- Java - Write to File
- Java - Read Files
- Java - Delete Files
- Java - Directories
- Java - I/O Streams
Java Error & Exceptions
- Java - Exceptions
- Java - try-catch Block
- Java - try-with-resources
- Java - Multi-catch Block
- Java - Nested try Block
- Java - Finally Block
- Java - throw Exception
- Java - Exception Propagation
- Java - Built-in Exceptions
- Java - Custom Exception
Java Multithreading
- Java - Multithreading
- Java - Thread Life Cycle
- Java - Creating a Thread
- Java - Starting a Thread
- Java - Joining Threads
- Java - Naming Thread
- Java - Thread Scheduler
- Java - Thread Pools
- Java - Main Thread
- Java - Thread Priority
- Java - Daemon Threads
- Java - Thread Group
- Java - Shutdown Hook
Java Synchronization
- Java - Synchronization
- Java - Block Synchronization
- Java - Static Synchronization
- Java - Inter-thread Communication
- Java - Thread Deadlock
- Java - Interrupting a Thread
- Java - Thread Control
- Java - Reentrant Monitor
Java Networking
- Java - Networking
- Java - Socket Programming
- Java - URL Processing
- Java - URL Class
- Java - URLConnection Class
- Java - HttpURLConnection Class
- Java - Socket Class
- Java - Generics
Java Collections
Java Interfaces
- Java - List Interface
- Java - Queue Interface
- Java - Map Interface
- Java - SortedMap Interface
- Java - Set Interface
- Java - SortedSet Interface
Java Data Structures
Java Collections Algorithms
Advanced Java
- Java - Command-Line Arguments
- Java - Lambda Expressions
- Java - Sending Email
- Java - Applet Basics
- Java - Javadoc Comments
- Java - Autoboxing and Unboxing
- Java - File Mismatch Method
- Java - REPL (JShell)
- Java - Multi-Release Jar Files
- Java - Private Interface Methods
- Java - Inner Class Diamond Operator
- Java - Multiresolution Image API
- Java - Collection Factory Methods
- Java - Module System
- Java - Nashorn JavaScript
- Java - Optional Class
- Java - Method References
- Java - Functional Interfaces
- Java - Default Methods
- Java - Base64 Encode Decode
- Java - Switch Expressions
- Java - Teeing Collectors
- Java - Microbenchmark
- Java - Text Blocks
- Java - Dynamic CDS archive
- Java - Z Garbage Collector (ZGC)
- Java - Null Pointer Exception
- Java - Packaging Tools
- Java - Sealed Classes
- Java - Record Classes
- Java - Hidden Classes
- Java - Pattern Matching
- Java - Compact Number Formatting
- Java - Garbage Collection
- Java - JIT Compiler
Java Miscellaneous
- Java - Recursion
- Java - Regular Expressions
- Java - Serialization
- Java - Strings
- Java - Process API Improvements
- Java - Stream API Improvements
- Java - Enhanced @Deprecated Annotation
- Java - CompletableFuture API Improvements
- Java - Streams
- Java - Datetime Api
- Java 8 - New Features
- Java 9 - New Features
- Java 10 - New Features
- Java 11 - New Features
- Java 12 - New Features
- Java 13 - New Features
- Java 14 - New Features
- Java 15 - New Features
- Java 16 - New Features
Java APIs & Frameworks
Java Class References
- Java - Scanner Class
- Java - Arrays Class
- Java - Strings
- Java - Date & Time
- Java - ArrayList
- Java - Vector Class
- Java - Stack Class
- Java - PriorityQueue
- Java - LinkedList
- Java - ArrayDeque
- Java - HashMap
- Java - LinkedHashMap
- Java - WeakHashMap
- Java - EnumMap
- Java - TreeMap
- Java - The IdentityHashMap Class
- Java - HashSet
- Java - EnumSet
- Java - LinkedHashSet
- Java - TreeSet
- Java - BitSet Class
- Java - Dictionary
- Java - Hashtable
- Java - Properties
- Java - Collection Interface
- Java - Array Methods
Java Useful Resources
Java - Base64 Encoding and Decoding
The Base64 utility class was introduced in Java 8 that has inbuilt encoder and decoder for Base64 encoding and decoding. We've three types of Base64 encoding and decoding available. Following is a brief of these types added:
Basic − Output is mapped to a set of characters lying in A-Za-z0-9+/. The encoder does not add any line feed in output, and the decoder rejects any character other than A-Za-z0-9+/.
URL − Output is mapped to set of characters lying in A-Za-z0-9+_. Output is URL and filename safe.
MIME − Output is mapped to MIME friendly format. Output is represented in lines of no more than 76 characters each, and uses a carriage return '\r' followed by a linefeed '\n' as the line separator. No line separator is present to the end of the encoded output.
Basic Base64 Encoding and Decoding
Basic Base64 encoder encodes the provided string without adding any line feed. This encoder uses characters lying in A-Za-z0-9+/ character set. Following is the snippet explaining the use of Base64 encoder.
String stringToEncode = "TutorialsPoint?java8"; // Encode using basic encoder String base64encodedString = Base64.getEncoder().encodeToString(stringToEncode.getBytes("utf-8"));
Simple Base64 decoder decodes the Base64 encoded string by rejecting any character other than A-Za-z0-9+/. Following is the snippet explaining the use of Base64 decoder.
// Decode the base64 encoded string using basic decoder byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString); // print the decoded string System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
Example: Basic Base64 Encoding and Decoding in Java
Following example showcases the use of Base64 basic encoder and decoder. We've first encoded a simple string using a encoder retrieved using Base64.getEncoder() and then returned encoded string is decoded using decoder retrieved using Base64.getDecoder() method.
package com.tutorialspoint; import java.io.UnsupportedEncodingException; import java.util.Base64; public class Base64Tester { public static void main(String[] args) throws UnsupportedEncodingException { String stringToEncode = "TutorialsPoint?java"; // Encode using basic encoder String base64encodedString = Base64.getEncoder().encodeToString(stringToEncode.getBytes("utf-8")); System.out.println("Encoded String: " + base64encodedString); // Decode the base64 encoded string using basic decoder byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString); // print the decoded string System.out.println("Decoded String: " + new String(base64decodedBytes, "utf-8")); } }
Output
Let us compile and run the above program, this will produce the following result −
Encoded String: VHV0b3JpYWxzUG9pbnQ/amF2YQ== Decoded String: TutorialsPoint?java
Base64 Encoding and Decoding for URL
URL Base64 encoder encodes the provided URL and makes it URL and filename safe. This encoder uses characters lying in A-Za-z0-9+/ character set. It encodes using the URL and Filename safe type base64 encoding scheme. Following is the snippet explaining the use of URL Base64 encoder.
String stringToEncode = "TutorialsPoint?java8"; // Encode using url encoder String base64encodedString = Base64.getUrlEncoder().encodeToString(stringToEncode.getBytes("utf-8"));
URL Base64 decoder decodes the Base64 encoded string by rejecting any character other than A-Za-z0-9+/. It decodes using the URL and Filename safe type base64 encoding scheme. Following is the snippet explaining the use of Base64 decoder.
// Decode the base64 encoded string using basic decoder byte[] base64decodedBytes = Base64.getUrlDecoder().decode(base64encodedString); // print the decoded string System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
Example: Base64 Encoding and Decoding for URL in Java
Following example showcases the use of Base64 URL encoder and decoder. We've first encoded a simple string using a encoder retrieved using Base64.getUrlEncoder() and then returned encoded string is decoded using decoder retrieved using Base64.getUrlDecoder() method.
package com.tutorialspoint; import java.io.UnsupportedEncodingException; import java.util.Base64; public class Base64Tester { public static void main(String[] args) throws UnsupportedEncodingException { String stringToEncode = "TutorialsPoint?java"; // Encode using url encoder String base64encodedString = Base64.getUrlEncoder().encodeToString(stringToEncode.getBytes("utf-8")); System.out.println("Encoded String: " + base64encodedString); // Decode the base64 encoded string using url decoder byte[] base64decodedBytes = Base64.getUrlDecoder().decode(base64encodedString); // print the decoded string System.out.println("Decoded String: " + new String(base64decodedBytes, "utf-8")); } }
Output
Let us compile and run the above program, this will produce the following result −
Encoded String: VHV0b3JpYWxzUG9pbnQ_amF2YQ== Decoded String: TutorialsPoint?java
Base64 Encoding and Decoding for MIME Type Content
MIME Base64 encoder encodes the provided string content to MIME friendly format. Output is represented in lines of no more than 76 characters each, and uses a carriage return '\r' followed by a linefeed '\n' as the line separator. No line separator is present to the end of the encoded output. Following is the snippet explaining the use of MIME Base64 encoder.
String stringToEncode = "TutorialsPoint?java8"; // Encode using mime encoder String base64encodedString = Base64.getMimeEncoder().encodeToString(stringToEncode.getBytes("utf-8"));
MIME Base64 decoder decodes the Base64 encoded string by rejecting any character other than A-Za-z0-9+/. It decodes using the MIME type base64 decoding scheme. Following is the snippet explaining the use of Base64 decoder.
// Decode the base64 encoded string using basic decoder byte[] base64decodedBytes = Base64.getMIMEDecoder().decode(base64encodedString); // print the decoded string System.out.println("Original String: " + new String(base64decodedBytes, "utf-8"));
Example: MIME Base64 Encoding and Decoding in Java
Following example showcases the use of Base64 MIME encoder and decoder. We've first encoded a simple string using a encoder retrieved using Base64.getMIMEEncoder() and then returned encoded string is decoded using decoder retrieved using Base64.getMIMEDecoder() method.
package com.tutorialspoint; import java.io.UnsupportedEncodingException; import java.util.Base64; import java.util.UUID; public class Base64Tester { public static void main(String[] args) throws UnsupportedEncodingException { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < 10; ++i) { stringBuilder.append(UUID.randomUUID().toString()); stringBuilder.append(","); } byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8"); String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes); System.out.println("Base64 Encoded String (MIME) : " + mimeEncodedString); // Decode the base64 encoded string using url decoder byte[] base64decodedBytes = Base64.getMimeDecoder().decode(mimeEncodedString); // print the decoded string System.out.println("Decoded String: " + new String(base64decodedBytes, "utf-8")); } }
Output
Let us compile and run the above program, this will produce the following result −
Base64 Encoded String (MIME) : NzRmNjkyODktYzJjZS00ZmU2LWEzYTUtMmFlMWRlMDQ1ZjU4LGQyNGQzMTU5LTVmOGUtNDZhMS04 NGRkLTBiMzNlNzc4ZjNiOCw2MmM1OTEzOS1kNmQwLTQ5MmQtYmUyMi01NmEzMTk5NmRkMTAsZDZh NjBlNzctZjRjZi00Y2Q4LTk5MWEtYTY2ZDEzMzU4YjFjLGFlNDhkZmZjLTEwZjctNDk5OS05NTFj LTU5ZGY1MjcyYjczNywxY2JiZjU0Ni0zNjc1LTQ4NzAtYTYxNC01MzkyODFkNjRjYmMsMTlhNTNi ODEtODQ0OS00M2MyLTg4NmMtNDhmZThmZDZmN2E1LDQyNmRhZDE0LTEyNjItNGJhZC1hMWJlLTNm ODc4MWE1YzhiMiw2NjEwMTgzZS03MGNkLTQzZTctOTRkNC0wZDgzZmY1MzhkNWYsOWMxNmMwM2Ut ZWZmZS00Zjg2LWFkYzgtNjc3MThjYTVlYjI2LA== Decoded String: 74f69289-c2ce-4fe6-a3a5-2ae1de045f58,d24d3159-5f8e-46a1-84dd-0b33e778f3b8,62c59139-d6d0-492d-be22-56a31996dd10,d6a60e77-f4cf-4cd8-991a-a66d13358b1c,ae48dffc-10f7-4999-951c-59df5272b737,1cbbf546-3675-4870-a614-539281d64cbc,19a53b81-8449-43c2-886c-48fe8fd6f7a5,426dad14-1262-4bad-a1be-3f8781a5c8b2,6610183e-70cd-43e7-94d4-0d83ff538d5f,9c16c03e-effe-4f86-adc8-67718ca5eb26,
Nested Classes of Base64 Class
Following classes are provided by Base64 class.
Sr.No. | Nested class & Description |
---|---|
1 | static class Base64.Decoder This class implements a decoder for decoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. |
2 | static class Base64.Encoder This class implements an encoder for encoding byte data using the Base64 encoding scheme as specified in RFC 4648 and RFC 2045. |
Base64 Class Methods
Following are the methods which provided by Base64 class to assist Base64 encoding/decoding.
Sr.No. | Method Name & Description |
---|---|
1 | static Base64.Decoder getDecoder() Returns a Base64.Decoder that decodes using the Basic type base64 encoding scheme. |
2 | static Base64.Encoder getEncoder() Returns a Base64.Encoder that encodes using the Basic type base64 encoding scheme. |
3 | static Base64.Decoder getMimeDecoder() Returns a Base64.Decoder that decodes using the MIME type base64 decoding scheme. |
4 | static Base64.Encoder getMimeEncoder() Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme. |
5 | static Base64.Encoder getMimeEncoder(int lineLength, byte[] lineSeparator) Returns a Base64.Encoder that encodes using the MIME type base64 encoding scheme with specified line length and line separators. |
6 | static Base64.Decoder getUrlDecoder() Returns a Base64.Decoder that decodes using the URL and Filename safe type base64 encoding scheme. |
7 | static Base64.Encoder getUrlEncoder() Returns a Base64.Encoder that encodes using the URL and Filename safe type base64 encoding scheme. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object