with java 8, base64 has finally got its due. java 8 now has inbuilt encoder and decoder for base64 encoding. in java 8, we can use three types of base64 encoding.
simple − output is mapped to a set of characters lying in a-za-z0-9+/. the encoder does not add 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 friendly format. output is represented in lines of no more than 76 characters each, and uses a carriage return '\r' followed by a linefeed '\n' as the line separator. no line separator is present to the end of the encoded output.
nested classes
sr.no. | nested class & description |
---|---|
1 |
static class base64.decoder this class implements a decoder for decoding byte data using the base64 encoding scheme as specified in rfc 4648 and rfc 2045. |
2 |
static class base64.encoder this class implements an encoder for encoding byte data using the base64 encoding scheme as specified in rfc 4648 and rfc 2045. |
methods
sr.no. | method name & description |
---|---|
1 |
static base64.decoder getdecoder() returns a base64.decoder that decodes using the basic type base64 encoding scheme. |
2 |
static base64.encoder getencoder() returns a base64.encoder that encodes using the basic type base64 encoding scheme. |
3 |
static base64.decoder getmimedecoder() returns a base64.decoder that decodes using the mime type base64 decoding scheme. |
4 |
static base64.encoder getmimeencoder() returns a base64.encoder that encodes using the mime type base64 encoding scheme. |
5 |
static base64.encoder getmimeencoder(int linelength, byte[] lineseparator) returns a base64.encoder that encodes using the mime type base64 encoding scheme with specified line length and line separators. |
6 |
static base64.decoder geturldecoder() returns a base64.decoder that decodes using the url and filename safe type base64 encoding scheme. |
7 |
static base64.encoder geturlencoder() returns a base64.encoder that encodes using the url and filename safe type base64 encoding scheme. |
methods inherited
this class inherits methods from the following class −
- java.lang.object
base64 example
create the following java program using any editor of your choice in say c:/> java.
java8tester.java
import java.util.base64; import java.util.uuid; import java.io.unsupportedencodingexception; public class helloworld { public static void main(string args[]) { try { // encode using basic 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 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()); } } }
verify the result
compile the class using javac compiler as follows −
c:\java>javac java8tester.java
now run the java8tester as follows −
c:\java>java java8tester
it should produce the following output −
base64 encoded string (basic) :vhv0b3jpywxzug9pbnq/amf2ytg= original string: tutorialspoint?java8 base64 encoded string (url) :vhv0b3jpywxzug9pbnq_amf2ytg= base64 encoded string (mime) :ymu3nwy2odktngm5ys00odlmlwi2mtutztvkotk2yzq1njk1y2ewztg2otetmmrizc00ytq1lwjl ntctmti1mwuwmtk0zwqynde0ndawyjgtytyxos00ndy5ltllytctnjc1yze3ywjhztk1mtq2mdqz nditotayoc00zwi0lthlotytzwu5yzcwnwqyyzvhmtqxmwrjytmty2mwni00mzu0ltg0mtgtngq1 mdkwyjdimzg2zty0owu5mmutzmnkys00ywewltg0mjqtythiotqxndq2yzhhntvhywexzjitnju2 mi00ymm4ltk2zgytmde4ymy5zdzhmjkwmzm3mwuzndmtmmq3ms00mdczlwi0y2utmtqxode0mgu5 yjdmytvloduxyzitn2nmos00n2uylwiyodqtmthlmwvkyty4m2q1yje3ytmyymitzjllms00mtfk lwjim2utm2jhyzuxyzi5owi4