Java - general

Base64 encoding/decoding

javax.mail.internet.MimeUtility
To encode/decode Base64 from/to ByteArray you can use the mail API.



public class Base64 {
   public String encode(byte[] ba) throws MessagingException, IOException  {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      OutputStream os = MimeUtility.encode(baos, "Base64");
      os.write(ba);
      os.close();
      return new String(baos.toByteArray());
   }
 
   public byte[] decode(String str) throws MessagingException, IOException  {
      ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
      InputStream is = MimeUtility.decode(bais, "Base64");
      byte[] ba = new byte[str.length()];
      int len = is.read(ba);
      byte[] ba2 = new byte[len];
      System.arraycopy(ba, 0, ba2, 0, n);
      return ba2;
   }
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007