Data Encoding Tutorials - Herong's Tutorial Examples - v5.22, by Herong Yang
Java Built-In Implementation of MIME Base64
This section provides a tutorial example on how to perform Base64 encoding on binary files with both standard and MIME options using the default Java implementation of the Base64 encoding algorithm.
Java also offers a variation of the Base64 algorithm called MIME (Multipurpose Internet Mail Extensions) Encoder, which wraps the encoded output into 76 characters to meet the MIME specification.
To perform MIME Base64 encoding, you need to call Base64.getMimeEncoder() to create a MIME Base64 encoder.
Here is a sample program that allows you to perform Base64 encoding on any given file with both standard and MIME options:
/* Base64MimeEncoder.java * Copyright (c) 2002 HerongYang.com. All Rights Reserved. */ import java.io.*; import java.util.Base64; class Base64MimeEncoder { public static void main(String[] args) { if (args.length<2) { System.out.println("Usage:"); System.out.println("java Base64MimeEncoder.java input_file mime"); return; } String fileName = args[0]; String mime = args[1]; try { File file = new File(fileName); long length = file.length(); FileInputStream inputStream = new FileInputStream(file); byte[] inputBytes = new byte[(int)length]; int l = inputStream.read(inputBytes); Base64.Encoder encoder = null; if (mime.equals("mime")) { encoder = Base64.getMimeEncoder(); } else { encoder = Base64.getEncoder(); } byte[] encodedBytes = encoder.encode(inputBytes); System.out.println(new String(encodedBytes)); } catch (Exception e) { e.printStackTrace(); } } }
To try the above program, you can download the binary image file from my Website:
herong> curl http://herongyang.com/_logo.png > icon.png
Now encode this binary file with both MIME and standard option:
herong> java Base64MimeEncoder.java icon.png mime iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAAAXNSR0IArs4c6QAAAARnQU1BAACx jwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAAEYSURBVEhL7ZbLDcIwDIYdBGfYADahm1E24QRr 9AxLNBvAuUjgJE7ruI5S8RAXPslqaltxfsdSax4IfJEZPQeMSa2EyLdmTwHCKUhwLmYt1BTIIPIl eovkyZUUj8zrrgDzJb0EQovuN64SbFt7d4/cCLE2bYWFCsxi2e9xutxCwOvormOV3OEMcxJkXLiO 55A/vuSIy+MsVrTA0+5TRfbQ0ErBl9EUIO2hGpxobUUXznzeCO6KCkI0U8DDA5oxuLvcooibjAy2 Zq3BQdEoF8CxsxWtBevdllZ51AJx1KJtGlQtwU4keTiiGmUFGdymU3i5wFTUAuk8BJOMcrpXL/lN /gWKfK6A+A5EpheQYzORr7foB38VHwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC herong> java Base64MimeEncoder.java icon.png standard iVBORw0KGgoAAAANSUhEUgAAABgAAAAY ... HwXgCfzQYOFCEQYGAAAAAElFTkSuQmCC
As you can see, the MIME Base64 encoding is identical to the standard Base64 encoding, except that it adds line breaks to keep the output in 76-character line format.
According the Base64 specification, adding line breaks and other whitespaces in the encoded output is allowed.
Table of Contents
►Base64 Encoding and Decoding Tools
Base64.Guru - Base64 Online Tool
Windows Command - "certutil -encode/-decode"
Java Built-In Implementation of Base64
►Java Built-In Implementation of MIME Base64
Python Built-In Implementation of Base64
Python Built-In Implementation of MIME Base64
PHP Built-In Implementation of Base64
PHP Built-In Implementation of MIME Base64
Perl Built-In Implementation of Base64
Perl Built-In Implementation of MIME Base64
Base64URL - URL Safe Base64 Encoding