Apache Commons Codec Base64是否可以替代sun.misc.BASE64? [英] Is Apache Commons Codec Base64 a drop-in replacement for sun.misc.BASE64?
问题描述
背景
Background
这是我最初的问题的后续问题从sun.misc.BASE64迁移到Java 8 java.util.Base64 ,因为有人建议,因为Apache Commons Codec Base64 API是用于替换使用sun.misc
Base64 API.
This is a follow-up question to my original question Migrating from sun.misc.BASE64 to Java 8 java.util.Base64 because someone suggested that the Apache Commons Codec Base64 API is a drop-in replacement to migrate code which uses the sun.misc
Base64 APIs.
问题
Question
对于不受支持的内部Java API sun.misc.BASE64Encoder
和sun.misc.BASE64Decoder
,org.apache.commons.codec.binary.Base64
是直接替代品 吗?
Is the org.apache.commons.codec.binary.Base64
a drop-in replacement for the unsupported, internal Java API sun.misc.BASE64Encoder
and sun.misc.BASE64Decoder
?
直接替换 意味着两种实现(Apache Commons Codec和sun.misc
)的编码/解码结果均相等,因此可以互换使用
Drop-in replacement means that the encoding/decoding results from both implementations (Apache Commons Codec and sun.misc
) are equal, thus the they can be used interchangebly.
推荐答案
Stuart标记了完美的反例答案 原始问题明确表明,对于至少一种边缘情况,Java 8 java.util.Base64
MIME编码器提供的结果与不受支持的结果不同,内部Java API sun.misc.BASE64Encoder
.
Stuart Marks perfect counter-example answer to my original question shows definitely that for at least one edge case the Java 8 java.util.Base64
MIME Encoder delivers a different result than the unsupported, internal Java API sun.misc.BASE64Encoder
.
因此,让我们使用 Apache Commons Codec库再次检查这种情况.
.边缘测试
Edge-case test
对于以下测试,我使用OpenJDK 7和Apache Commons Codec库1.14的sun.misc.BASE64Encoder
实现.
For the following test I use the sun.misc.BASE64Encoder
implementation of OpenJDK 7 and Apache Commons Codec library 1.14.
public class StuartMarksBase64EncodingEdgeCaseTestForApacheCommonsCodec {
public static void main(String[] args) {
byte[] bytes = new byte[57];
String enc1 = new sun_misc_jdk7.BASE64Encoder().encode(bytes);
String enc2 = new String(org.apache.commons.codec.binary.Base64.encodeBase64(bytes));
System.out.println("enc1 = <" + enc1 + ">");
System.out.println("enc2 = <" + enc2 + ">");
System.out.println(enc1.equals(enc2));
}
}
这将导致输出
enc1 = <AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>
enc2 = <AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>
false
结论
Conclusion
鉴于测试输出,我得出结论,Apache Commons Codec Base64 API是 不是直接替代品 ,以迁移使用sun.misc
Base64 API的代码.
Given the test output I conclude that the Apache Commons Codec Base64 API is not a drop-in replacement to migrate code which uses the sun.misc
Base64 APIs.
这篇关于Apache Commons Codec Base64是否可以替代sun.misc.BASE64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!