RuneHive-Game
Loading...
Searching...
No Matches
ByteBufUtil.kt
Go to the documentation of this file.
1package org.jire.runehiveps
2
3import io.netty.buffer.ByteBuf
4import io.netty.util.ByteProcessor
5import java.nio.charset.Charset
6
7/**
8 * @author Jire
9 */
10object ByteBufUtil {
11
12 private const val STRING_DELIMITER_317 = 10.toByte()
13
14 private val stringByteProcessor317 = ByteProcessor.IndexOfProcessor(STRING_DELIMITER_317)
15
16 @JvmStatic
17 fun ByteBuf.readStringArray(): ByteArray {
18 val start = readerIndex()
19
20 val end = forEachByte(stringByteProcessor317)
21 require(end != -1) {
22 "Unterminated string"
23 }
24
25 val length = end - start
26 val bytes = ByteArray(length)
27 readBytes(bytes)
28
29 return bytes
30 }
31
32 @JvmStatic
33 @JvmOverloads
34 fun ByteBuf.readString(charset: Charset = Charsets.UTF_8): String {
35 val start = readerIndex()
36
37 val end = forEachByte(stringByteProcessor317)
38 require(end != -1) {
39 "Unterminated string"
40 }
41
42 val length = end - start
43 val string = toString(start, length, charset)
44 .intern()
45
46 readerIndex(end + 1)
47
48 return string
49 }
50
51}