Implementation of BASE64 Coding and Decoding for Pictures in Java 8

Keywords: Java encoding JDK

Basic encoding is a standard BASE64 encoding, which is used to handle conventional requirements: the output content does not add line breaks, and the output is composed of letters plus numbers.

Recently, I made a Web template, in which I want to use Base64 background image. Although there are ready-made encoders on the Internet, we always want to achieve one by ourselves. Maybe many people don't know that the new Base64 class provided by JDK 8 can handle this task very easily: Base64 (Java Platform SE 8 ) .

import java.io.IOException;
import java.nio.file.*;
import java.util.Base64;

import static java.lang.System.out;

public class Base64Trans {

    /**
     * Read the content from the picture file.
     * @param path The path of the image file.
     * @return Binary image content byte array.
     *
     */
    private byte[] readFile(Path path) {
        byte[] imageContents = null;

        try {
            imageContents = Files.readAllBytes(path);
        } catch (IOException e) {
            out.println("Error reading file...~zZ");
        }

        return imageContents;
    }

    /**
     * Encoding picture files, encoding output is {@code String} format.
     * @param imageContents Binary image content byte array.
     * @return {@code String}The encoded content of the format.
     */
    private String base64Encoding(byte[] imageContents) {
        if(imageContents != null)
            return Base64.getEncoder().encodeToString(imageContents);
        else return null;
    }

    /**
     * Decode picture files.
     * @param imageContents The string format of the picture file to be decoded.
     * @return The binary content of the decoded image file.
     */
    private byte[] base64Decoding(String imageContents) {
        if(imageContents != null)
            return Base64.getDecoder().decode(imageContents);
        else return null;
    }

    /**
     * Write the decoded binary content into the file.
     * @param path Write path.
     * @param imageContents The decoded binary content.
     */
    private void writeFile(Path path, byte[] imageContents) {
        if(imageContents != null)
            try {
                Files.write(path, imageContents, StandardOpenOption.CREATE);
            } catch (IOException e) {
                out.println("Write file error...~zZ");
            }
    }

    public static void main(String[] args) {
        Base64Trans bt = new Base64Trans();
        String encodingString = bt.base64Encoding(bt.readFile(Paths.get("D:/temp/pic.jpg")));

        out.println("Binary Picture File Base64 Code:" + encodingString);

        bt.writeFile(Paths.get("D:/temp/pic2.jpg"), bt.base64Decoding(encodingString));

        out.println("End of task...");
    }
}

The BASE64Encoder and BASE64Decoder classes under the sun.misc suite are not recommended and may be removed in future JDK versions.

Posted by nbarone on Thu, 03 Oct 2019 19:16:42 -0700