Java 8 - Base64


With Java 8, Base64 has at long last got its due. Java 8 currently has inbuilt encoder and decoder for Base64 encoding. In Java 8, we can utilize three sorts of Base64 encoding.

  • Simple − Output is mapped to a lot of characters lying in A-Za-z0-9+/. The encoder does not include any line feed in output, and the decoder rejects any character other than A-Za-z0-9+/.

  • URL − Output is mapped to set of characters lying in A-Za-z0-9+_. output is URL and filename safe.

  • MIME − Output is mapped to MIME inviting configuration. output is represented to in lines of close to 76 characters each, and utilizes a carriage return '\r' trailed by a linefeed '\n' as the line separator. No line separator is available as far as possible of the encoded output.

Methods Inherited

This class acquires strategies from the accompanying class −

  • java.lang.Object

Base64 Example

Create the accompanying Java program utilizing any editorial manager of your decision in state C:/> JAVA.

 

 

import java.util.Base64; 

import java.util.UUID; 

import java.io.UnsupportedEncodingException; 

open class HelloWorld { 

open static void main(String args[]) { 

attempt { 

/Encode utilizing essential encoder 

String base64encodedString = Base64.getEncoder().encodeToString( 

"TutorialsPoint?java8".getBytes("utf-8")); 

System.out.println("Base64 Encoded String (Basic) :" + base64encodedString); 

/Decode 

byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString); 

System.out.println("Original String: " + new String(base64decodedBytes, "utf-8")); 

base64encodedString = Base64.getUrlEncoder().encodeToString( 

"TutorialsPoint?java8".getBytes("utf-8")); 

System.out.println("Base64 Encoded String (URL) :" + base64encodedString); 

stringBuilder = new StringBuilder(); 

for (int I = 0; I < 10; ++i) { 

stringBuilder.append(UUID.randomUUID().toString()); 

} 

byte[] mimeBytes = stringBuilder.toString().getBytes("utf-8"); 

String mimeEncodedString = Base64.getMimeEncoder().encodeToString(mimeBytes); 

System.out.println("Base64 Encoded String (MIME) :" + mimeEncodedString); 

} catch(UnsupportedEncodingException e) { 

System.out.println("Error :" + e.getMessage()); 

} 

} 

} 

 

Run precedent » It should create the accompanying output −
Base64 Encoded String (Basic) :VHV0b3JpYWxzUG9pbnQ/amF2YTg= 

Unique String: TutorialsPoint?java8 

Base64 Encoded String (URL) :VHV0b3JpYWxzUG9pbnQ_amF2YTg= 

Base64 Encoded String (MIME) :YmU3NWY2ODktNGM5YS00ODlmLWI2MTUtZTVkOTk2YzQ1Njk1Y2EwZTg2OTEtMmRiZC00YTQ1LWJl 

NTctMTI1MWUwMTk0ZWQyNDE0NDAwYjgtYTYxOS00NDY5LTllYTctNjc1YzE3YWJhZTk1MTQ2MDQz 

NDItOTAyOC00ZWI0LThlOTYtZWU5YzcwNWQyYzVhMTQxMWRjYTMtY2MwNi00MzU0LTg0MTgtNGQ1 

MDkwYjdiMzg2ZTY0OWU5MmUtZmNkYS00YWEwLTg0MjQtYThiOTQxNDQ2YzhhNTVhYWExZjItNjU2 

Mi00YmM4LTk2ZGYtMDE4YmY5ZDZhMjkwMzM3MWUzNDMtMmQ3MS00MDczLWI0Y2UtMTQxODE0MGU5 

YjdmYTVlODUxYzItN2NmOS00N2UyLWIyODQtMThlMWVkYTY4M2Q1YjE3YTMyYmItZjllMS00MTFk 

LWJiM2UtM2JhYzUxYzI5OWI4 






javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition

JAVACODEGEEKS
EmberJS Tutorial