0
0
mirror of https://github.com/tursom/TursomServer.git synced 2025-04-26 13:01:22 +08:00
This commit is contained in:
tursom 2021-07-13 21:00:11 +08:00
parent 05d90029ae
commit 7045d77c7d
2 changed files with 28 additions and 0 deletions
ts-core/ts-buffer/src/main/kotlin/cn/tursom/core/buffer

View File

@ -89,6 +89,10 @@ interface ByteBuffer : Closeable {
writePosition = 0
}
fun skip(n: Int) {
readPosition += n
}
fun get(): Byte = read { it.get() }
fun getChar(byteOrder: ByteOrder = ByteOrder.nativeOrder()): Char = read { it.char }
fun getShort(byteOrder: ByteOrder = ByteOrder.nativeOrder()): Short = read { it.short }

View File

@ -11,6 +11,8 @@ package cn.tursom.core.buffer
import cn.tursom.core.Unsafe.unsafe
import cn.tursom.core.isStatic
import cn.tursom.core.toInt
import java.nio.ByteOrder
class UnsupportedException : Exception()
@ -206,3 +208,25 @@ fun <T> ByteBuffer.unSerialize(clazz: Class<T>): T {
inline fun <reified T : Any> ByteBuffer.unSerialize(): T {
return unSerialize(T::class.java)
}
fun ByteBuffer.getIntWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Int {
var time = 4
return toInt(byteOrder) {
if (--time < size) {
get()
} else {
0
}
}
}
fun ByteBuffer.getLongWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Int {
var time = 8
return toInt(byteOrder) {
if (--time < size) {
get()
} else {
0
}
}
}