Use of Base64 class in JDK7

From the documentation:

Since: 1.8

So no, it is not available in JDK 7.


Base64.getDecoder().decode() is available from Java 1.8

Try to use Google Guava.

pom.xml

<dependency>
   <artifactId>guava</artifactId>
   <groupId>com.google.guava</groupId>
   <type>jar</type>
   <version>14.0.1</version>
</dependency>

Code Snippet

String inputContent = "Hello World";
String base64String = BaseEncoding.base64().encode(inputContent.getBytes("UTF-8"));
//decode
System.out.println("Base64:" + base64String);
byte[] contentInBytes = BaseEncoding.base64().decode(base64String);
System.out.println("Source content: " + new String(contentInBytes, "UTF-8"));//Hello World

If it is needed to specifically use JDK7 for your project, and you still need to use java.util.Base64 class, you can include in your project the code for that class from OpenJDK.

Source for this class is available at:
http://www.grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java?av=f

Base64.java file can be downloaded at:
http://www.grepcode.com/file_/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Base64.java/?v=source&disposition=attachment

Tags:

Java