Skip to content

Instantly share code, notes, and snippets.

@priyankahdp
Created August 15, 2018 11:37
Show Gist options
  • Save priyankahdp/b7591b46cddfbceac828600ee934cb8a to your computer and use it in GitHub Desktop.
Save priyankahdp/b7591b46cddfbceac828600ee934cb8a to your computer and use it in GitHub Desktop.
EncodeORDecode
package com.priyan.demo;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import org.apache.commons.codec.binary.Base64;
public class PriyanDemo {
public static void main(String[] args) {
String orgString="0989786574";
String encodedString=encode("0989786574");
String decodedString=decode("0989786574");
boolean isBase64Pre = Base64.isBase64(orgString);
boolean isBase64Post = Base64.isBase64(encodedString);
boolean isBase64Dec = Base64.isBase64(decodedString);
System.out.println(isBase64Pre);
System.out.println(isBase64Post);
System.out.println(isBase64Dec);
}
public static String encode(String url) {
try {
String encodeURL = URLEncoder.encode(url, "UTF-8");
return encodeURL;
} catch (UnsupportedEncodingException e) {
return "Issue while encoding" + e.getMessage();
}
}
public static String decode(String url) {
try {
String prevURL = "";
String decodeURL = url;
while (!prevURL.equals(decodeURL)) {
prevURL = decodeURL;
decodeURL = URLDecoder.decode(decodeURL, "UTF-8");
}
return decodeURL;
} catch (UnsupportedEncodingException e) {
return "Issue while decoding" + e.getMessage();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment