Encoding/Decoding is the method of representing an data, to a different format so that data can be transferred through the network or web.
Encoder usually converts the data into web representation and after received in the other end, decoder converts back the web representation data to original data.
1. Base64 Encoding/Decoding
Base64 is a way of handling binary/any characters to text format so that the information can be easily transmitted via network between systems.
Below is the sample Base64 encode decode, present in Java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; public class EncoderDecoder { public static void main(String[] args) { Encoder encoder = Base64.getEncoder(); String originalData = "This is my sample message across internet..."; System.out.println("Original String :\t" + originalData); byte[] encodedData = encoder.encode(originalData.getBytes()); String encodedString = new String(encodedData); System.out.println("Encoded String :\t" + encodedString); Decoder decoder = Base64.getDecoder(); byte[] decodedDataInBytes = decoder.decode(encodedString.getBytes()); String decodedData = new String(decodedDataInBytes); System.out.println("Decoded String :\t" + decodedData); } } |
Result:
1 2 3 |
Original String : This is my sample message across internet... Encoded String : VGhpcyBpcyBteSBzYW1wbGUgbWVzc2FnZSBhY3Jvc3MgaW50ZXJuZXQuLi4= Decoded String : This is my sample message across internet... |
2. XML Encoding/Decoding
A more commonly widely used interchange format is XML., and we can convert Java Objects to XML, save with state, and also pass it to external systems, and decode the XML back to Java Objects.
java.beans.* package provides XMLEncoder and XMLDecoder by default for XML encoding/decoding.
Below example shows how a Java Object is converted to XML and XML is converted back to Java Object.
However the Java Object or Pojo (in this case Ticket class) which needs to be encoded to XML needs to be public class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
package my.tutorialflow; import java.util.Date; public class Ticket { public String showName; public Date showTiming; public int price; public Ticket() {} public Ticket(String showName, Date showTiming, int price) { this.showName = showName; this.showTiming = showTiming; this.price = price; } public String getShowName() { return showName; } public void setShowName(String showName) { this.showName = showName; } public Date getShowTiming() { return showTiming; } public void setShowTiming(Date showTiming) { this.showTiming = showTiming; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } @Override public String toString() { return "Ticket [showName=" + showName + ", showTiming=" + showTiming + ", price=" + price + "]"; } } |
In the below example, a movieTicket object of Ticket class is created and it is converted to XML using XMLEncoder class, and when the XML is printed, we could the corresponding values are also printed.(state is maintained)
Again, when the XML with state is decoded using XMLDecoder class, we could see the Object created back with the same state..(with showName, showTiming & price)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
package my.tutorialflow; import java.beans.XMLDecoder; import java.beans.XMLEncoder; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.GregorianCalendar; public class XMLEncoderDecoder { public static void main(String[] args) throws IOException { Ticket movieTicket = new Ticket("Mission Impossible", new GregorianCalendar(2019, 5, 31).getTime(), 1200); System.out.println("Java Object of Ticket: " + movieTicket); /***** XMLEncoder - Encoding the Java Object to XML or converting Java Object to XML *****/ System.out.println("XML Output converted:::::::"); String xmlEncodedTicket = encodeUsingXMLEncoder(movieTicket); System.out.println(xmlEncodedTicket); System.out.println("::::::::::::::::::::::::::"); /***** XMLDecoder - Encoding the encoded XML back to Java Object or converting XML to Pojo *****/ System.out.println("Back to Java Class converted:::::::"); Ticket movieTicketFromXML = decodeUsingXMLDecoder(xmlEncodedTicket); System.out.println(movieTicketFromXML); System.out.println("::::::::::::::::::::::::::"); } public static String encodeUsingXMLEncoder(Ticket movieTicket) throws IOException { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); XMLEncoder xmlEncoder = new XMLEncoder(byteArrayOutputStream); xmlEncoder.writeObject(movieTicket); xmlEncoder.close(); byte[] xmlInBytes = byteArrayOutputStream.toByteArray(); String xmlEncodedTicket = new String(xmlInBytes); byteArrayOutputStream.close(); return xmlEncodedTicket; } public static Ticket decodeUsingXMLDecoder(String movieTicketInXMLString) throws IOException { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(movieTicketInXMLString.getBytes()); XMLDecoder xmlDecoder = new XMLDecoder(byteArrayInputStream); Ticket movieTicket = (Ticket) xmlDecoder.readObject(); xmlDecoder.close(); return movieTicket; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
Java Object of Ticket: Ticket [showName=Mission Impossible, showTiming=Mon Jul 01 00:00:00 IST 2019, price=1200] XML Output converted::::::: <?xml version="1.0" encoding="UTF-8"?> <java version="1.8.0_181" class="java.beans.XMLDecoder"> <object class="my.test.Ticket" id="Ticket0"> <void class="my.test.Ticket" method="getField"> <string>showName</string> <void method="set"> <object idref="Ticket0"/> <string>Mission Impossible</string> </void> <void class="my.test.Ticket" method="getField"> <string>price</string> <void method="set"> <object idref="Ticket0"/> <int>1200</int> </void> </void> </object> </java> :::::::::::::::::::::::::: Back to Java Class converted::::::: Ticket [showName=Mission Impossible, showTiming=Mon Jul 01 00:00:00 IST 2019, price=1200] :::::::::::::::::::::::::: |