mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-14 03:40:06 +08:00
add NioBuffers
This commit is contained in:
parent
159fe0ff52
commit
3610737e47
@ -196,7 +196,7 @@ fun ByteArray.toDoubleArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteO
|
||||
|
||||
fun Short.hton(): Short = ntoh()
|
||||
fun Short.ntoh(): Short {
|
||||
return if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
|
||||
return if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
|
||||
this
|
||||
} else {
|
||||
val value = toInt()
|
||||
@ -207,7 +207,7 @@ fun Short.ntoh(): Short {
|
||||
|
||||
fun Int.hton(): Int = ntoh()
|
||||
fun Int.ntoh(): Int {
|
||||
return if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
|
||||
return if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
|
||||
this
|
||||
} else {
|
||||
shr(24) or (shr(16) and 0xff00) or (shr(8) and 0xff0000) or (this and 0xff)
|
||||
@ -217,7 +217,7 @@ fun Int.ntoh(): Int {
|
||||
|
||||
fun Long.hton(): Long = ntoh()
|
||||
fun Long.ntoh(): Long {
|
||||
return if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
|
||||
return if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {
|
||||
this
|
||||
} else {
|
||||
shr(56) or (shr(48) and 0xff00) or (shr(40) and 0xff0000)
|
||||
@ -229,60 +229,53 @@ fun Long.ntoh(): Long {
|
||||
fun ByteArray.put(char: Char, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
val value = char.code
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = value.toByte()
|
||||
this[offset + 1] = (value shr 8).toByte()
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
this[offset + 1] = value.toByte()
|
||||
this[offset] = (value shr 8).toByte()
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = value.toByte()
|
||||
this[offset + 1] = (value shr 8).toByte()
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(short: Short, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
val value = short.toInt()
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = value.toByte()
|
||||
this[offset + 1] = (value shr 8).toByte()
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
this[offset + 1] = value.toByte()
|
||||
this[offset] = (value shr 8).toByte()
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = value.toByte()
|
||||
this[offset + 1] = (value shr 8).toByte()
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(int: Int, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = int.toByte()
|
||||
this[offset + 1] = (int shr 8).toByte()
|
||||
this[offset + 2] = (int shr 16).toByte()
|
||||
this[offset + 3] = (int shr 24).toByte()
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
this[offset + 3] = int.toByte()
|
||||
this[offset + 2] = (int shr 8).toByte()
|
||||
this[offset + 1] = (int shr 16).toByte()
|
||||
this[offset] = (int shr 24).toByte()
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = int.toByte()
|
||||
this[offset + 1] = (int shr 8).toByte()
|
||||
this[offset + 2] = (int shr 16).toByte()
|
||||
this[offset + 3] = (int shr 24).toByte()
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(long: Long, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = long.toByte()
|
||||
this[offset + 1] = (long shr 8).toByte()
|
||||
this[offset + 2] = (long shr 16).toByte()
|
||||
this[offset + 3] = (long shr 24).toByte()
|
||||
this[offset + 4] = (long shr 32).toByte()
|
||||
this[offset + 5] = (long shr 40).toByte()
|
||||
this[offset + 6] = (long shr 48).toByte()
|
||||
this[offset + 7] = (long shr 56).toByte()
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
this[offset + 7] = long.toByte()
|
||||
this[offset + 6] = (long shr 8).toByte()
|
||||
@ -293,6 +286,17 @@ fun ByteArray.put(long: Long, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.
|
||||
this[offset + 1] = (long shr 48).toByte()
|
||||
this[offset] = (long shr 56).toByte()
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = long.toByte()
|
||||
this[offset + 1] = (long shr 8).toByte()
|
||||
this[offset + 2] = (long shr 16).toByte()
|
||||
this[offset + 3] = (long shr 24).toByte()
|
||||
this[offset + 4] = (long shr 32).toByte()
|
||||
this[offset + 5] = (long shr 40).toByte()
|
||||
this[offset + 6] = (long shr 48).toByte()
|
||||
this[offset + 7] = (long shr 56).toByte()
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
@ -674,60 +678,53 @@ inline fun DoubleArray.forEachIndex(fromIndex: Int, toIndex: Int, action: (Doubl
|
||||
inline fun Char.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
val value = code
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(value.toByte())
|
||||
action((value shr 8).toByte())
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
action((value shr 8).toByte())
|
||||
action(value.toByte())
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(value.toByte())
|
||||
action((value shr 8).toByte())
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Short.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
val value = toInt()
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(value.toByte())
|
||||
action((value shr 8).toByte())
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
action((value shr 8).toByte())
|
||||
action(value.toByte())
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(value.toByte())
|
||||
action((value shr 8).toByte())
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Int.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(this.toByte())
|
||||
action((this shr 8).toByte())
|
||||
action((this shr 16).toByte())
|
||||
action((this shr 24).toByte())
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
action((this shr 24).toByte())
|
||||
action((this shr 16).toByte())
|
||||
action((this shr 8).toByte())
|
||||
action(this.toByte())
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(this.toByte())
|
||||
action((this shr 8).toByte())
|
||||
action((this shr 16).toByte())
|
||||
action((this shr 24).toByte())
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Long.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(this.toByte())
|
||||
action((this shr 8).toByte())
|
||||
action((this shr 16).toByte())
|
||||
action((this shr 24).toByte())
|
||||
action((this shr 32).toByte())
|
||||
action((this shr 40).toByte())
|
||||
action((this shr 48).toByte())
|
||||
action((this shr 56).toByte())
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
action((this shr 56).toByte())
|
||||
action((this shr 48).toByte())
|
||||
@ -738,6 +735,17 @@ inline fun Long.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (By
|
||||
action((this shr 8).toByte())
|
||||
action(this.toByte())
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(this.toByte())
|
||||
action((this shr 8).toByte())
|
||||
action((this shr 16).toByte())
|
||||
action((this shr 24).toByte())
|
||||
action((this shr 32).toByte())
|
||||
action((this shr 40).toByte())
|
||||
action((this shr 48).toByte())
|
||||
action((this shr 56).toByte())
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
|
||||
@ -751,38 +759,32 @@ inline fun Double.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (
|
||||
|
||||
inline fun toChar(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Char {
|
||||
return when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() or (get().toInt() shl 8)
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
val late = get()
|
||||
get().toInt() or (late.toInt() shl 8)
|
||||
}
|
||||
else -> {
|
||||
throw UnsupportedOperationException()
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() or (get().toInt() shl 8)
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}.toChar()
|
||||
}
|
||||
|
||||
inline fun toShort(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Short {
|
||||
return when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() or (get().toInt() shl 8)
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
val late = get()
|
||||
get().toInt() or (late.toInt() shl 8)
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() or (get().toInt() shl 8)
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}.toShort()
|
||||
}
|
||||
|
||||
inline fun toInt(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Int {
|
||||
return when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() and 0xff or (get().toInt() shl 8 and 0xff00) or
|
||||
(get().toInt() shl 16 and 0xff0000) or (get().toInt() shl 24 and 0xff000000.toInt())
|
||||
}
|
||||
ByteOrder.BIG_ENDIAN -> {
|
||||
val i1 = get()
|
||||
val i2 = get()
|
||||
@ -790,6 +792,10 @@ inline fun toInt(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte):
|
||||
get().toInt() and 0xff or (i3.toInt() shl 8 and 0xff00) or
|
||||
(i2.toInt() shl 16 and 0xff0000) or (i1.toInt() shl 24 and 0xff000000.toInt())
|
||||
}
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() and 0xff or (get().toInt() shl 8 and 0xff00) or
|
||||
(get().toInt() shl 16 and 0xff0000) or (get().toInt() shl 24 and 0xff000000.toInt())
|
||||
}
|
||||
else -> throw UnsupportedOperationException()
|
||||
}
|
||||
}
|
||||
@ -815,51 +821,150 @@ inline fun toDouble(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte
|
||||
private val UPPER_HEX_ARRAY = "0123456789ABCDEF".toCharArray()
|
||||
private val LOWER_HEX_ARRAY = "0123456789abcdef".toCharArray()
|
||||
|
||||
private inline fun toHexString(
|
||||
upper: Boolean,
|
||||
length: Int,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
toBytes: (ByteOrder, (Byte) -> Unit) -> Unit,
|
||||
): String {
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline fun toHex(hexArray: CharArray, b: Int, ca: CharArray, index: Int) {
|
||||
ca[index] = hexArray[b ushr 4 and 0x0F]
|
||||
ca[index + 1] = hexArray[b and 0x0F]
|
||||
}
|
||||
|
||||
fun Byte.toHexChars(
|
||||
upper: Boolean = true,
|
||||
): CharArray {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(length)
|
||||
val hexChars = CharArray(2)
|
||||
toHex(hexArray, toInt(), hexChars, 0)
|
||||
return hexChars
|
||||
}
|
||||
|
||||
fun Char.toHexChars(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): CharArray {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(4)
|
||||
var i = 0
|
||||
toBytes(byteOrder) { b ->
|
||||
@Suppress("NAME_SHADOWING") val b = b.toInt()
|
||||
hexChars[i shl 1] = hexArray[b ushr 4 and 0x0F]
|
||||
hexChars[(i shl 1) + 1] = hexArray[b and 0x0F]
|
||||
i++
|
||||
toBytes(byteOrder) {
|
||||
toHex(hexArray, it.toInt(), hexChars, i)
|
||||
i += 2
|
||||
}
|
||||
return String(hexChars)
|
||||
return hexChars
|
||||
}
|
||||
|
||||
fun Short.toHexChars(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): CharArray {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(4)
|
||||
var i = 0
|
||||
toBytes(byteOrder) {
|
||||
toHex(hexArray, it.toInt(), hexChars, i)
|
||||
i += 2
|
||||
}
|
||||
return hexChars
|
||||
}
|
||||
|
||||
fun Int.toHexChars(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): CharArray {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(8)
|
||||
var i = 0
|
||||
toBytes(byteOrder) {
|
||||
toHex(hexArray, it.toInt(), hexChars, i)
|
||||
i += 2
|
||||
}
|
||||
return hexChars
|
||||
}
|
||||
|
||||
fun Long.toHexChars(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): CharArray {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(16)
|
||||
var i = 0
|
||||
toBytes(byteOrder) {
|
||||
toHex(hexArray, it.toInt(), hexChars, i)
|
||||
i += 2
|
||||
}
|
||||
return hexChars
|
||||
}
|
||||
|
||||
fun Float.toHexChars(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): CharArray {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(8)
|
||||
var i = 0
|
||||
toBytes(byteOrder) {
|
||||
toHex(hexArray, it.toInt(), hexChars, i)
|
||||
i += 2
|
||||
}
|
||||
return hexChars
|
||||
}
|
||||
|
||||
fun Double.toHexChars(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): CharArray {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(16)
|
||||
var i = 0
|
||||
toBytes(byteOrder) {
|
||||
toHex(hexArray, it.toInt(), hexChars, i)
|
||||
i += 2
|
||||
}
|
||||
return hexChars
|
||||
}
|
||||
|
||||
fun Byte.toHexString(
|
||||
upper: Boolean = true,
|
||||
): String {
|
||||
return toHexChars(upper).packageToString()
|
||||
}
|
||||
|
||||
fun Char.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 4, byteOrder, ::toBytes)
|
||||
): String {
|
||||
return toHexChars(upper, byteOrder).packageToString()
|
||||
}
|
||||
|
||||
fun Short.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 4, byteOrder, ::toBytes)
|
||||
): String {
|
||||
return toHexChars(upper, byteOrder).packageToString()
|
||||
}
|
||||
|
||||
fun Int.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 8, byteOrder, ::toBytes)
|
||||
): String {
|
||||
return toHexChars(upper, byteOrder).packageToString()
|
||||
}
|
||||
|
||||
fun Long.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 16, byteOrder, ::toBytes)
|
||||
): String {
|
||||
return toHexChars(upper, byteOrder).packageToString()
|
||||
}
|
||||
|
||||
fun Float.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 8, byteOrder, ::toBytes)
|
||||
): String {
|
||||
return toHexChars(upper, byteOrder).packageToString()
|
||||
}
|
||||
|
||||
fun Double.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 16, byteOrder, ::toBytes)
|
||||
): String {
|
||||
return toHexChars(upper, byteOrder).packageToString()
|
||||
}
|
||||
|
||||
|
@ -80,6 +80,23 @@ object Utils {
|
||||
val ownerField: Field by lazy {
|
||||
kotlin.jvm.internal.CallableReference::class.java.getDeclaredField("owner").apply { isAccessible = true }
|
||||
}
|
||||
|
||||
val strValue = String::class.java.declaredFields.firstOrNull {
|
||||
it.type == CharArray::class.java
|
||||
}?.also {
|
||||
it.isAccessible = true
|
||||
it.final = false
|
||||
}
|
||||
}
|
||||
|
||||
fun CharArray.packageToString(): String {
|
||||
return if (size < 64 * 1024 || Utils.strValue == null) {
|
||||
String(this)
|
||||
} else {
|
||||
val str = String()
|
||||
Utils.strValue.set(str, this)
|
||||
str
|
||||
}
|
||||
}
|
||||
|
||||
fun String.hexStringToByteArray(): ByteArray {
|
||||
|
@ -62,14 +62,10 @@ interface ByteBuffer : Closeable {
|
||||
fun closeChild(child: ByteBuffer) {}
|
||||
|
||||
fun readBuffer(): java.nio.ByteBuffer
|
||||
fun finishRead(buffer: java.nio.ByteBuffer) {
|
||||
readPosition += buffer.position()
|
||||
}
|
||||
fun finishRead(buffer: java.nio.ByteBuffer)
|
||||
|
||||
fun writeBuffer(): java.nio.ByteBuffer
|
||||
fun finishWrite(buffer: java.nio.ByteBuffer) {
|
||||
writePosition += buffer.position()
|
||||
}
|
||||
fun finishWrite(buffer: java.nio.ByteBuffer)
|
||||
|
||||
fun reset()
|
||||
fun slice(position: Int, size: Int): ByteBuffer = slice(position, size, 0, 0)
|
||||
@ -88,8 +84,9 @@ interface ByteBuffer : Closeable {
|
||||
writePosition = 0
|
||||
}
|
||||
|
||||
fun skip(n: Int) {
|
||||
fun skip(n: Int): Int {
|
||||
readPosition += n
|
||||
return n
|
||||
}
|
||||
|
||||
fun get(): Byte = read { it.get() }
|
||||
|
@ -128,7 +128,7 @@ val Collection<ByteBuffer>.writeable: Int
|
||||
return size
|
||||
}
|
||||
|
||||
fun ByteBuffer.getIntWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Int {
|
||||
fun ByteBuffer.getIntWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Int {
|
||||
var time = 4
|
||||
return toInt(byteOrder) {
|
||||
if (--time < size) {
|
||||
@ -139,7 +139,7 @@ fun ByteBuffer.getIntWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.native
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteBuffer.getLongWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Int {
|
||||
fun ByteBuffer.getLongWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Int {
|
||||
var time = 8
|
||||
return toInt(byteOrder) {
|
||||
if (--time < size) {
|
||||
@ -150,7 +150,7 @@ fun ByteBuffer.getLongWithSize(size: Int, byteOrder: ByteOrder = ByteOrder.nativ
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteBuffer.putIntWithSize(n: Int, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteBuffer.putIntWithSize(n: Int, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
var time = size
|
||||
@ -171,7 +171,7 @@ fun ByteBuffer.putIntWithSize(n: Int, size: Int, byteOrder: ByteOrder = ByteOrde
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteBuffer.putLongWithSize(l: Long, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteBuffer.putLongWithSize(l: Long, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
var time = size
|
||||
|
@ -2,6 +2,7 @@ package cn.tursom.core.buffer
|
||||
|
||||
import cn.tursom.core.buffer.impl.ListByteBuffer
|
||||
import cn.tursom.core.forEachIndex
|
||||
import cn.tursom.core.toBytes
|
||||
import java.io.Closeable
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
@ -39,32 +40,35 @@ interface MultipleByteBuffer : Closeable, ByteBuffer, NioBuffers.Sequences {
|
||||
}
|
||||
|
||||
override fun readBufferSequence(): Sequence<java.nio.ByteBuffer> = sequence {
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
yieldAll(it.readBufferSequence())
|
||||
buffers.forEach { subBuf ->
|
||||
val sequences = subBuf.getExtension(NioBuffers.Sequences)
|
||||
if (sequences != null) {
|
||||
yieldAll(sequences.readBufferSequence())
|
||||
} else {
|
||||
yield(it.readBuffer())
|
||||
yield(subBuf.readBuffer())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun writeBufferSequence(): Sequence<java.nio.ByteBuffer> = sequence {
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
yieldAll(it.writeBufferSequence())
|
||||
buffers.forEach { subBuf ->
|
||||
val sequences = subBuf.getExtension(NioBuffers.Sequences)
|
||||
if (sequences != null) {
|
||||
yieldAll(sequences.writeBufferSequence())
|
||||
} else {
|
||||
yield(it.writeBuffer())
|
||||
yield(subBuf.writeBuffer())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun finishRead(buffers: Sequence<java.nio.ByteBuffer>) = finishRead(buffers.iterator())
|
||||
override fun finishRead(buffers: Iterator<java.nio.ByteBuffer>) {
|
||||
this.buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.finishRead(buffers)
|
||||
this.buffers.forEach { subBuf ->
|
||||
val sequences = subBuf.getExtension(NioBuffers.Sequences)
|
||||
if (sequences != null) {
|
||||
sequences.finishRead(buffers)
|
||||
} else {
|
||||
it.finishRead(buffers.next())
|
||||
subBuf.finishRead(buffers.next())
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -72,8 +76,9 @@ interface MultipleByteBuffer : Closeable, ByteBuffer, NioBuffers.Sequences {
|
||||
fun finishWrite(buffers: Sequence<java.nio.ByteBuffer>) = finishWrite(buffers.iterator())
|
||||
override fun finishWrite(buffers: Iterator<java.nio.ByteBuffer>) {
|
||||
this.buffers.forEach { subBuf ->
|
||||
if (subBuf is MultipleByteBuffer) {
|
||||
subBuf.finishWrite(buffers)
|
||||
val sequences = subBuf.getExtension(NioBuffers.Sequences)
|
||||
if (sequences != null) {
|
||||
sequences.finishWrite(buffers)
|
||||
} else {
|
||||
subBuf.finishWrite(buffers.next())
|
||||
}
|
||||
@ -85,9 +90,12 @@ interface MultipleByteBuffer : Closeable, ByteBuffer, NioBuffers.Sequences {
|
||||
return ListByteBuffer(ArrayList(buffers.subList(position, position + size)))
|
||||
}
|
||||
|
||||
override fun slice(position: Int, size: Int, readPosition: Int, writePosition: Int): ByteBuffer =
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
override fun fill(byte: Byte) = buffers.forEach { it.fill(byte) }
|
||||
override fun clear() = buffers.forEach(ByteBuffer::clear)
|
||||
override fun reset() = buffers.forEach(ByteBuffer::reset)
|
||||
override fun clear() = buffers.forEach { it.clear() }
|
||||
override fun reset() = buffers.forEach { it.reset() }
|
||||
|
||||
|
||||
override val resized: Boolean get() = false
|
||||
@ -102,8 +110,6 @@ interface MultipleByteBuffer : Closeable, ByteBuffer, NioBuffers.Sequences {
|
||||
|
||||
override fun readBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
override fun writeBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
override fun slice(position: Int, size: Int, readPosition: Int, writePosition: Int): ByteBuffer =
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
override fun resize(newSize: Int): Boolean = false
|
||||
|
||||
@ -161,6 +167,29 @@ interface MultipleByteBuffer : Closeable, ByteBuffer, NioBuffers.Sequences {
|
||||
}
|
||||
|
||||
override fun put(byte: Byte)
|
||||
override fun put(char: Char, byteOrder: ByteOrder) {
|
||||
char.toBytes { put(it) }
|
||||
}
|
||||
|
||||
override fun put(short: Short, byteOrder: ByteOrder) {
|
||||
short.toBytes { put(it) }
|
||||
}
|
||||
|
||||
override fun put(int: Int, byteOrder: ByteOrder) {
|
||||
int.toBytes { put(it) }
|
||||
}
|
||||
|
||||
override fun put(long: Long, byteOrder: ByteOrder) {
|
||||
long.toBytes { put(it) }
|
||||
}
|
||||
|
||||
override fun put(float: Float, byteOrder: ByteOrder) {
|
||||
float.toBytes { put(it) }
|
||||
}
|
||||
|
||||
override fun put(double: Double, byteOrder: ByteOrder) {
|
||||
double.toBytes { put(it) }
|
||||
}
|
||||
|
||||
override fun put(byteArray: ByteArray, offset: Int, len: Int): Int {
|
||||
var write = 0
|
||||
|
@ -4,4 +4,8 @@ import cn.tursom.core.buffer.ByteBuffer
|
||||
|
||||
class ArrayByteBuffer(
|
||||
vararg buffers: ByteBuffer,
|
||||
) : ListByteBuffer(ArrayList(buffers.asList()))
|
||||
) : ListByteBuffer(ArrayList(buffers.asList())) {
|
||||
override fun toString(): String {
|
||||
return "ArrayByteBuffer[$readPosition:$writePosition:$capacity]"
|
||||
}
|
||||
}
|
@ -21,7 +21,13 @@ class DirectByteBuffer(
|
||||
buffer.limit(writePosition)
|
||||
if (buffer.position() != readPosition)
|
||||
buffer.position(readPosition)
|
||||
return buffer.slice()
|
||||
return buffer
|
||||
}
|
||||
|
||||
override fun finishRead(buffer: java.nio.ByteBuffer) {
|
||||
if (buffer === this.buffer) {
|
||||
readPosition = buffer.position()
|
||||
}
|
||||
}
|
||||
|
||||
override fun writeBuffer(): java.nio.ByteBuffer {
|
||||
@ -29,7 +35,13 @@ class DirectByteBuffer(
|
||||
buffer.limit(capacity)
|
||||
if (buffer.position() != writePosition)
|
||||
buffer.position(writePosition)
|
||||
return buffer.slice()
|
||||
return buffer
|
||||
}
|
||||
|
||||
override fun finishWrite(buffer: java.nio.ByteBuffer) {
|
||||
if (buffer === this.buffer) {
|
||||
writePosition = buffer.position()
|
||||
}
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
|
@ -29,7 +29,13 @@ class HeapByteBuffer(
|
||||
buffer.limit(writePosition)
|
||||
if (buffer.position() != readPosition)
|
||||
buffer.position(readPosition)
|
||||
return buffer.slice()
|
||||
return buffer
|
||||
}
|
||||
|
||||
override fun finishRead(buffer: java.nio.ByteBuffer) {
|
||||
if (buffer === this.buffer) {
|
||||
readPosition = buffer.position()
|
||||
}
|
||||
}
|
||||
|
||||
override fun writeBuffer(): java.nio.ByteBuffer {
|
||||
@ -37,7 +43,13 @@ class HeapByteBuffer(
|
||||
buffer.limit(capacity)
|
||||
if (buffer.position() != writePosition)
|
||||
buffer.position(writePosition)
|
||||
return buffer.slice()
|
||||
return buffer
|
||||
}
|
||||
|
||||
override fun finishWrite(buffer: java.nio.ByteBuffer) {
|
||||
if (buffer === this.buffer) {
|
||||
writePosition = buffer.position()
|
||||
}
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
|
@ -8,10 +8,10 @@ import java.nio.ByteOrder
|
||||
open class ListByteBuffer(
|
||||
final override val buffers: MutableList<ByteBuffer> = ArrayList(),
|
||||
) : MultipleByteBuffer {
|
||||
var readArrayPosition: Int = 0
|
||||
var writeArrayPosition: Int = 0
|
||||
var readOperator = buffers.firstOrNull()
|
||||
var writeOperator = buffers.firstOrNull()
|
||||
var readArrayPosition: Int = if (readOperator == null) -1 else 0
|
||||
var writeArrayPosition: Int = if (writeOperator == null) -1 else 0
|
||||
|
||||
private var buffersArrayCache: Array<out ByteBuffer>? = null
|
||||
override val buffersArray: Array<out ByteBuffer>
|
||||
@ -42,8 +42,56 @@ open class ListByteBuffer(
|
||||
return writeOperator?.isWriteable ?: false
|
||||
}
|
||||
|
||||
override fun clear() {
|
||||
super.clear()
|
||||
readPosition = 0
|
||||
writePosition = 0
|
||||
readArrayPosition = 0
|
||||
writeArrayPosition = 0
|
||||
}
|
||||
|
||||
override fun reset() {
|
||||
super.reset()
|
||||
readPosition = 0
|
||||
writePosition = 0
|
||||
readArrayPosition = 0
|
||||
writeArrayPosition = 0
|
||||
}
|
||||
|
||||
override fun skip(n: Int): Int {
|
||||
return when {
|
||||
n == 0 -> 0
|
||||
n > 0 -> {
|
||||
var skip = 0
|
||||
while (skip == n) {
|
||||
skip += readOperator?.skip(n - skip) ?: 0
|
||||
if (readArrayPosition < buffers.size) {
|
||||
readOperator = buffers[readArrayPosition++]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
skip
|
||||
}
|
||||
else -> {
|
||||
var fallback = 0
|
||||
while (fallback == n) {
|
||||
fallback += readOperator?.skip(n - fallback) ?: 0
|
||||
if (readArrayPosition > 0) {
|
||||
readOperator = buffers[--readArrayPosition]
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
fallback
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun readBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
override fun finishRead(buffer: java.nio.ByteBuffer) = throw UnsupportedOperationException()
|
||||
override fun writeBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
override fun finishWrite(buffer: java.nio.ByteBuffer) = throw UnsupportedOperationException()
|
||||
|
||||
override fun append(buffer: ByteBuffer) {
|
||||
val bufReadPosition = buffer.readPosition
|
||||
@ -66,7 +114,7 @@ open class ListByteBuffer(
|
||||
}
|
||||
|
||||
private fun updateWrite() {
|
||||
while (writeArrayPosition < buffers.size && writeOperator?.isWriteable == true) {
|
||||
while (writeArrayPosition < buffers.size && writeOperator?.isWriteable != true) {
|
||||
writeOperator = buffers[writeArrayPosition++]
|
||||
}
|
||||
}
|
||||
|
@ -116,10 +116,18 @@ class NettyByteBuffer(
|
||||
return byteBuf.internalNioBuffer(readPosition, readable).slice()
|
||||
}
|
||||
|
||||
override fun finishRead(buffer: java.nio.ByteBuffer) {
|
||||
byteBuf.readerIndex(buffer.position())
|
||||
}
|
||||
|
||||
override fun writeBuffer(): java.nio.ByteBuffer {
|
||||
return byteBuf.internalNioBuffer(writePosition, writeable).slice()
|
||||
}
|
||||
|
||||
override fun finishWrite(buffer: java.nio.ByteBuffer) {
|
||||
byteBuf.writerIndex(buffer.position())
|
||||
}
|
||||
|
||||
override val readOffset: Int get() = byteBuf.arrayOffset() + byteBuf.readerIndex()
|
||||
|
||||
override fun clear() {
|
||||
|
Loading…
Reference in New Issue
Block a user