|
| 1 | +package java8; |
| 2 | + |
| 3 | +import com.sandwich.koan.Koan; |
| 4 | + |
| 5 | +import java.util.Base64; |
| 6 | +import java.io.UnsupportedEncodingException; |
| 7 | + |
| 8 | +import static com.sandwich.koan.constant.KoanConstants.__; |
| 9 | +import static com.sandwich.util.Assert.assertEquals; |
| 10 | + |
| 11 | +public class AboutBase64 { |
| 12 | + |
| 13 | + private final String plainText = "lorem ipsum"; |
| 14 | + private final String encodedText = "bG9yZW0gaXBzdW0="; |
| 15 | + |
| 16 | + @Koan |
| 17 | + public void base64Encoding() { |
| 18 | + try { |
| 19 | + // Encode the plainText |
| 20 | + // This uses the basic Base64 encoding scheme but there are corresponding |
| 21 | + // getMimeEncoder and getUrlEncoder methods available if you require a |
| 22 | + // different format/Base64 Alphabet |
| 23 | + assertEquals(encodedText, Base64.getEncoder().encodeToString(__.getBytes("utf-8"))); |
| 24 | + } catch (UnsupportedEncodingException ex) {} |
| 25 | + } |
| 26 | + |
| 27 | + @Koan |
| 28 | + public void base64Decoding() { |
| 29 | + // Decode the Base64 encodedText |
| 30 | + // This uses the basic Base64 decoding scheme but there are corresponding |
| 31 | + // getMimeDecoder and getUrlDecoder methods available if you require a |
| 32 | + // different format/Base64 Alphabet |
| 33 | + byte[] decodedBytes = Base64.getDecoder().decode(__); |
| 34 | + try { |
| 35 | + String decodedText = new String(decodedBytes, "utf-8"); |
| 36 | + assertEquals(plainText, decodedText); |
| 37 | + } catch (UnsupportedEncodingException ex) {} |
| 38 | + } |
| 39 | + |
| 40 | +} |
0 commit comments