Add method for stripping the version trailer from a ByteBuf

Signed-off-by: Graham <gpe@openrs2.dev>
bzip2
Graham 4 years ago
parent e764403cb2
commit 78f8069177
  1. 16
      cache/src/main/java/dev/openrs2/cache/VersionTrailer.kt
  2. 35
      cache/src/test/java/dev/openrs2/cache/VersionTrailerTest.kt

@ -0,0 +1,16 @@
package dev.openrs2.cache
import io.netty.buffer.ByteBuf
public object VersionTrailer {
public fun strip(buf: ByteBuf): Int? {
return if (buf.readableBytes() >= 2) {
val index = buf.writerIndex() - 2
val version = buf.getUnsignedShort(index)
buf.writerIndex(index)
version
} else {
null
}
}
}

@ -0,0 +1,35 @@
package dev.openrs2.cache
import dev.openrs2.buffer.use
import io.netty.buffer.Unpooled
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
object VersionTrailerTest {
@Test
public fun testStrip() {
assertNull(VersionTrailer.strip(Unpooled.EMPTY_BUFFER))
Unpooled.wrappedBuffer(byteArrayOf(0)).use { buf ->
assertNull(VersionTrailer.strip(buf))
Unpooled.wrappedBuffer(byteArrayOf(0)).use { expected ->
assertEquals(expected, buf)
}
}
Unpooled.wrappedBuffer(byteArrayOf(0x12, 0x34)).use { buf ->
assertEquals(0x1234, VersionTrailer.strip(buf))
assertEquals(Unpooled.EMPTY_BUFFER, buf)
}
Unpooled.wrappedBuffer(byteArrayOf(0x12, 0x34, 0x56)).use { buf ->
assertEquals(0x3456, VersionTrailer.strip(buf))
Unpooled.wrappedBuffer(byteArrayOf(0x012)).use { expected ->
assertEquals(expected, buf)
}
}
}
}
Loading…
Cancel
Save