Java Mime Encoding and Decoding using Base64 - Java 147

Java MIME Encoding and Decoding using Base64 – Java 147

Java MIME Encoding and Decoding using Base64

從 Java 8 開始,已經包含使用 Base64 編碼的內建編碼器和解碼器套件,其中之一 MIME 編碼器,會將輸出映射到 A-Za-z0-9+/ 中的一組字元,如果編碼的資料超過 76 個字元,則會資料將拆分為多行,每行最多包含 76 個字元,添加行分隔符號 \r\n ,輸出末尾不存在行分隔符號,將忽略所有在 Base64 字母表中,找不到的行分隔符號或其他字元,同時也使用 Apache Commons Codec 的實用 API 進行編解碼, MIME Encoding and Decoding in Java 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

./
   +- src
       +- test
       |   +- org
       |       +- ruoxue
       |           +- java_147
       |               +- base64
       |                   +- mime
       |                       +- MIMEEncodingDecodingTest.java  

單元測試

Java MIME Encoder Decoder 提供 Base64 MIME 編碼器操作,將位元組編碼成 Base64 ,並將 Base64 字串解碼為普通字串。

encode

建立一個 String ,轉成 byte[] ,使用 Base64.MimeEncoder 進行編碼。

	@Test
	public void encode() throws Exception {
		Base64.Encoder encoder = Base64.getMimeEncoder();
		StringBuilder value = new StringBuilder();
		value.append("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
		System.out.println(value);
		byte[] result = encoder.encode(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
[B@6aa8ceb6
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==

decode

Java MIME Encoder Decoder 建立一個 String ,轉成 byte[] ,使用 Base64.MimeDecoder 進行解碼。

	@Test
	public void decode() throws Exception {
		Base64.Decoder decoder = Base64.getMimeDecoder();
		String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n"
				+ "NjAsIElUIDQ4NA==";
		System.out.println(value);
		byte[] result = decoder.decode(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
	}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==
[B@6aa8ceb6
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484

Base64_encodeBase64Chunked

Java MIME Encoder Decoder 建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行編碼。

	@Test
	public void Base64_encodeBase64Chunked() throws Exception {
		StringBuilder value = new StringBuilder();
		value.append("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
		System.out.println(value);
		byte[] result = org.apache.commons.codec.binary.Base64
				.encodeBase64Chunked(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
[B@4c98385c
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==

Base64_decodeBase64

建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行解碼。

	@Test
	public void Base64_decodeBase64() throws Exception {
		String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n"
				+ "NjAsIElUIDQ4NA==";
		System.out.println(value);
		byte[] result = org.apache.commons.codec.binary.Base64
				.decodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
	}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==
[B@4c98385c
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484

Base64_decodeBase64WithString

建立一個 String ,使用 Apache Commons Codec 套件 Base64 進行解碼。

	@Test
	public void Base64_decodeBase64WithString() throws Exception {
		String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n"
				+ "NjAsIElUIDQ4NA==";
		System.out.println(value);
		byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
	}
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==
[B@6bf2d08e
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484

MimeEncodingDecodingTest.java

MIME Encoder Decoder in Java 新增單元測試,驗證 Java MIME Encoding Decoding 是否符合預期。

package org.ruoxue.java_147.base64.mime;

import static org.assertj.core.api.Assertions.assertThat;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

import org.junit.Test;

public class MIMEEncodingDecodingTest {

	@Test
	public void encode() throws Exception {
		Base64.Encoder encoder = Base64.getMimeEncoder();
		StringBuilder value = new StringBuilder();
		value.append("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
		System.out.println(value);
		byte[] result = encoder.encode(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}

	@Test
	public void decode() throws Exception {
		Base64.Decoder decoder = Base64.getMimeDecoder();
		String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n"
				+ "NjAsIElUIDQ4NA==";
		System.out.println(value);
		byte[] result = decoder.decode(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
	}

	@Test
	public void Base64_encodeBase64Chunked() throws Exception {
		StringBuilder value = new StringBuilder();
		value.append("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
		System.out.println(value);
		byte[] result = org.apache.commons.codec.binary.Base64
				.encodeBase64Chunked(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}

	@Test
	public void Base64_decodeBase64() throws Exception {
		String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n"
				+ "NjAsIElUIDQ4NA==";
		System.out.println(value);
		byte[] result = org.apache.commons.codec.binary.Base64
				.decodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
	}

	@Test
	public void Base64_decodeBase64WithString() throws Exception {
		String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n"
				+ "NjAsIElUIDQ4NA==";
		System.out.println(value);
		byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
		System.out.println(result);
		System.out.println(new String(result));
		assertThat(new String(result)).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
	}
}

心得分享

Java MIME Encoding Decoding 依照 RFC 2045 中指定的 Base64 字母表,輸出映射到一組字元A-Za-z0-9+/ ,將忽略所有在 Base64 字母表中,找不到的行分隔符號或其他字元,熟悉 MIME Encoder Decoder in Java 工具類別提供的靜態方法,取得 MIME 編碼器,進行編碼解碼的操作。

發佈留言