All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jio.console.Base64DecodeCommand Maven / Gradle / Ivy

There is a newer version: 3.0.0-RC2
Show newest version
package jio.console;

import jio.IO;
import jio.RetryPolicies;
import jsonvalues.JsObj;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.function.Function;

/**
 * Represents a command that decodes a base64 encoded string into a new string using the Base64 encoding scheme. The
 * decoded string is returned as the result of this command.
 * 

* Usage: {@code base64-decode {encoded}} *

* Examples: - {@code base64-decode aGkhIGknbGwgYmUgZW5jb2RlZCBpbnRvIGJhc2UgNjQ=} - {@code base64-decode $var} */ class Base64DecodeCommand extends Command { private final static Base64.Decoder decoder = Base64.getDecoder(); private static final String COMMAND_NAME = "base64-decode"; public Base64DecodeCommand() { super( COMMAND_NAME, """ Decodes a base64 encoded string into a new string using the Base64 encoding scheme. Usage: $command {encoded} Examples: $command aGkhIGknbGwgYmUgZW5jb2RlZCBpbnRvIGJhc2UgNjQ=""".replace( "$command", COMMAND_NAME ) ); } @Override public Function> apply( final JsObj conf, final State state ) { return tokens -> { int nTokens = tokens.length; if (nTokens == 1) { return Programs.ASK_FOR_INPUT(new Programs.AskForInputParams( "Type the string encoded in base64", e -> e.length() == 1, "Space blank is not a valid base64 scheme", RetryPolicies.limitRetries(3) )) .then(encoded -> IO.succeed(new String( decoder.decode(encoded), StandardCharsets.UTF_8 ))); } if (nTokens == 2) { return IO.succeed( new String( decoder.decode(tokens[1]), StandardCharsets.UTF_8 ) ); } return IO.fail( new InvalidCommand( this, "Space blank is not a valid base64 character" )); }; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy