Skip to content

Instantly share code, notes, and snippets.

@irgaly
Created January 22, 2022 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irgaly/e35a0bceb850d0b78741624142324a8e to your computer and use it in GitHub Desktop.
Save irgaly/e35a0bceb850d0b78741624142324a8e to your computer and use it in GitHub Desktop.
//https://www.ne.jp/asahi/hishidama/home/tech/java/Buffer.html#h_CharsetDecoder
val input = "0あいう" +
ubyteArrayOf(0xE9U, 0x82U, 0x8AU, 0xF3U, 0xA0U, 0x84U, 0x80U).toByteArray().toString(Charsets.UTF_8) +
ubyteArrayOf(0xF0U, 0x9FU, 0x98U, 0x8aU).toByteArray().toString(Charsets.UTF_8)
val stream = input.byteInputStream()
val decoder = Charsets.UTF_8.newDecoder()
val byteBuffer = ByteBuffer.allocate(4)
val charBuffer = CharBuffer.allocate(2)
var eof = false
while(!eof) {
val byte = stream.read()
if (0 <= byte) {
byteBuffer.put(byte.toByte())
} else {
eof = true
}
byteBuffer.flip()
var result = decoder.decode(byteBuffer, charBuffer, eof)
if (!result.isUnderflow) {
result.throwException()
}
if (eof) {
result = decoder.flush(charBuffer)
if (!result.isUnderflow) {
result.throwException()
}
}
charBuffer.flip()
while(charBuffer.hasRemaining()) {
val c = charBuffer.get()
println("${c}-[0x${Integer.toHexString(c.code).uppercase()}]")
}
if (byteBuffer.hasRemaining()) {
println("compact")
byteBuffer.compact()
} else {
byteBuffer.clear()
}
charBuffer.clear()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment