Base64 encoding in Java / Groovy

Apache Commons has many utilities:

Binary Package: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html

Download: http://commons.apache.org/codec/download_codec.cgi


The preferred way to do this in groovy is:

 def encoded = "Hello World".bytes.encodeBase64().toString()
 assert encoded == "SGVsbG8gV29ybGQ="
 def decoded = new String("SGVsbG8gV29ybGQ=".decodeBase64())
 assert decoded == "Hello World"

You could use the open source Base64Coder library

import biz.source_code.base64Coder.Base64Coder

@Grab(group='biz.source_code', module='base64coder', version='2010-09-21')

String s1 = Base64Coder.encodeString("Hello world")
String s2 = Base64Coder.decodeString("SGVsbG8gd29ybGQ=")