Path originalPath = Paths.get("c:/temp", "mail.txt") If you don’t want to directly work with data and rather prefer to work with streams, you can wrap the output stream such that all data written to this output stream will be automatically base 64 encoded. (new String(decodedByteArray)) username:password 3.3. String encodedString = "dXNlcm5hbWU6cGFzc3dvcmQ=" īase64.Decoder decoder = Base64.getDecoder() īyte decodedByteArray = code(encodedString) Just get the instance of Base64.Decoder and use it to decode the base 64 encoded string. NormalString.getBytes(StandardCharsets.UTF_8) ) dXNlcm5hbWU6cGFzc3dvcmQ= 3.2. String encodedString = encoder.encodeToString( String normalString = "username:password" Base64.Encoder encoder = Base64.getEncoder() This is as simple as getting an instance of the Base64.Encoder and input the string as bytes to encode it. It has two nested classes to work with the encoding and decoding process. Java 8 has added 64 for Base-64 encoding and decoding purposes. The base-64 encoded string containing the above characters is safe to be transferred over the network supporting text data without fear of losing data in a confusion of control characters. Generally, this encoding is done for binary data in email messages and "basic" HTTP authentication. These associated protocols may interpret our binary data as control characters which they are not.īase-64 encoding converts our binary data into a string containing characters out of 64 printable characters. Why? Because some media types are designed for streaming text only. When we have some binary data that we want to send across a network, we generally don’t do it by just converting data into a stream of bits in a raw format.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |