InterviewSolution
Saved Bookmarks
| 1. |
Inbuilt encoder and decoder for Base64 encoding in Java 8 |
|
Answer» The BASE64 class consist of static methods for obtaining encoders and decoders for Base64 encoding. JAVA 8 allows us to use three types of encoding:
The declaration of the Base64 class is as follows: public class Base64 EXTENDS ObjectLet us see an example of basic Base64 encoding and decoding: import java.util.Base64; public class Example { public static void main(String[] args) { String enc = Base64.getEncoder().encodeToString("Encoding in Base64".getBytes()); System.out.println("Encoder Output : " + enc); byte[] dec = Base64.getDecoder().decode(enc); System.out.println("Decoder Output : " + new String(dec)); } }The output is as follows: $javac Example.java $java -Xmx128M -Xms16M Example Encoder Output : RW5jb2RpbmcgaW4gQmFzZTY0 Decoder Output : Encoding in Base64 |
|