Skip to content

Commit 086083e

Browse files
Java Base64 Encoding and Decoding with Apache Commons Codec
1 parent 00d84da commit 086083e

File tree

9 files changed

+386
-2
lines changed

9 files changed

+386
-2
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.gradle
2+
.idea
3+
build

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
# Base64ApacheCommonsCodec
2-
Java Base64 Encoding and Decoding with Apache Commons Codec
1+
# Java Base64 Encoding and Decoding with Apache Commons Codec
2+
Sample application for tutorial: https://simplesolution.dev/java-base64-encoding-decoding-apache-commons-codec/
3+
4+
## Gradle command To run application
5+
> gradle run

build.gradle

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
group 'simplesolution.dev'
2+
version '1.0.0'
3+
4+
apply plugin: 'java'
5+
apply plugin: 'application'
6+
7+
mainClassName = "simplesolution.dev.Base64Samples"
8+
9+
sourceCompatibility = 1.8
10+
11+
repositories {
12+
mavenCentral()
13+
}
14+
15+
dependencies {
16+
compile group: 'commons-codec', name: 'commons-codec', version: '1.12'
17+
}

gradle/wrapper/gradle-wrapper.jar

53.1 KB
Binary file not shown.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
zipStoreBase=GRADLE_USER_HOME
4+
zipStorePath=wrapper/dists
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-bin.zip

gradlew

Lines changed: 172 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gradlew.bat

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = 'Base64ApacheCommonsCodec'
2+
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)