January 3, 2017

Java 8 - Base64

Java 8 now supports java.util.Base64.

  • Base64.getEncoder()/Base64.getDecoder() - Output is mapped to a set of characters lying in A-Za-z0-9+/.
  • Base64.getUrlEncoder()/Base64.getUrlDecoder() - Output is mapped to set of characters lying in A-Za-z0-9+_. Output is URL and filename safe.
  • Base64.getMimeEncoder()/Base64.getMimeDecoder() - Simple format, but represented in lines of 76 characters and seperated with '\r\n'.

Example

byte[] bytes = new byte[2048];
new Random().nextBytes(bytes);
System.out.println("Simple :\n" + new String(getEncoder().encode(bytes), UTF_8) + "\n");
System.out.println("URL :\n" + new String(getUrlEncoder().encode(bytes), UTF_8) + "\n");
System.out.println("MIME :\n" + new String(getMimeEncoder().encode(bytes), UTF_8) + "\n");

Output

Simple :
KoogbGxCpjqSlWfn78lUtpKmKYAJY6AwfzvB42RGRezOMaegQk+n3+IeIKMFfC/G1MmZWWLIhqQGD3DEiA58yH/XHUuBhLT4XzFoVIwt8Rfqy0vITcNT8AJxacn+QHrmAV9OMQzrVppW8Y9cv3eT4lSjhb3hca+0C3hc0IMcjUcuA6fgTZe8HksCNiL81LSB5no3rPixLcgw+VI7Ni+x+XSkvraxFZUCdbmcKkQhPlhGaOYws9U01G9iF2scXz8lVGek/yrnXnM9IIMUv6oH5XJyx171Z1ZvPmWL3ZyCAgPkohS6rA04RsPGK5M20t/TN+jE6eMATIsFcTkPBIk3iU4f7bqwqIMeWWZ785c7lf+6BmjiaioK4aH9qh4CMzosT6R6+rOh9mMFVFlIdY7mi/Pv4J25VJsh46VNtytb4CW513nPuGjdRwPDuxaGxnl4LfGmscLatBYPEvH4d87FVPFr5bzCwKyBQdcJPSMUPVCIGWufvhY4il6yJ+jDtArrcZ7rQFucL5vih73cy0zT4ncgV+umhhHAfEkUc06JRtJHBqXEEQrknLxRb0Cg6+1HQ5zS27oAKIoKR249PKOx4JWyZmXzMFmr2rm1E+ihtsaFiEgP/vdNgh3yxjUWTse5D5nJ0RhdoK76n4LFjXeqnbAcp2m40GbCvWN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