Hanya Sebagai Pengingat

=================================

Kamis, 28 Juli 2011

warning: sun.misc.BASE64Encoder is Sun proprietary API and may be removed in a future release

Base64 is the scheme using which Binary data can be encoded into Base64 representation. Base64 representation uses A–Z, a–z, and 0–9 for the first 62 values and +/ as rest of two values. It is generally used when some binary data need to be sent over textual protocol like XML.
Learn more about Base64
In recent times use of web services has increased for integrations and APIs. Generally these APIs and integrations use SOAP based web services which are again based on XML protocol. If you want to send some binary data as a part of web service request/response which is based on SOAP/XML it will now allow sending it because of its textual nature.
Base64 encoding helps in these scenarios. Other than this you can also use this encryption scheme for encrypting some secure values if you want.
The warning above came while compiling the Java source files which were using Base64Encoder in code. As warning clearly says that it does not encourage the use of Sun implementation of Base64Encoder and gives a warning that the implementation may be removed in future releases, what we can do is to switch to other implementation of Base64 encoder.
We can use Commons Codec library for Base64 Encoder. Following is an example:
1. Add Commons Codec library in classpath of your project
2. Add import statement for Base64 Class.

import org.apache.commons.codec.binary.Base64;

3. Encrypt your data

String testString = "Hello World";
byte[] encodedBytes = Base64.encodeBase64(testString.getBytes());
// Get encoded string
String encodedString = new String(encodedBytes);
// Get decoded string back
String decodedString = new String(Base64.decodeBase64(encodedBytes));


After using Commons codec library, you should not see above warning again.

Tidak ada komentar: