UserMan
UserMan

Reputation: 471

Java Special Characters in Base64 encode

I'm trying to use a basic64 encoding in my Java program.

The problem is that when I use

new sun.misc.BASE64Encoder().encode("my string".getBytes())

I receive the base 64 string with \n and \r. I don't know how avoid this because when I write that string in a text File I get the following

xxxxxxxxxxxxxxxx
yyyy

where the next line is produced by \n and \r.

I tried using a replace("\n","\n") and the same for \r but I want to know if there is a better solution or an extra parameter that I could use in Base64 encoding (or even other tipe of encoding.)

I changed the library and it is working!

here is the difference

Apache Q0FTSCNTVUJJUiMzNWYwMjFiMC1kZWRjLTRmZjgtYjNjMS0wZDY0NmVkMjFmOGUjMjAzMzU1MzczMzY=

Sun FWmsMAID7Ecbf8gUd0WIvKBjJNXhtMCbcKVDW0R4KXY/e1Do8lItqDN4NH/RiBdckoIMeFrncJ5X Fju7R0cX822I/lFSkLab

Weird... I thought that Base64 was a standard...

Upvotes: 0

Views: 3107

Answers (1)

Roman Pustylnikov
Roman Pustylnikov

Reputation: 1932

Why Developers Should Not Write Programs That Call 'sun' Packages

The sun.* packages are not part of the supported, public interface. A Java program that directly calls into sun.* packages is not guaranteed to work on all Java-compatible platforms. In fact, such a program is not guaranteed to work even in future versions on the same platform.

Consider this:

probably somthing like this:

import org.apache.commons.codec.binary.Base64;

Upvotes: 1

Related Questions