From 29fecec34e05513e0e66048cfff7e1a01fb7260a Mon Sep 17 00:00:00 2001 From: Graham Date: Tue, 25 May 2021 18:54:09 +0100 Subject: [PATCH] Add support for overriding the charset in readString/writeString Signed-off-by: Graham --- .../org/openrs2/buffer/ByteBufExtensions.kt | 8 +-- .../openrs2/buffer/ByteBufExtensionsTest.kt | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/buffer/src/main/kotlin/org/openrs2/buffer/ByteBufExtensions.kt b/buffer/src/main/kotlin/org/openrs2/buffer/ByteBufExtensions.kt index de08587c7c..8f0a5a410e 100644 --- a/buffer/src/main/kotlin/org/openrs2/buffer/ByteBufExtensions.kt +++ b/buffer/src/main/kotlin/org/openrs2/buffer/ByteBufExtensions.kt @@ -95,7 +95,7 @@ public fun ByteBuf.writeUnsignedIntSmart(v: Int): ByteBuf { return this } -public fun ByteBuf.readString(): String { +public fun ByteBuf.readString(charset: Charset = Cp1252Charset): String { val start = readerIndex() val end = forEachByte(ByteProcessor.FIND_NUL) @@ -103,13 +103,13 @@ public fun ByteBuf.readString(): String { "Unterminated string" } - val s = toString(start, end - start, Cp1252Charset) + val s = toString(start, end - start, charset) readerIndex(end + 1) return s } -public fun ByteBuf.writeString(s: CharSequence): ByteBuf { - writeCharSequence(s, Cp1252Charset) +public fun ByteBuf.writeString(s: CharSequence, charset: Charset = Cp1252Charset): ByteBuf { + writeCharSequence(s, charset) writeByte(0) return this } diff --git a/buffer/src/test/kotlin/org/openrs2/buffer/ByteBufExtensionsTest.kt b/buffer/src/test/kotlin/org/openrs2/buffer/ByteBufExtensionsTest.kt index e3a5e25d1c..f745b5deac 100644 --- a/buffer/src/test/kotlin/org/openrs2/buffer/ByteBufExtensionsTest.kt +++ b/buffer/src/test/kotlin/org/openrs2/buffer/ByteBufExtensionsTest.kt @@ -2,6 +2,7 @@ package org.openrs2.buffer import io.netty.buffer.ByteBufAllocator import io.netty.buffer.Unpooled +import org.openrs2.util.charset.ModifiedUtf8Charset import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -433,6 +434,33 @@ class ByteBufExtensionsTest { } } + @Test + fun testReadStringCharset() { + wrappedBuffer(0).use { buf -> + assertEquals("", buf.readString(ModifiedUtf8Charset)) + assertFalse(buf.isReadable) + } + + wrappedBuffer( + 0xC3.toByte(), + 0x96.toByte(), + 'p'.code.toByte(), + 'e'.code.toByte(), + 'n'.code.toByte(), + 'R'.code.toByte(), + 'S'.code.toByte(), + '2'.code.toByte(), + 0 + ).use { buf -> + assertEquals("ÖpenRS2", buf.readString(ModifiedUtf8Charset)) + assertFalse(buf.isReadable) + } + + assertFailsWith { + Unpooled.EMPTY_BUFFER.readString(ModifiedUtf8Charset) + } + } + @Test fun testWriteString() { ByteBufAllocator.DEFAULT.buffer().use { actual -> @@ -461,6 +489,35 @@ class ByteBufExtensionsTest { } } + @Test + fun testWriteStringCharset() { + ByteBufAllocator.DEFAULT.buffer().use { actual -> + actual.writeString("", ModifiedUtf8Charset) + + wrappedBuffer(0).use { expected -> + assertEquals(expected, actual) + } + } + + ByteBufAllocator.DEFAULT.buffer().use { actual -> + actual.writeString("ÖpenRS2", ModifiedUtf8Charset) + + wrappedBuffer( + 0xC3.toByte(), + 0x96.toByte(), + 'p'.code.toByte(), + 'e'.code.toByte(), + 'n'.code.toByte(), + 'R'.code.toByte(), + 'S'.code.toByte(), + '2'.code.toByte(), + 0 + ).use { expected -> + assertEquals(expected, actual) + } + } + } + @Test fun testReadVersionedString() { wrappedBuffer(0, 0).use { buf ->