|
| 1 | +/** |
| 2 | + * https://simplesolution.dev/ |
| 3 | + */ |
| 4 | +package simplesolution.dev; |
| 5 | + |
| 6 | +import org.apache.commons.codec.binary.Base64; |
| 7 | + |
| 8 | +public class Base64Samples { |
| 9 | + |
| 10 | + public static void main(String... args) { |
| 11 | + encodeSolution1(); |
| 12 | + encodeSolution2(); |
| 13 | + encodeSolution3(); |
| 14 | + encodeSolution4(); |
| 15 | + |
| 16 | + decodeSolution1(); |
| 17 | + decodeSolution2(); |
| 18 | + decodeSolution3(); |
| 19 | + decodeSolution4(); |
| 20 | + } |
| 21 | + |
| 22 | + private static void encodeSolution1() { |
| 23 | + Base64 base64 = new Base64(); |
| 24 | + String valueToEncode = "https://simplesolution.dev/"; |
| 25 | + byte[] encodedBytes = base64.encode(valueToEncode.getBytes()); |
| 26 | + String encodedString = new String(encodedBytes); |
| 27 | + |
| 28 | + System.out.println("Decode Solution #1 output: "); |
| 29 | + System.out.println(encodedString); |
| 30 | + } |
| 31 | + |
| 32 | + private static void encodeSolution2() { |
| 33 | + Base64 base64 = new Base64(); |
| 34 | + String valueToEncode = "https://simplesolution.dev/"; |
| 35 | + String encodedString = base64.encodeToString(valueToEncode.getBytes()); |
| 36 | + |
| 37 | + System.out.println("Decode Solution #2 output: "); |
| 38 | + System.out.println(encodedString); |
| 39 | + } |
| 40 | + |
| 41 | + private static void encodeSolution3() { |
| 42 | + String valueToEncode = "https://simplesolution.dev/"; |
| 43 | + byte[] encodedBytes = Base64.encodeBase64(valueToEncode.getBytes()); |
| 44 | + String encodedString = new String(encodedBytes); |
| 45 | + |
| 46 | + System.out.println("Decode Solution #3 output: "); |
| 47 | + System.out.println(encodedString); |
| 48 | + } |
| 49 | + |
| 50 | + private static void encodeSolution4() { |
| 51 | + String valueToEncode = "https://simplesolution.dev/"; |
| 52 | + String encodedString = Base64.encodeBase64String(valueToEncode.getBytes()); |
| 53 | + |
| 54 | + System.out.println("Decode Solution #4 output: "); |
| 55 | + System.out.println(encodedString); |
| 56 | + } |
| 57 | + |
| 58 | + private static void decodeSolution1() { |
| 59 | + Base64 base64 = new Base64(); |
| 60 | + String valueToDecode = "aHR0cHM6Ly9zaW1wbGVzb2x1dGlvbi5kZXYv"; |
| 61 | + byte[] bytesToDecode = valueToDecode.getBytes(); |
| 62 | + byte[] decodedBytes = base64.decode(bytesToDecode); |
| 63 | + String decodedString = new String(decodedBytes); |
| 64 | + |
| 65 | + System.out.println("Decode Solution #1 output: "); |
| 66 | + System.out.println(decodedString); |
| 67 | + } |
| 68 | + |
| 69 | + private static void decodeSolution2() { |
| 70 | + Base64 base64 = new Base64(); |
| 71 | + String valueToDecode = "aHR0cHM6Ly9zaW1wbGVzb2x1dGlvbi5kZXYv"; |
| 72 | + byte[] decodedBytes = base64.decode(valueToDecode); |
| 73 | + String decodedString = new String(decodedBytes); |
| 74 | + |
| 75 | + System.out.println("Decode Solution #2 output: "); |
| 76 | + System.out.println(decodedString); |
| 77 | + } |
| 78 | + |
| 79 | + private static void decodeSolution3() { |
| 80 | + String valueToDecode = "aHR0cHM6Ly9zaW1wbGVzb2x1dGlvbi5kZXYv"; |
| 81 | + byte[] bytesToDecode = valueToDecode.getBytes(); |
| 82 | + byte[] decodedBytes = Base64.decodeBase64(bytesToDecode); |
| 83 | + String decodedString = new String(decodedBytes); |
| 84 | + |
| 85 | + System.out.println("Decode Solution #3 output: "); |
| 86 | + System.out.println(decodedString); |
| 87 | + } |
| 88 | + |
| 89 | + private static void decodeSolution4() { |
| 90 | + String valueToDecode = "aHR0cHM6Ly9zaW1wbGVzb2x1dGlvbi5kZXYv"; |
| 91 | + byte[] decodedBytes = Base64.decodeBase64(valueToDecode); |
| 92 | + String decodedString = new String(decodedBytes); |
| 93 | + |
| 94 | + System.out.println("Decode Solution #4 output: "); |
| 95 | + System.out.println(decodedString); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments