Base64 Encoding and Decoding in Java
• Java provides a class Base64 to deal with
encryption. You can encrypt and decrypt your data
by using provided methods.
• Base 64 is an encoding scheme that converts binary data
into text format so that encoded textual data can be easily
transported over network un-corrupted and without any
data loss.
• need to import [Link].Base64 in your source file
to use its methods.
• class provides three different encoders and
decoders to encrypt information at each level.
Basic Encoding and Decoding
• uses the Base64 alphabet specified by Java in RFC 4648 and RFC
2045 for encoding and decoding operations.
• Encoder does not add any line separator character.
• decoder rejects data that contains characters outside the base64
alphabet.
URL and Filename Encoding and
Decoding
• uses the Base64 alphabet specified by Java in RFC 4648 for
encoding and decoding operations.
Nested Classes of Base64
• [Link]: This class implements a decoder for decoding byte
data using the Base64 encoding scheme as specified in RFC 4648
and RFC 2045.
• [Link]: This class implements an encoder for encoding
byte data using the Base64 encoding scheme as specified in RFC
4648 and RFC 2045.
Base64 Methods
• public static [Link] getDecoder()
returns a [Link] that decodes using the Basic type base64
encoding scheme.
• public static [Link] getEncoder()
returns a [Link] that encodes using the Basic type base64
encoding scheme.
• public static [Link] getUrlDecoder()
It returns a [Link] that decodes using the URL and Filename
safe type base64 encoding scheme.
• public static [Link] getMimeDecoder()
returns a [Link] that decodes using the MIME (Multipurpose Internet
Mail Extensions) type base64 decoding scheme.
[Link] Methods
• public byte[] decode(byte[] src)
decodes all bytes from the input byte array using the Base64 encoding
scheme, writing the results into a newly-allocated output byte array.
The returned byte array is of the length of the resulting bytes.
• public byte[] decode(String src)
decodes a Base64 encoded String into a newly-allocated byte array
using the Base64 encoding scheme.
• public int decode(byte[] src, byte[] dst)
decodes all bytes from the input byte array using the Base64 encoding
scheme, writing the results into the given output byte array, starting at
offset 0.
• public ByteBuffer decode(ByteBuffer buffer)
decodes all bytes from the input byte buffer using the Base64 encoding scheme,
writing the results into a newly-allocated ByteBuffer.
• public InputStream wrap(InputStream is)
returns an input stream for decoding Base64 encoded byte stream.
[Link] Methods
• public byte[] encode(byte[] src)
encodes all bytes from the specified byte array into a newly-allocated
byte array using the Base64 encoding scheme. The returned byte array
is of the length of the resulting bytes
• public int encode(byte[] src, byte[] dst)
encodes all bytes from the specified byte array using the Base64 encoding
scheme, writing the resulting bytes to the given output byte array, starting at
offset 0.
• public String encodeToString(byte[] src)
encodes the specified byte array into a String using the Base64 encoding
scheme.
• public ByteBuffer encode(ByteBuffer buffer)
encodes all remaining bytes from the specified byte buffer into a newly-
allocated ByteBuffer using the Base64 encoding scheme.
• public OutputStream wrap(OutputStream os)
wraps an output stream for encoding byte data using the Base64
encoding scheme
• public [Link] withoutPadding()
returns an encoder instance that encodes equivalently to this one, but
without adding any padding character at the end of the encoded byte
data.
Java Base64 Example: Basic Encoding and
Decoding
// Java program to demonstrate
// Encoding simple String into Basic Base 64 format
import [Link].*;
public class GFG {
public static void main(String[] args){
// create a sample String to encode
String sample = "India Team will win the Cup";
// print actual String
[Link]("Sample String:\n“ + sample);
// Encode into Base64 format
String BasicBase64format =
[Link]().encodeToString([Link]());
// print encoded String
[Link]("Encoded String:\n"+ BasicBase64format);
}
}
• Output
• Sample String:
• India Team will win the Cup
• Encoded String:
• SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw
• Program 2: Decode Basic Base 64 format to String
import [Link].*;
public class GFG {
public static void main(String[] args)
{
// create an encoded String to decode
String encoded ="SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw";
// print encoded String
[Link]("Encoded String:\n" + encoded);
// decode into String from encoded format
byte[] actualByte = [Link]() .decode(encoded);
String actualString = new String(actualByte);
// print actual String
[Link]("actual String:\n"+ actualString);
}
}
Output:
Encoded String:
SW5kaWEgVGVhbSB3aWxsIHdpbiB0aGUgQ3Vw
actual String:
India Team will win the Cup
Example: URL encoding using Base64 class.
import [Link].*;
public class GFG {
public static void main(String[] args)
{
// create a sample url String to encode
String sampleURL = "https:// [Link]/";
// print actual URL String
[Link]("Sample URL:\n"+ sampleURL);
// Encode into Base64 URL format
String encodedURL =
[Link]().encodeToString([Link]());
// print encoded URL
[Link]("encoded URL:\n" + encodedURL);
}
}
Output:
Sample URL:
https:/ / [Link] /
encoded URL:
aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv
Example: URL Decoding using Base64 class.
import [Link].*;
public class GFG {
public static void main(String[] args)
{ // create a encoded URL to decode
String encoded ="aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv";
[Link]("encoded URL:\n" + encoded);
// decode into String URL from encoded format
byte[] actualByte = [Link]() .decode(encoded);
String actualURLString = new String(actualByte);
// print actual String
[Link]("actual String:\n" + actualURLString); }}
Output:
encoded URL:
aHR0cHM6Ly93d3cuZ2Vla3Nmb3JnZWVrcy5vcmcv
actual String:
[Link]
Java forEach loop
• Java provides a new method forEach() to iterate the elements.
• defined in Iterable and Stream interface.
• It is a default method defined in the Iterable interface.
• Collection classes which extends Iterable interface can use
forEach loop to iterate elements.
• This method takes a single parameter which is a functional
interface. So, you can pass lambda expression as an argument.
• forEach() Signature in Iterable Interface
default void forEach(Consumer<super T>action)
• Java 8 forEach() example
import [Link];
import [Link];
public class ForEachExample {
public static void main(String[] args) {
List<String> gamesList = new ArrayList<String>();
[Link]("Football");
[Link]("Cricket");
[Link]("Chess");
[Link]("Hocky");
[Link]("------------Iterating by passing lambda expression------
--------");
[Link](games -> [Link](games));
}
}
Output:
------------Iterating by passing lambda expression--------------
Football
Cricket
Chess
Hocky
Java 8 forEach() example 2
import [Link];
import [Link];
public class ForEachExample {
public static void main(String[] args) {
List<String> gamesList = new ArrayList<String>();
[Link]("Football");
[Link]("Cricket");
[Link]("Chess");
[Link]("Hocky");
[Link]("------------Iterating by passing method reference---------------");
[Link]([Link]::println);
}
}
Output:
------------Iterating by passing method reference---------------
Football
Cricket
Chess
Hocky
Java Stream forEachOrdered() Method
• with forEach() method, Java provides one more
method forEachOrdered(). It is used to iterate
elements in the order specified by the stream.
• Singnature:
void forEachOrdered(Consumer<? super T> action)
forEachOrdered() Method Example
[Link] [Link];
[Link] [Link];
[Link] class ForEachOrderedExample {
4. public static void main(String[] args) {
5. List<String> gamesList = new ArrayList<Str
ing>();
6. [Link]("Football");
7. [Link]("Cricket");
1. [Link]("Chess");
2. [Link]("Hocky");
3. [Link]("------------
Iterating by passing lambda expression---------------");
4. [Link]().forEachOrdered(games -
> [Link](games));
5. [Link]("------------
Iterating by passing method reference---------------");
6. [Link]().forEachOrdered([Link]::println);
7. }
8.
9.}
------------Iterating by passing lambda expression---------------
Football
Cricket
Chess
Hocky
------------Iterating by passing method reference---------------
Football
Cricket
Chess
Hocky
Thank You