Re: Generating a unique string without normal character sets
Jon Gomez wrote:
It is probably a bad idea to use the sun.* packages, since they may be
unavailable on other JDK platforms or may change (or who knows, could
vanish) between JDK versions, at least according to a report by Sun in
1996. Their use is not advised unless you want to take that risk [2].
And why should anyone want to do that when there is a supported
way !?!?
Only drawback is that ones needs a Java EE environment or
getting JavaMail RI.
Code below.
Arne
===========================================
public static String b64encode(byte[] b) throws MessagingException,
IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64os = MimeUtility.encode(baos, "base64");
b64os.write(b);
b64os.close();
return new String(baos.toByteArray());
}
public static byte[] b64decode(String s) throws
MessagingException, IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(s.getBytes());
InputStream b64is = MimeUtility.decode(bais, "Base64");
byte[] tmp = new byte[s.length()];
int n = b64is.read(tmp);
byte[] res = new byte[n];
System.arraycopy(tmp, 0, res, 0, n);
return res;
}