问题
Background
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
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
?
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.
回答1:
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
.
So let's check that edge case again using the Apache Commons Codec library.
Edge-case test
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));
}
}
This then results in the output
enc1 = <AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
>
enc2 = <AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>
false
Conclusion
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.
来源:https://stackoverflow.com/questions/61863542/is-apache-commons-codec-base64-a-drop-in-replacement-for-sun-misc-base64