-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAboutBase64.java
More file actions
40 lines (32 loc) · 1.38 KB
/
AboutBase64.java
File metadata and controls
40 lines (32 loc) · 1.38 KB
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
package java8;
import com.sandwich.koan.Koan;
import java.util.Base64;
import java.io.UnsupportedEncodingException;
import static com.sandwich.koan.constant.KoanConstants.__;
import static com.sandwich.util.Assert.assertEquals;
public class AboutBase64 {
private final String plainText = "lorem ipsum";
private final String encodedText = "bG9yZW0gaXBzdW0=";
@Koan
public void base64Encoding() {
try {
// Encode the plainText
// This uses the basic Base64 encoding scheme but there are corresponding
// getMimeEncoder and getUrlEncoder methods available if you require a
// different format/Base64 Alphabet
assertEquals(encodedText, Base64.getEncoder().encodeToString("lorem ipsum".getBytes("utf-8")));
} catch (UnsupportedEncodingException ex) {}
}
@Koan
public void base64Decoding() {
// Decode the Base64 encodedText
// This uses the basic Base64 decoding scheme but there are corresponding
// getMimeDecoder and getUrlDecoder methods available if you require a
// different format/Base64 Alphabet
byte[] decodedBytes = Base64.getDecoder().decode("bG9yZW0gaXBzdW0=");
try {
String decodedText = new String(decodedBytes, "utf-8");
assertEquals(plainText, decodedText);
} catch (UnsupportedEncodingException ex) {}
}
}