生活随笔
收集整理的這篇文章主要介紹了
Java 8中的Base64 –加入乐趣为时不晚
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最后,Java 8發(fā)布了。 最后,有一種執(zhí)行Base64編碼的標(biāo)準(zhǔn)方法。 長期以來,我們一直依賴于Apache Commons Codec(無論如何還是很棒的)。 內(nèi)存敏感的編碼人員將拼命使用sun.misc.BASE64Encoder和sun.misc.BASE64Decoder,以避免在其程序中添加額外的JAR文件,前提是他們確信僅使用Sun / Oracle JDK。 這些類仍在Java 8中潛伏。
為了進(jìn)行試驗(yàn),我提供了一個(gè)JUnit測試,以顯示如何使用以下API進(jìn)行編碼:
- 公用編解碼器:org.apache.commons.codec.binary.Base64
- Java 8的新java.util.Base64
- Sun / Oracle JDK的常綠內(nèi)部代碼:sun.misc.BASE64Encoder
package org.gizmo.util;import java.util.Random;import org.apache.commons.codec.binary.Base64;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;import sun.misc.BASE64Encoder;public class Base64Tests {private static byte[] randomBinaryData = new byte[5000000];private static long durationCommons = 0;private static long durationJava8 = 0;private static long durationSun = 0;private static byte[] encodedCommons;private static byte[] encodedJava8;private static String encodedSun;@BeforeClasspublic static void setUp() throws Exception {//We want to test the APIs against the same datanew Random().nextBytes(randomBinaryData); }@Testpublic void testSunBase64Encode() throws Exception {BASE64Encoder encoder = new BASE64Encoder();long before = System.currentTimeMillis();encodedSun = encoder.encode(randomBinaryData);long after = System.currentTimeMillis();durationSun = after-before;System.out.println("Sun: " + durationSun);} @Testpublic void testJava8Base64Encode() throws Exception {long before = System.currentTimeMillis();java.util.Base64.Encoder encoder = java.util.Base64.getEncoder();encodedJava8 = encoder.encode(randomBinaryData);long after = System.currentTimeMillis();durationJava8 = after-before;System.out.println("Java8: " + durationJava8);}@Testpublic void testCommonsBase64Encode() throws Exception {long before = System.currentTimeMillis();encodedCommons = Base64.encodeBase64(randomBinaryData);long after = System.currentTimeMillis();durationCommons = after-before;System.out.println("Commons: " + durationCommons);}@AfterClasspublic static void report() throws Exception {//Sanity checkassertArrayEquals(encodedCommons, encodedJava8);System.out.println(durationCommons*1.0/durationJava8);}
}
這三種方式的性能如何? Base64似乎是一個(gè)很小的方法,因此擰緊它的方法很少,但是您永遠(yuǎn)不會(huì)知道表面之下的內(nèi)容。 從一般的時(shí)間安排(在JUnit測試中)看來,可以將3種方法排列成這樣,從最快到最慢:Java 8,Commons,Sun。 時(shí)間示例(編碼大小為5,000,000的字節(jié)數(shù)組):
太陽:521
公地:160
Java8:37
Java 8的方法運(yùn)行速度比Commons快4倍,比Sun快14倍。 但是此示例只是簡單化。 一定要為自己建立基準(zhǔn),以得出自己的結(jié)論。
那么,要使用哪些API? 正如任何專家都會(huì)告訴您的那樣……要視情況而定。 如果您有足夠的能力指示您的代碼只能在Java 8及更高版本上運(yùn)行,則請務(wù)必使用新的java.util.Base64。 如果您只需要支持多個(gè)JDK版本和供應(yīng)商,則可以使用Commons Codec或其他一些第三方API。 或者等到較舊的Java不再發(fā)行或使用后,再重寫您寶貴的代碼庫。 或繼續(xù)使用另一種編程語言。
注意:我什至沒有提到使用sun.misc.BASE64Encoder。 盡可能避免使用它。 也許有一天,該類將在另一個(gè)(alos)JDK版本中刪除……其他供應(yīng)商在其他(heteros)JDK中不提供該類。
資源資源
- http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
- http://stackoverflow.com/questions/13109588/base64-encoding-in-java/22704819#22704819
- http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/binary/Base64.html
翻譯自: https://www.javacodegeeks.com/2014/04/base64-in-java-8-its-not-too-late-to-join-in-the-fun.html
總結(jié)
以上是生活随笔為你收集整理的Java 8中的Base64 –加入乐趣为时不晚的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。