Table of Contents
ToggleJava Base64 UrlDecoder Methods
提供 URL 解碼器,對字串進行解碼,然後將傳回值轉換為實際的 URL ,支援 URL 和檔案名稱安全性,與基本 Base64 編碼相同,只是將 + 替換為 – ,並將 / 替換為 _ ,使得輸出 URL 和檔案名稱安全,解碼器拒絕包含 A-Za-z0-9-_ 之外的字元的資料,此外也採用 Apache Commons Codec 的實用 API 進行解碼, Base64 UrlDecoder Java Methods 本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- base64
| +- url
| +- Base64UrlDecoderMethodsTest.java
單元測試
Base64 UrlDecoder Methods Java 提供 URL 解碼器操作,將字串解碼成二進位資料。
decode
建立一個 String ,轉成 byte[] ,使用 Base64.UrlDecoder 進行解碼。
@Test
public void decode() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=
[B@6aa8ceb6
https://www.ruoxue.org/?s=base64
decodeWithoutPadding
Base64 UrlDecoder Methods Java 建立一個 String ,編碼不進行填充 ,轉成 byte[] ,使用 Base64.UrlDecoder 解碼。
@Test
public void decodeWithoutPadding() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ
[B@6aa8ceb6
https://www.ruoxue.org/?s=base64
decodeWithDst
Base64 UrlDecoder Methods Java 建立一個 String ,轉成 byte[] 使用 Base64.UrlDecoder 進行解碼,將結果存放到另一個陣列。
@Test
public void decodeWithDst() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
int size = 100;
byte[] dst = new byte[size];
int result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
String stringResult = new String(dst);
System.out.println(stringResult);
assertThat(stringResult).containsIgnoringWhitespaces("https://www.ruoxue.org/?s=base64").hasSize(size);
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=
32
https://www.ruoxue.org/?s=base64
decodeWithByteBuffer
Base64 UrlDecoder Methods Java 建立一個 String ,轉成 byte[] 包裝成 ByteBuffer ,使用 Base64.UrlDecoder 進行解碼,傳回 ByteBuffer 。
@Test
public void decodeWithByteBuffer() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.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("https://www.ruoxue.org/?s=base64");
}
java.nio.HeapByteBuffer[pos=0 lim=44 cap=44]
java.nio.HeapByteBuffer[pos=0 lim=32 cap=32]
https://www.ruoxue.org/?s=base64
decodeWithString
Base64 UrlDecoder Methods 建立一個 String ,使用 Base64.UrlDecoder 進行解碼。
@Test
public void decodeWithString() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
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("https://www.ruoxue.org/?s=base64");
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=
[B@6aa8ceb6
https://www.ruoxue.org/?s=base64
Base64_decodeBase64
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行解碼。
@Test
public void Base64_decodeBase64() throws Exception {
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.decodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=
[B@4c98385c
https://www.ruoxue.org/?s=base64
Base64_decodeBase64WithString
建立一個 String ,轉成 byte[] 使用 Apache Commons Codec 套件 Base64 進行解碼。
@Test
public void Base64_decodeBase64WithString() throws Exception {
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=
[B@6bf2d08e
https://www.ruoxue.org/?s=base64
Base64UrlDecoderMethodsTest.java
Base64 UrlDecoder Methods in Java 新增單元測試,驗證 Base64 UrlDecoder Functions in Java 是否符合預期。
package org.ruoxue.java_147.base64.url;
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 Base64UrlDecoderMethodsTest {
@Test
public void decode() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
@Test
public void decodeWithoutPadding() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ";
System.out.println(value);
byte[] result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
@Test
public void decodeWithDst() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
int size = 100;
byte[] dst = new byte[size];
int result = decoder.decode(value.getBytes(StandardCharsets.UTF_8.toString()), dst);
System.out.println(result);
String stringResult = new String(dst);
System.out.println(stringResult);
assertThat(stringResult).containsIgnoringWhitespaces("https://www.ruoxue.org/?s=base64").hasSize(size);
}
@Test
public void decodeWithByteBuffer() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
ByteBuffer byteBuffer = ByteBuffer.wrap(value.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("https://www.ruoxue.org/?s=base64");
}
@Test
public void decodeWithString() throws Exception {
Base64.Decoder decoder = Base64.getUrlDecoder();
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
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("https://www.ruoxue.org/?s=base64");
}
@Test
public void Base64_decodeBase64() throws Exception {
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64
.decodeBase64(value.getBytes(StandardCharsets.UTF_8.toString()));
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
@Test
public void Base64_decodeBase64WithString() throws Exception {
String value = "aHR0cHM6Ly93d3cucnVveHVlLm9yZy8_cz1iYXNlNjQ=";
System.out.println(value);
byte[] result = org.apache.commons.codec.binary.Base64.decodeBase64(value);
System.out.println(result);
String stringResult = new String(result);
System.out.println(stringResult);
assertThat(stringResult).isEqualTo("https://www.ruoxue.org/?s=base64");
}
}
心得分享
Base64 UrlDecoder Functions in Java 工具類別提供了一套靜態方法取得 URL 解碼器,依照 RFC 4648 中指定的 Base64 字母表,將一組字元A-Za-z0-9_- ,解碼成二進位資料,熟悉 Base64 UrlDecoder Methods in Java 這些方法的操作,提升開發效率,在應用上相當廣泛。