Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
356eaef2
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
356eaef2
编写于
12月 11, 2015
作者:
J
Juergen Hoeller
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #935 from garyrussell/SPR-13784
Base64Utils: URL/File Safe Alphabet Issue: SPR-13784
上级
aaffc236
14fc6c2a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
120 addition
and
0 deletion
+120
-0
spring-core/src/main/java/org/springframework/util/Base64Utils.java
...e/src/main/java/org/springframework/util/Base64Utils.java
+98
-0
spring-core/src/test/java/org/springframework/util/Base64UtilsTests.java
.../test/java/org/springframework/util/Base64UtilsTests.java
+22
-0
未找到文件。
spring-core/src/main/java/org/springframework/util/Base64Utils.java
浏览文件 @
356eaef2
...
...
@@ -18,6 +18,7 @@ package org.springframework.util;
import
java.nio.charset.Charset
;
import
java.util.Base64
;
import
javax.xml.bind.DatatypeConverter
;
import
org.springframework.lang.UsesJava8
;
...
...
@@ -30,8 +31,14 @@ import org.springframework.lang.UsesJava8;
* Codec present, {@link #encode}/{@link #decode} calls will throw an IllegalStateException.
* However, as of Spring 4.2, {@link #encodeToString} and {@link #decodeFromString} will
* nevertheless work since they can delegate to the JAXB DatatypeConverter as a fallback.
* However, this does not apply when using the ...UrlSafe... methods for RFC 4648 "URL and
* Filename Safe Alphabet"; a delegate is required.
* <p>
* <em>Note:</em> Apache Commons Codec does not add padding ({@code =}) when encoding with
* the URL and Filename Safe Alphabet.
*
* @author Juergen Hoeller
* @author Gary Russell
* @since 4.1
* @see java.util.Base64
* @see org.apache.commons.codec.binary.Base64
...
...
@@ -92,6 +99,32 @@ public abstract class Base64Utils {
return
delegate
.
decode
(
src
);
}
/**
* Base64-encode the given byte array using the RFC 4868
* "URL and Filename Safe Alphabet".
* @param src the original byte array (may be {@code null})
* @return the encoded byte array (or {@code null} if the input was {@code null})
* @throws IllegalStateException if Base64 encoding between byte arrays is not
* supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
*/
public
static
byte
[]
encodeUrlSafe
(
byte
[]
src
)
{
assertDelegateAvailable
();
return
delegate
.
encodeUrlSafe
(
src
);
}
/**
* Base64-decode the given byte array using the RFC 4868
* "URL and Filename Safe Alphabet".
* @param src the encoded byte array (may be {@code null})
* @return the original byte array (or {@code null} if the input was {@code null})
* @throws IllegalStateException if Base64 encoding between byte arrays is not
* supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
*/
public
static
byte
[]
decodeUrlSafe
(
byte
[]
src
)
{
assertDelegateAvailable
();
return
delegate
.
decodeUrlSafe
(
src
);
}
/**
* Base64-encode the given byte array to a String.
* @param src the original byte array (may be {@code null})
...
...
@@ -139,18 +172,50 @@ public abstract class Base64Utils {
}
}
/**
* Base64-encode the given byte array to a String using the RFC 4868
* "URL and Filename Safe Alphabet".
* @param src the original byte array (may be {@code null})
* @return the encoded byte array as a UTF-8 String
* (or {@code null} if the input was {@code null})
* @throws IllegalStateException if Base64 encoding between byte arrays is not
* supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
*/
public
static
String
encodeToUrlSafeString
(
byte
[]
src
)
{
assertDelegateAvailable
();
return
new
String
(
delegate
.
encodeUrlSafe
(
src
),
DEFAULT_CHARSET
);
}
/**
* Base64-decode the given byte array from an UTF-8 String using the RFC 4868
* "URL and Filename Safe Alphabet".
* @param src the encoded UTF-8 String (may be {@code null})
* @return the original byte array (or {@code null} if the input was {@code null})
* @throws IllegalStateException if Base64 encoding between byte arrays is not
* supported, i.e. neither Java 8 nor Apache Commons Codec is present at runtime
*/
public
static
byte
[]
decodeFromUrlSafeString
(
String
src
)
{
assertDelegateAvailable
();
return
delegate
.
decodeUrlSafe
(
src
.
getBytes
(
DEFAULT_CHARSET
));
}
interface
Base64Delegate
{
byte
[]
encode
(
byte
[]
src
);
byte
[]
decode
(
byte
[]
src
);
byte
[]
encodeUrlSafe
(
byte
[]
src
);
byte
[]
decodeUrlSafe
(
byte
[]
src
);
}
@UsesJava8
static
class
JdkBase64Delegate
implements
Base64Delegate
{
@Override
public
byte
[]
encode
(
byte
[]
src
)
{
if
(
src
==
null
||
src
.
length
==
0
)
{
return
src
;
...
...
@@ -158,12 +223,30 @@ public abstract class Base64Utils {
return
Base64
.
getEncoder
().
encode
(
src
);
}
@Override
public
byte
[]
decode
(
byte
[]
src
)
{
if
(
src
==
null
||
src
.
length
==
0
)
{
return
src
;
}
return
Base64
.
getDecoder
().
decode
(
src
);
}
@Override
public
byte
[]
encodeUrlSafe
(
byte
[]
src
)
{
if
(
src
==
null
||
src
.
length
==
0
)
{
return
src
;
}
return
Base64
.
getUrlEncoder
().
encode
(
src
);
}
@Override
public
byte
[]
decodeUrlSafe
(
byte
[]
src
)
{
if
(
src
==
null
||
src
.
length
==
0
)
{
return
src
;
}
return
Base64
.
getUrlDecoder
().
decode
(
src
);
}
}
...
...
@@ -171,13 +254,28 @@ public abstract class Base64Utils {
private
final
org
.
apache
.
commons
.
codec
.
binary
.
Base64
base64
=
new
org
.
apache
.
commons
.
codec
.
binary
.
Base64
();
private
final
org
.
apache
.
commons
.
codec
.
binary
.
Base64
base64UrlSafe
=
new
org
.
apache
.
commons
.
codec
.
binary
.
Base64
(
0
,
null
,
true
);
@Override
public
byte
[]
encode
(
byte
[]
src
)
{
return
this
.
base64
.
encode
(
src
);
}
@Override
public
byte
[]
decode
(
byte
[]
src
)
{
return
this
.
base64
.
decode
(
src
);
}
@Override
public
byte
[]
encodeUrlSafe
(
byte
[]
src
)
{
return
this
.
base64UrlSafe
.
encode
(
src
);
}
@Override
public
byte
[]
decodeUrlSafe
(
byte
[]
src
)
{
return
this
.
base64UrlSafe
.
decode
(
src
);
}
}
}
spring-core/src/test/java/org/springframework/util/Base64UtilsTests.java
浏览文件 @
356eaef2
...
...
@@ -55,6 +55,17 @@ public class Base64UtilsTests {
assertArrayEquals
(
jdkDelegate
.
encode
(
bytes
),
commonsDelegate
.
encode
(
bytes
));
assertArrayEquals
(
bytes
,
jdkDelegate
.
decode
(
jdkDelegate
.
encode
(
bytes
)));
assertArrayEquals
(
bytes
,
commonsDelegate
.
decode
(
commonsDelegate
.
encode
(
bytes
)));
bytes
=
new
byte
[]
{
(
byte
)
0xfb
,
(
byte
)
0xf0
};
assertArrayEquals
(
"+/A="
.
getBytes
(),
jdkDelegate
.
encode
(
bytes
));
assertArrayEquals
(
"+/A="
.
getBytes
(),
commonsDelegate
.
encode
(
bytes
));
assertArrayEquals
(
bytes
,
jdkDelegate
.
decode
(
jdkDelegate
.
encode
(
bytes
)));
assertArrayEquals
(
bytes
,
commonsDelegate
.
decode
(
commonsDelegate
.
encode
(
bytes
)));
assertArrayEquals
(
"-_A="
.
getBytes
(),
jdkDelegate
.
encodeUrlSafe
(
bytes
));
assertArrayEquals
(
"-_A"
.
getBytes
(),
commonsDelegate
.
encodeUrlSafe
(
bytes
));
// no padding with commons and URL safe
assertArrayEquals
(
bytes
,
jdkDelegate
.
decodeUrlSafe
(
jdkDelegate
.
encodeUrlSafe
(
bytes
)));
assertArrayEquals
(
bytes
,
commonsDelegate
.
decodeUrlSafe
(
commonsDelegate
.
encodeUrlSafe
(
bytes
)));
}
@Test
...
...
@@ -79,6 +90,17 @@ public class Base64UtilsTests {
assertEquals
(
Base64Utils
.
encodeToString
(
bytes
),
DatatypeConverter
.
printBase64Binary
(
bytes
));
assertArrayEquals
(
bytes
,
Base64Utils
.
decodeFromString
(
Base64Utils
.
encodeToString
(
bytes
)));
assertArrayEquals
(
bytes
,
DatatypeConverter
.
parseBase64Binary
(
DatatypeConverter
.
printBase64Binary
(
bytes
)));
}
@Test
public
void
encodeDecodeUrlSafe
()
{
byte
[]
bytes
=
new
byte
[]
{
(
byte
)
0xfb
,
(
byte
)
0xf0
};
assertArrayEquals
(
"-_A="
.
getBytes
(),
Base64Utils
.
encodeUrlSafe
(
bytes
));
assertArrayEquals
(
bytes
,
Base64Utils
.
decodeUrlSafe
(
Base64Utils
.
encodeUrlSafe
(
bytes
)));
assertEquals
(
"-_A="
,
Base64Utils
.
encodeToUrlSafeString
(
bytes
));
assertArrayEquals
(
bytes
,
Base64Utils
.
decodeFromUrlSafeString
(
Base64Utils
.
encodeToUrlSafeString
(
bytes
)));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录