Mime Encoding and Decoding using Base64 in Java - Java 147

MIME Encoding and Decoding using Base64 in Java – Java 147

MIME Encoding and Decoding using Base64 in Java

使用 MIME 編碼器轉換成 Base64 ,將非 ASCII 資料傳輸到電子郵件伺服器的規格,與 Base64 編碼類似,是一種二進位到文字的編碼方案,會取得原始輸入的位元組,並使用字元 A-Z 、 a-z 、 0-9 以及 + 、 / 等,編碼的資料超過 76 個字元,則會資料將拆分為多行,每行最多包含 76 個字元,Java 8 套件 Base64 類別中提供了一個 MIME 編碼器,可用於將字串編碼為 Base64,並將 Base64 字串解碼為普通字串, MIME Java Encoding Decoding 本篇增加了範例,並透過單元測試來驗證產出結果。

檔案目錄

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

單元測試

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

encodeWithDst

建立一個 String ,轉成 byte[] 使用 Base64.MimeEncoder 進行編碼,將結果存放到另一個陣列。

	@Test
	public void encodeWithDst() 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);
		int size = 256;
		byte[] dst = new byte[size];
		int result = encoder.encode(value.toString().getBytes(StandardCharsets.UTF_8.toString()), dst);
		System.out.println(result);
		String stringResult = new String(dst);
		System.out.println(stringResult);
		assertThat(stringResult).containsIgnoringWhitespaces(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==")
				.hasSize(size);
	}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
94
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==

decodeWithDst

Java Encoding Decoding MIME 建立一個 String ,轉成 byte[] 使用 Base64.MimeDecoder 進行解碼,將結果存放到另一個陣列。

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

encodeWithByteBuffer

Java Encoding Decoding MIME 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.MimeEncoder 進行編碼,傳回 ByteBuffer 。

	@Test
	public void encodeWithByteBuffer() 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");
		ByteBuffer byteBuffer = ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(byteBuffer);
		ByteBuffer result = encoder.encode(byteBuffer);
		System.out.println(result);
		String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
		System.out.println(stringResult);
		assertThat(stringResult).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}
java.nio.HeapByteBuffer[pos=0 lim=67 cap=67]
java.nio.HeapByteBuffer[pos=0 lim=94 cap=94]
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==

decodeWithByteBuffer

Java Encoding Decoding MIME 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.MimeDecoder 進行解碼,傳回 ByteBuffer 。

	@Test
	public void decodeWithByteBuffer() throws Exception {
		Base64.Decoder decoder = Base64.getMimeDecoder();
		String value = "SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n"
				+ "NjAsIElUIDQ4NA==";
		ByteBuffer byteBuffer = ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(byteBuffer);
		ByteBuffer result = decoder.decode(byteBuffer);
		System.out.println(result);
		String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
		System.out.println(stringResult);
		assertThat(stringResult).isEqualTo("Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484");
	}
java.nio.HeapByteBuffer[pos=0 lim=93 cap=93]
java.nio.HeapByteBuffer[pos=0 lim=67 cap=67]
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484

encodeToString

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

	@Test
	public void encodeToString() 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);
		String result = encoder.encodeToString(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		assertThat(result).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}
Java 147, Spring boot 168, Junit 151, AssertJ 155, Bash 460, IT 484
SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0
NjAsIElUIDQ4NA==

decodeWithString

MIME Java Encoder Decoder using Base64 建立一個 String ,使用 Base64.MimeEncoder 進行解碼。

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

MimeEncodingDecodingBase64Test.java

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

package org.ruoxue.java_147.base64.mime;

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

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

import org.junit.Test;

public class MIMEEncodingDecodingBase64Test {

	@Test
	public void encodeWithDst() 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);
		int size = 256;
		byte[] dst = new byte[size];
		int result = encoder.encode(value.toString().getBytes(StandardCharsets.UTF_8.toString()), dst);
		System.out.println(result);
		String stringResult = new String(dst);
		System.out.println(stringResult);
		assertThat(stringResult).containsIgnoringWhitespaces(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==")
				.hasSize(size);
	}

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

	@Test
	public void encodeWithByteBuffer() 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");
		ByteBuffer byteBuffer = ByteBuffer.wrap(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(byteBuffer);
		ByteBuffer result = encoder.encode(byteBuffer);
		System.out.println(result);
		String stringResult = new String(result.array(), StandardCharsets.UTF_8.toString());
		System.out.println(stringResult);
		assertThat(stringResult).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}

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

	@Test
	public void encodeToString() 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);
		String result = encoder.encodeToString(value.toString().getBytes(StandardCharsets.UTF_8.toString()));
		System.out.println(result);
		assertThat(result).containsIgnoringNewLines(
				"SmF2YSAxNDcsIFNwcmluZyBib290IDE2OCwgSnVuaXQgMTUxLCBBc3NlcnRKIDE1NSwgQmFzaCA0\n" + "NjAsIElUIDQ4NA==");
	}

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

心得分享

MIME Java Encoding and Decoding Example 使用 MIME 編碼器,將字串編碼為 Base64,首先將字串轉換為 byte 陣列,然後使用 Base64.MimeEncoder 將 byte 陣列傳給 encode 方法,反之要解碼 Base64 編碼的字串,則使用 Base64.MimeDecoder ,將字串傳給 decode 方法,取得 byte 陣列,提供 MIME Java Encoder Decoder using Base64 工具類別的靜態方法,使用基本編碼器,進行編碼解碼的操作。

發佈留言