Decode Base64 data in Java
As of v6, Java SE ships with JAXB. javax.xml.bind.DatatypeConverter
has static methods that make this easy. See parseBase64Binary()
and printBase64Binary()
.
UPDATE: JAXB is no longer shipped with Java (since Java 11). If JAXB is required for your project, you will need to configure the relevant libraries via your dependency management system, for example Maven. If you require the compiler (xjc.exe
) you also need to download that separately.
Base64 Java encode and decode a string
You can use following approach:
import org.apache.commons.codec.binary.Base64;
// Encode data on your side using BASE64
byte[] bytesEncoded = Base64.encodeBase64(str.getBytes());
System.out.println("encoded value is " + new String(bytesEncoded));
// Decode data on other side, by processing encoded data
byte[] valueDecoded = Base64.decodeBase64(bytesEncoded);
System.out.println("Decoded value is " + new String(valueDecoded));
Hope this answers your doubt.
encode/decode file in java with Base64
You are not closing the files. Also there is the mentioned problem when you use text (String/Reader/Writer) for binary data: corrupt data, slower, double memory, platform dependent when not specifying the encoding.
The optimal solution is not to take the bytes in memory, additionally making a 8/5 larger byte array with base 64.
Use try-with-resources to automatically close the files, even on an exception (like illegal Base 64 chars).
public static void encodeFile(String inputFile, String outputFile)
throws IOException {
Path inPath = Paths.get(inputFile);
Path outPath = Paths.get(outputFile);
try (OutputStream out = Base64.getEncoder().wrap(Files.newOutputStream(outPath))) {
Files.copy(inPath, out);
}
}
public static void decodeFile(String encodedfilecontent, String decodedfile)
throws IOException {
Path inPath = Paths.get(encodedfilecontent);
Path outPath = Paths.get(decodedfile);
try (InputStream in = Base64.getDecoder().wrap(Files.newInputStream(inPath))) {
Files.copy(in, outPath);
}
}
Java Base64 decode results differs unexpectedly
You cannot convert arbitrary binary data to a UTF-8 string. UTF-8 is a unicode encoding which follows certain rules (e.g. all multibyte sequences must start with 11 or 10 as high-bits, and the first byte of a multibyte sequence tells the decoder how many bytes are contained in this multibyte sequence)
What you really want is to write the byte array directly, not convert it to a String first:
byte[] data = Base64.getDecoder().decode(line.getBytes());
System.out.write(data);
Base 64 decode to String not as expected
You have an issue with your String conversions - try this instead:
String result = new BigInteger(new String(Base64.getDecoder().decode(encoded.getBytes("UTF-8")))).toString();
By converting the byte array to String using a new String constructor you're getting a simple String representation which BigInteger constructor knows how to parse. The result will be the expected -101001101110
Related Topics
What Does a "Cannot Find Symbol" or "Cannot Resolve Symbol" Error Mean
How to Avoid Java Code in Jsp Files, Using Jsp 2
Concurrent Modification Exception: Adding to an Arraylist
Why Retrieving Google Directions For Android Using Kml Data Is Not Working Anymore
Servlet Returns "Http Status 404 the Requested Resource (/Servlet) Is Not Available"
Relation Between Memory Host and Memory Arguments Xms and Xmx from Java
Distinguishing Between Java Threads and Os Threads
Cannot Create or Edit Android Virtual Devices (Avd) from Eclipse, Adt 22.6
Populating a Listview Using an Arraylist
Rjava Load Error in Rstudio/R After "Upgrading" to Osx Yosemite
Selenium: How to Stop Geckodriver Process Impacting Pc Memory, Without Calling Driver.Quit()
Java Using Much More Memory Than Heap Size (Or Size Correctly Docker Memory Limit)
Virtual Memory Usage from Java Under Linux, Too Much Memory Used
Why Can't I Use Switch Statement on a String
How to Read the Value of a Private Field from a Different Class in Java