Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net library : transformations #28

Open
sporniket opened this issue Jan 30, 2020 · 0 comments
Open

net library : transformations #28

sporniket opened this issue Jan 30, 2020 · 0 comments

Comments

@sporniket
Copy link
Owner

Convert the following code into builtins mapper 👍

import static java.nio.charset.StandardCharsets.UTF_8 ;

import java.io.UnsupportedEncodingException ;
import java.net.URLDecoder ;
import java.net.URLEncoder ;
import java.util.Base64 ;

/**
 * @author spornda
 *
 */
public final class BuiltinsNetTransformations {

    public static final Function<String, byte[]> DECODE_BASE64 = t -> Base64.getDecoder().decode(t) ;

    public static final Function<String, String> DECODE_URL = t -> {
        if (null == t || "null".equals(t)) {
            return null ;
        }
        try {
            return URLDecoder.decode(t, UTF_8.name()) ;
        } catch (UnsupportedEncodingException e) {
            // Ça ne doit jamais arriver
            throw new IllegalStateException("The Character Encoding is not supported : " + t) ;
        }
    } ;

    public static final Function<byte[], String> ENCODE_BASE64 = t -> (null != t) ? Base64.getEncoder().encodeToString(t) : "" ;

    public static final Function<String, String> ENCODE_URL = t -> {
        if (t == null) {
            return t ;
        }
        try {
            return URLEncoder.encode(t, UTF_8.name()) ;
        } catch (UnsupportedEncodingException e) {
            // Ça ne doit jamais arriver
            throw new IllegalStateException("The Character Encoding is not supported : " + t) ;
        }

    } ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant