Table of Contents
ToggleJava Base64 Encoder Methods
在 Java 中提供 Base64 編碼器,將輸入映射到 A-Z 、 a-z 、 0-9 、 +/ 字元集中的一組字元,輸出編碼字串的長度必須是四的倍數,若不足夠,會根據需要在輸出末尾添加一兩個填充字元 (=),以滿足此要求,同時也使用 Apache Commons Codec 的實用 API 進行編碼, Base64 Encoder Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- base64
| +- basic
| +- Base64EncoderMethodsTest.java
單元測試
Base64 Encoder Methods Java 提供 Base64 基本編碼器操作,將 byte[] 編碼成 Base64 。
encode
建立一個 String ,轉成 byte[] ,使用 Base64.Encoder 進行編碼。
@Test
public void encode() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
byte[] result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
[B@6aa8ceb6
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==
encodeWithoutPadding
Base64 Encoder Methods Java 建立一個 String ,轉成 byte[] ,使用 Base64.Encoder 不進行填充的編碼。
@Test
public void encodeWithoutPadding() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
byte[] result = encoder.withoutPadding().encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
[B@6aa8ceb6
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw
encodeWithDst
Base64 Encoder Methods Java 建立一個 String ,轉成 byte[] 使用 Base64.Encoder 進行編碼,將結果存放到另一個陣列。
@Test
public void encodeWithDst() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
int size = 100;
byte[] dst = new byte[size];
int result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
String stringResult = new String(dst);
System.out.println(stringResult);
assertThat(stringResult)
.containsIgnoringWhitespaces(
"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==")
.hasSize(size);
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
88
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==
encodeWithByteBuffer
Base64 Encoder Methods Java 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.Encoder 進行編碼,傳回 ByteBuffer 。
@Test
public void encodeWithByteBuffer() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.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)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
java.nio.HeapByteBuffer[pos=0 lim=64 cap=64]
java.nio.HeapByteBuffer[pos=0 lim=88 cap=88]
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==
encodeToString
Base64 Encoder Methods 建立一個 String ,轉成 byte[] 使用 Base64.Encoder 進行編碼,傳回字串。
@Test
public void encodeToString() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
String result = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==
wrap
Base64 Encoder Methods 建立一個檔案 ,使用 Base64.Encoder 進行編碼,將結果輸出到另一個檔案。
@Test
public void wrap() throws Exception {
Path src = Paths.get("./", "README.md");
System.out.println(src);
Path dst = Paths.get("./", "README.log");
System.out.println(dst);
Base64.Encoder encoder = Base64.getEncoder();
try (OutputStream output = Files.newOutputStream(dst)) {
long result = Files.copy(src, encoder.wrap(output));
System.out.println(result);
// OutputStream encodedStrem = encoder.wrap(output);
}
}
./README.md
./README.log
1214
Base64_encodeBase64
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行編碼。
@Test
public void Base64_encodeBase64() throws Exception {
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.encodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
[B@4c98385c
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==
Base64_encodeBase64String
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行編碼,傳回字串。
@Test
public void Base64_encodeBase64String() throws Exception {
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
String result = org.apache.commons.codec.binary.Base64
.encodeBase64String(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/
QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==
Base64EncoderMethodsTest.java
Base64 Encoder Methods in Java 新增單元測試,驗證 Base64 Encoder Functions in Java 是否符合預期。
package org.ruoxue.java_147.base64.basic;
import static org.assertj.core.api.Assertions.assertThat;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64;
import org.junit.Test;
public class Base64EncoderMethodsTest {
@Test
public void encode() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
byte[] result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
@Test
public void encodeWithoutPadding() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
byte[] result = encoder.withoutPadding().encode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw");
}
@Test
public void encodeWithDst() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
int size = 100;
byte[] dst = new byte[size];
int result = encoder.encode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
String stringResult = new String(dst);
System.out.println(stringResult);
assertThat(stringResult)
.containsIgnoringWhitespaces(
"QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==")
.hasSize(size);
}
@Test
public void encodeWithByteBuffer() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.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)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
@Test
public void encodeToString() throws Exception {
Base64.Encoder encoder = Base64.getEncoder();
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
String result = encoder.encodeToString(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
@Test
public void wrap() throws Exception {
Path src = Paths.get("./", "README.md");
System.out.println(src);
Path dst = Paths.get("./", "README.log");
System.out.println(dst);
Base64.Encoder encoder = Base64.getEncoder();
try (OutputStream output = Files.newOutputStream(dst)) {
long result = Files.copy(src, encoder.wrap(output));
System.out.println(result);
// OutputStream encodedStrem = encoder.wrap(output);
}
}
@Test
public void Base64_encodeBase64() throws Exception {
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.encodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
@Test
public void Base64_encodeBase64String() throws Exception {
String value = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
System.out.println(value);
String result = org.apache.commons.codec.binary.Base64
.encodeBase64String(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
assertThat(result)
.isEqualTo("QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVphYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ejAxMjM0NTY3ODkrLw==");
}
}
心得分享
Base64 Encoder Functions in Java 工具類別提供了一套靜態方法取得基本編碼器,依照 RFC 4648 中指定的 Base64 字母表,輸出映射到一組字元A-Za-z0-9+/ ,不添加任何行分隔符號,熟悉 Base64 Encoder Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。