Click Upvote Click Upvote - 26 days ago 13
Java Question

Catch-all way of handling all character encodings in Java?

I have a use case where I need to be able to read user generated data into a string, and do all the normal string operations on it.

The user generated data will be base64 encoded, and I need to decode that into a string.

I know nothing about this data except that it will be of the mime-type text/plain .

I want a simple, no-brainer way of decoding this data into a string. Something that just works out of the box and I don't have to think about any edge cases.

Any ideas?

Answer
 public static String fromBase64ToString(String base64String){
        byte[] b = Base64.getDecoder().decode(base64String);
        return new String(b);
    }
Comments