Skip to content

Instantly share code, notes, and snippets.

@goldobin
Last active October 5, 2015 07:41
Show Gist options
  • Save goldobin/5d845ba9df5680597ff5 to your computer and use it in GitHub Desktop.
Save goldobin/5d845ba9df5680597ff5 to your computer and use it in GitHub Desktop.
Base64 codecs for Scala (JDK8)
object Base64Codecs {
private val base64Encoder = Base64.getEncoder
private val base64Decoder = Base64.getDecoder
private def toBase64String(bytes: Array[Byte]): String = {
base64Encoder.encodeToString(bytes)
}
private def parseBase64String(s: String): Array[Byte] = {
base64Decoder.decode(s)
}
implicit class StringWithBase64Methods(s: String) {
def decodeBase64: ByteString = ByteString(base64Decoder.decode(s))
}
implicit class ByteSeqWithMethods(s: Seq[Byte]) {
def encodeBase64: String = base64Encoder.encodeToString(s.toArray)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment