ZjAyY2QxNDUyOWZiMjI3NDctNmI3OC00YjgyLThiZGQtM2MyY2E3ZGNjYmIxOTQ1MDVkOGQtMzIz NDMtYzA0MC00MjExLTk0NWMtYmFiZGRlNDk5OTZhMDMxZGE5ZTYtZWVhYS00OGFmLTlhMjgtMDM1 ZmYtMTU1NzY0MWM5Zjk4ODA5ZjVjOGUtOGMxNi00ZmVjLTgyZjctNmVjYTU5MTAxZWUyNjQ1MjJj NDU5ZTFkNDEtMDVlNy00MDFiLTk3YjgtMWRlMmRkMWEzMzc5YTJkZmEzY2YtM2Y2My00Y2Q4LTk5 String mimeEncoded = Base64.getMimeEncoder().encodeToString(toEncode) ![]() The following example generates a block of text (this is needed just to make sure we have enough 'body' to encode into more than 76 characters) and encodes it using the MIME encoder:īyte toEncode = sb.toString().getBytes("utf-8") The MIME encoder generates a Base64 encoded output using the Basic Alphabet but in a MIME friendly format: each line of the output is no longer than 76 characters and ends with a carriage return followed by a linefeed (\r\n). The sample above illustrates some content which if encoded using a basic encoder will result in a string containing a forward slash while when using a URL safe encoder the output will include an underscore instead (URL Safe encoding is described in clause 5 of the RFC) MIME Encoding Using Basic Alphabet: c3ViamVjdHM/YWJjZA= String urlEncoded = Base64.getUrlEncoder().encodeToString("subjects?abcd".getBytes("utf-8")) ("Using Basic Alphabet: " + basicEncoded) String basicEncoded = Base64.getEncoder().encodeToString("subjects?abcd".getBytes("utf-8")) The second type of encoder uses the alternative "URL and Filename safe" Alphabet which includes -_ (minus and underline) instead of +/. Most of us are used to get annoyed when we have to encode something to be later included in a URL or as a filename - the problem is that the Base64 Alphabet contains meaningful characters in both URIs and filesystems (most specifically the forward slash (/)). It can't get simpler than that and, unlike in earlier versions of Java, there is any need for external dependencies (commons-codec), Sun classes (64Decoder) or JAXB's DatatypeConverter (Java 6 and above). (new String(asBytes, "utf-8")) // And the output is: some string (asB64) // Output will be: c29tZSBzdHJpbmc=īyte asBytes = Base64.getDecoder().decode("c29tZSBzdHJpbmc=") String asB64 = Base64.getEncoder().encodeToString("some string".getBytes("utf-8")) ![]() The standard encoding we all think about when we deal with Base64: no line feeds are added to the output and the output is mapped to characters in the Base64 Alphabet: A-Za-z0-9+/ (we see in a minute why is it important). It provides a set of static factory methods used to obtain one of the three Base64 encodes and decoders: ![]() The entry point to Java 8's Base64 support is the 64 class. Here is a short introduction of this new API (apparently it has a little more than the regular encode/decode API). Finally Java 8 includes a decent API for it in the java.util package. The lack of Base64 encoding API in Java is, in my opinion, by far one of the most annoying holes in the libraries.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |