My java application use base64 encoding which puts a new line (\n
) after every 76 character. I need to put this encoded string in a properties file and the newline breaks the functionality.
When I do a encodedString.replaceAll("\n", "");
things are working fine, but I just want to make sure that this is expected and I am not introducing a hidden issue.
RFC 2045, which defined Base64, REQUIRES a newline after 76 characters (max). What makes you think your example is not the correct way? @MSalters RFC 4648 specifically addresses that issue.
Need for Padding in Base64:It is mandatory for a proper conversion into Base64 that the resulting data must be converted into sequences of 24 bits each. However, at times, it happens that this length is not satisfied, i.e., a few bits might not be there, or the total bits of the encoded data are fewer than 24.
Encoding to/from Base64 is completely lossless. The quality loss happens probably when you save it. To prevent that, use an ImageWriter directly ( ImageIO.
Base64 only contains A–Z , a–z , 0–9 , + , / and = . So the list of characters not to be used is: all possible characters minus the ones mentioned above. For special purposes .
Breaking a base64 encoded string into multiple lines has been necessary for many old programs that couldn't handle long lines. Programs written in Java can usually handle long lines since they don't need to do the memory management themselves. As long as your lines are shorter than 64 million characters there should be no problem.
And since you don't need the newlines, you shouldn't generate them at all, if possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With