You need to change the import of your class:
import org.apache.commons.codec.binary.Base64;
And then change your class to use the Base64 class.
Here's some example code:
byte[] encodedBytes = Base64.encodeBase64("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));
Then read why you shouldn't use sun.* packages.
Update (2016-12-16)
You can now use java.util.Base64 with Java 8. First, import it as you normally do:
import java.util.Base64;
Then use the Base64 static methods as follows:
byte[] encodedBytes = Base64.getEncoder().encode("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));
If you directly want to encode string and get the result as encoded string, you can use this:
String encodeBytes = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
See Java documentation for Base64 for more.
Answer from Frank on Stack OverflowYou need to change the import of your class:
import org.apache.commons.codec.binary.Base64;
And then change your class to use the Base64 class.
Here's some example code:
byte[] encodedBytes = Base64.encodeBase64("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));
Then read why you shouldn't use sun.* packages.
Update (2016-12-16)
You can now use java.util.Base64 with Java 8. First, import it as you normally do:
import java.util.Base64;
Then use the Base64 static methods as follows:
byte[] encodedBytes = Base64.getEncoder().encode("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));
If you directly want to encode string and get the result as encoded string, you can use this:
String encodeBytes = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());
See Java documentation for Base64 for more.
Use Java 8's never-too-late-to-join-in-the-fun class: java.util.Base64
new String(Base64.getEncoder().encode(bytes));
Will Base64.Encoder.encode(byte[]) behave consistently across platforms?
Help needed to decode base64 string to byte array.
Base64 Encoding Performance: Java vs Rust
Is it a good idea to convert images into base64 string and save them in database?
My coworker and I are building a Java API that expects a certain parameter to be passed in as a byte array that is Base64-encoded in accordance with RFC 4648.
We are currently writing sample Java code to share with integrators that is meant to be illustrative of how they should call the API. My suggestion was that we should tell them to call Base64.Encoder.encode(byte[]) on their pre-encoded payload, and then pass the returned base64-encoded byte array into our API.
My coworker has expressed worry that this may result in behavior that is platform-dependent, with the behavior potentially being different depending on what the default Charset is on whatever platform the integrator's software is running on. He has suggested that we should instead call Base64.Encoder.encodeToString(byte[]), and then convert the returned ISO-8859-1 String to byte[].
To give a concrete example, his worry seems to be that Base64.Encoder.encode(byte[]) could theoretically encode the base64 alphabet sequence ABC as {0, 65, 0, 66, 0, 67} instead of {65, 66, 67} — again, depending on what the default Charset is.
I don't think something like this can happen, for the following reasons:
-
We are starting with a
bytearray and ending with abytearray, soCharsets shouldn't be relevant, as we aren't dealing inStrings at all. -
As I understand, Java is generally platform-independent in the sense that the same code should behave the same way, regardless of what platform it's running on.
Is his concern a valid one, and if so, how should we write our sample code?
Thanks!