Skip to content

Instantly share code, notes, and snippets.

@bdarfler
Last active February 20, 2020 14:38
Show Gist options
  • Save bdarfler/d080cf34f35df6c687cd5ee5a3c899f4 to your computer and use it in GitHub Desktop.
Save bdarfler/d080cf34f35df6c687cd5ee5a3c899f4 to your computer and use it in GitHub Desktop.
import io.Codec
import java.nio.ByteBuffer
val UTF8 = “UTF-8”
val ISO8859 = “ISO-8859–1”
val REPLACEMENT_CHAR = ‘\uFFFD’
def bytesToString(bytes: Array[Byte], encoding: String) = {
val upper = encoding.toUpperCase
val codec = if (ISO8859 == upper) Codec.ISO8859 else Codec.UTF8
val decoded = codec.decode(ByteBuffer.wrap(bytes)).toString
if (!decoded.contains(REPLACEMENT_CHAR)) {
decoded
} else {
val otherCodec = if (ISO8859 == upper) Codec.UTF8 else Codec.ISO8859
val otherDecoded = otherCodec.decode(ByteBuffer.wrap(bytes)).toString
if (!otherDecoded.contains(REPLACEMENT_CHAR)) {
otherDecoded
} else {
val utf8 = if (ISO8859 == upper) otherDecoded else decoded
utf8.replace(REPLACEMENT_CHAR, ‘?’)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment