在 Java 中,我们发现类 Base64
和 BASE64Encoder
通过
Base64.getEncoder.encodeToString(someByteArray);
和分别:
new BASE64Encoder().encode(someByteArray);
我正在尝试对图像文件进行base64编码,这将返回一个相对较大的字符串(6k-7k字符长的base64)
是否存在使用其中一种高于另一种的惯例,是否有任何性能差异或其他原因而使用其中一种高于另一种?
最佳答案
使用Base64.getEncoder()
。 sun
软件包(其中 sun.misc.BASE64Encoder
所在)不受支持,不鼓励使用它们。
参见Why Developers Should Not Write Programs that Call 'sun' Packages :
The sun.* packages are not part of the supported, public interface.
A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.
Eclipse 可能已经向您显示访问限制
错误以及这些包中的大多数类 won't be accessible from Java 9 on .
Performance-wise it also seems advisable to use the new implementation ,虽然我自己没有做过任何测试:
Everything is evident from the table: appalling performance of sun.misc classes, acceptable performance of Commons Codec classes and very good performance of both iharder.net and MiG Base64 implementation. New Java 8 encoder/decoder is running faster than any other implementation (but you need Java 8...). javax.xml class turned out to be a hidden gem!
关于java - 我应该使用 new BASE64Encoder() 还是 Base64.getEncoder() 在 java 中对图像进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48288897/