I try to find a way to encode and decode strings with the use of java.util.Base64.Encoder and Decoder classes. Unfortunately, it is impossible to call encode and decode methods statically, so I've created references to Encoder and Decoder classes. But in order to create an instance object for each of those references I need to cast some arguments into constructors. To be honest I don't have even a clue what arguments I can cast to them. API documentation remains silent https://docs.oracle.com/javase/8/docs/api/java/util/Base64.Encoder.html. Below is my almost working example, which throws NullPointerException because of lack of Encoder instance.
import java.util.Base64.Encoder;
import java.util.Base64.Decoder;
public class NumberCipher {
private static Encoder encoder;
private static Decoder decoder;
public static void main(String[] args) {
String test = "There is no clue about Batman and Robin tryst at 43 Joker Street Motel.";
String test_enc = encode(test);
String test_dec = decode(test_enc);
System.out.println(test);
System.out.println(test_enc);
System.out.println(test_dec);
}
public static String decode(String toDecode) {
byte[] bytesDecoded = decoder.decode(toDecode.getBytes());
String decoded = new String(bytesDecoded);
return decoded;
}
public static String encode(String toEncode) {
byte[] bytesEncoded = encoder.encode(toEncode.getBytes());
String encoded = new String(bytesEncoded);
return encoded;
}
}