生活随笔
收集整理的這篇文章主要介紹了
java8 base64_Java 8中的Base64 –加入乐趣为时不晚
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
java8 base64
最后,Java 8發布了。 最后,有一種執行Base64編碼的標準方法。 長期以來,我們一直依賴于Apache Commons Codec(無論如何還是很棒的)。 內存敏感的編碼人員將拼命使用sun.misc.BASE64Encoder和sun.misc.BASE64Decoder,以避免在其程序中添加額外的JAR文件,前提是他們確信只使用Sun / Oracle JDK。 這些類仍在Java 8中潛伏。
為了進行試驗,我提供了一個JUnit測試,以顯示如何使用以下API進行編碼:
- 公用編解碼器:org.apache.commons.codec.binary.Base64
- Java 8的新java.util.Base64
- Sun / Oracle JDK的一種常綠內部代碼: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似乎是一個很小的方法,因此擰緊它的方法很少,但是您永遠不會知道表面之下的內容。 從一般的時間安排(在JUnit測試中)看來,可以將3種方法排列成這樣,從最快到最慢:Java 8,Commons,Sun。 時間示例(編碼大小為5,000,000的字節數組):
太陽:521
公地:160
Java8:37
Java 8的運行速度比Commons快4倍,比Sun快14倍。 但是此示例只是簡單化。 一定要為自己建立基準,以得出自己的結論。
那么,要使用哪些API? 正如任何專家都會告訴您的那樣……要視情況而定。 如果您有足夠的能力來決定您的代碼只能在Java 8及更高版本上運行,那么請務必使用新的java.util.Base64。 如果只需要支持多個JDK版本和供應商,則可以使用Commons Codec或其他一些第三方API。 或者等到較舊的Java不再發行或使用后,再重寫您寶貴的代碼庫。 或繼續使用另一種編程語言。
注意:我什至沒有提到使用sun.misc.BASE64Encoder。 盡可能避免使用它。 也許有一天,該類將在另一個(alos)JDK版本中刪除……其他供應商在其他(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
java8 base64
總結
以上是生活随笔為你收集整理的java8 base64_Java 8中的Base64 –加入乐趣为时不晚的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。