Several methods of base64 encoding, Encode and Decode encoding
Base64 is a method that can combine any Binary data into a string with 64 characters, and the Binary data and string data can be converted to each other, which is very convenient. In practical application, Base64 can not only visualize Binary data, but also commonly used to represent the encrypted content of strings. If you want to use Java programming language to implement the encoding and decoding function of Base64, you can refer to the practice of this article.
Early practice
In the early days, the encoding and decoding of Base64 on Java will be used in the JDK sun BASE64Encoder and BASE64Decoder under misc suite are used as follows
final BASE64Encoder encoder = new BASE64Encoder(); final BASE64Decoder decoder = new BASE64Decoder(); final String text = "String text"; final byte[] textByte = text.getBytes("UTF-8"); //code final String encodedText = encoder.encode(textByte); System.out.println(encodedText); //decode System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8")); final BASE64Encoder encoder = new BASE64Encoder(); final BASE64Decoder decoder = new BASE64Decoder(); final String text = "String text"; final byte[] textByte = text.getBytes("UTF-8"); //code final String encodedText = encoder.encode(textByte); System.out.println(encodedText); //decode System.out.println(new String(decoder.decodeBuffer(encodedText), "UTF-8"));
It can be found from the above program that it is not difficult to use Base64 in Java. It can be solved in a few lines of code! Just this sun The base64 function provided by the MIS C suite is not very efficient in encoding and decoding, and may not be supported in future Java versions. It is not recommended to use it at all.
Apache Commons Codec approach
Apache Commons Codec provides Base64 encoding and decoding functions, and will use org apache. commons. codec. The base64 category under binary suite is used as follows:
final Base64 base64 = new Base64(); final String text = "String text"; final byte[] textByte = text.getBytes("UTF-8"); //code final String encodedText = base64.encodeToString(textByte); System.out.println(encodedText); //decode System.out.println(new String(base64.decode(encodedText), "UTF-8")); final Base64 base64 = new Base64(); final String text = "String text"; final byte[] textByte = text.getBytes("UTF-8"); //code final String encodedText = base64.encodeToString(textByte); System.out.println(encodedText); //decode System.out.println(new String(base64.decode(encodedText), "UTF-8"));
The above code looks better than the earlier use of sun The MIS C suite is even more streamlined, and the actual performance is much faster. The disadvantage is that you need to reference Apache common codec, which is very troublesome.
Practice after Java 8
Java 8 In the util suite, the base64 category is added to handle Base64 encoding and decoding. The usage is as follows:
final Base64.Decoder decoder = Base64.getDecoder(); final Base64.Encoder encoder = Base64.getEncoder(); final String text = "String text"; final byte[] textByte = text.getBytes("UTF-8"); //code final String encodedText = encoder.encodeToString(textByte); System.out.println(encodedText); //decode System.out.println(new String(decoder.decode(encodedText), "UTF-8")); final Base64.Decoder decoder = Base64.getDecoder(); final Base64.Encoder encoder = Base64.getEncoder(); final String text = "String text"; final byte[] textByte = text.getBytes("UTF-8"); //code final String encodedText = encoder.encodeToString(textByte); System.out.println(encodedText); //decode System.out.println(new String(decoder.decode(encodedText), "UTF-8"));
With sun Compared with the base64 codec provided by Apache Commons Codec, the base64 provided by Java 8 has better performance. In the actual test of encoding and decoding speed, the base64 provided by Java 8 is better than sun The MIS C suite is at least 11 times faster than that provided by Apache Commons Codec, and at least 3 times faster than that provided by Apache Commons Codec. Therefore, if you want to use Base64 on Java, the Java under Java 8 The base64 category provided by the util suite is absolutely preferred