mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-14 03:40:06 +08:00
update buffers
This commit is contained in:
parent
d48e9645a5
commit
8b826b7350
@ -7,43 +7,43 @@ import java.nio.ByteOrder
|
||||
|
||||
class WrongPushTypeException : Exception()
|
||||
|
||||
fun Char.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun Char.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val array = ByteArray(2)
|
||||
array.put(this, 0, byteOrder)
|
||||
return array
|
||||
}
|
||||
|
||||
fun Short.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun Short.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val array = ByteArray(2)
|
||||
array.put(this, 0, byteOrder)
|
||||
return array
|
||||
}
|
||||
|
||||
fun Int.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun Int.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val array = ByteArray(4)
|
||||
array.put(this, 0, byteOrder)
|
||||
return array
|
||||
}
|
||||
|
||||
fun Long.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun Long.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val array = ByteArray(8)
|
||||
array.put(this, 0, byteOrder)
|
||||
return array
|
||||
}
|
||||
|
||||
fun Float.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun Float.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val array = ByteArray(4)
|
||||
array.put(this, 0, byteOrder)
|
||||
return array
|
||||
}
|
||||
|
||||
fun Double.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun Double.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val array = ByteArray(8)
|
||||
array.put(this, 0, byteOrder)
|
||||
return array
|
||||
}
|
||||
|
||||
fun CharArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun CharArray.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val newArray = ByteArray(size * 2)
|
||||
repeat(size) {
|
||||
newArray.put(this[it], it * 2, byteOrder)
|
||||
@ -51,7 +51,7 @@ fun CharArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteA
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun ShortArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun ShortArray.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val newArray = ByteArray(size * 2)
|
||||
repeat(size) {
|
||||
newArray.put(this[it], it * 2, byteOrder)
|
||||
@ -59,7 +59,7 @@ fun ShortArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): Byte
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun IntArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun IntArray.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val newArray = ByteArray(size * 4)
|
||||
repeat(size) {
|
||||
newArray.put(this[it], it * 4, byteOrder)
|
||||
@ -67,7 +67,7 @@ fun IntArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteAr
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun LongArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun LongArray.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val newArray = ByteArray(size * 8)
|
||||
repeat(size) {
|
||||
newArray.put(this[it], it * 8, byteOrder)
|
||||
@ -75,7 +75,7 @@ fun LongArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteA
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun FloatArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun FloatArray.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val newArray = ByteArray(size * 4)
|
||||
repeat(size) {
|
||||
newArray.put(this[it], it * 4, byteOrder)
|
||||
@ -83,7 +83,7 @@ fun FloatArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): Byte
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun DoubleArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): ByteArray {
|
||||
fun DoubleArray.toByteArray(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ByteArray {
|
||||
val newArray = ByteArray(size * 8)
|
||||
repeat(size) {
|
||||
newArray.put(this[it], it * 8, byteOrder)
|
||||
@ -91,7 +91,7 @@ fun DoubleArray.toByteArray(byteOrder: ByteOrder = ByteOrder.nativeOrder()): Byt
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun ByteArray.toChar(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Char {
|
||||
fun ByteArray.toChar(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Char {
|
||||
return if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
||||
(this[offset].toInt() or (this[offset + 1].toInt() shl 8))
|
||||
} else {
|
||||
@ -99,7 +99,7 @@ fun ByteArray.toChar(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrd
|
||||
}.toChar()
|
||||
}
|
||||
|
||||
fun ByteArray.toShort(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Short {
|
||||
fun ByteArray.toShort(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Short {
|
||||
return if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
||||
(this[offset].toInt() or (this[offset + 1].toInt() shl 8))
|
||||
} else {
|
||||
@ -107,7 +107,7 @@ fun ByteArray.toShort(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOr
|
||||
}.toShort()
|
||||
}
|
||||
|
||||
fun ByteArray.toInt(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Int {
|
||||
fun ByteArray.toInt(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Int {
|
||||
return if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
||||
this[offset].toInt() and 0xff or (this[offset + 1].toInt() shl 8 and 0xff00) or
|
||||
(this[offset + 2].toInt() shl 16 and 0xff0000) or (this[offset + 3].toInt() shl 24 and 0xff000000.toInt())
|
||||
@ -117,7 +117,7 @@ fun ByteArray.toInt(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrde
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.toLong(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Long {
|
||||
fun ByteArray.toLong(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Long {
|
||||
return if (byteOrder == ByteOrder.LITTLE_ENDIAN) {
|
||||
toInt(offset).toLong() or (toInt(offset + 4).toLong().shl(32) and 0xffff_ffffL.inv())
|
||||
} else {
|
||||
@ -125,15 +125,15 @@ fun ByteArray.toLong(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrd
|
||||
}.toLong()
|
||||
}
|
||||
|
||||
fun ByteArray.toFloat(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Float {
|
||||
fun ByteArray.toFloat(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Float {
|
||||
return Float.fromBits(toInt(offset, byteOrder))
|
||||
}
|
||||
|
||||
fun ByteArray.toDouble(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Double {
|
||||
fun ByteArray.toDouble(offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Double {
|
||||
return Double.fromBits(toLong(offset, byteOrder))
|
||||
}
|
||||
|
||||
fun ByteArray.toCharArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): CharArray {
|
||||
fun ByteArray.toCharArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): CharArray {
|
||||
if (offset + size * 2 > this.size) throw IndexOutOfBoundsException()
|
||||
val newArray = CharArray(size)
|
||||
repeat(newArray.size) {
|
||||
@ -142,7 +142,7 @@ fun ByteArray.toCharArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrd
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun ByteArray.toShortArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): ShortArray {
|
||||
fun ByteArray.toShortArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): ShortArray {
|
||||
if (offset + size * 2 > this.size) throw IndexOutOfBoundsException()
|
||||
val newArray = ShortArray(size)
|
||||
repeat(newArray.size) {
|
||||
@ -151,7 +151,7 @@ fun ByteArray.toShortArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOr
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun ByteArray.toIntArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): IntArray {
|
||||
fun ByteArray.toIntArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): IntArray {
|
||||
if (offset + size * 4 > this.size) throw IndexOutOfBoundsException()
|
||||
val newArray = IntArray(size)
|
||||
repeat(newArray.size) {
|
||||
@ -160,7 +160,7 @@ fun ByteArray.toIntArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrde
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun ByteArray.toLongArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): LongArray {
|
||||
fun ByteArray.toLongArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): LongArray {
|
||||
if (offset + size * 8 > this.size) throw IndexOutOfBoundsException()
|
||||
val newArray = LongArray(size)
|
||||
repeat(newArray.size) {
|
||||
@ -169,7 +169,7 @@ fun ByteArray.toLongArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrd
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun ByteArray.toFloatArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): FloatArray {
|
||||
fun ByteArray.toFloatArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): FloatArray {
|
||||
if (offset + size * 4 > this.size) throw IndexOutOfBoundsException()
|
||||
val newArray = FloatArray(size)
|
||||
repeat(newArray.size) {
|
||||
@ -178,7 +178,7 @@ fun ByteArray.toFloatArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOr
|
||||
return newArray
|
||||
}
|
||||
|
||||
fun ByteArray.toDoubleArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.nativeOrder()): DoubleArray {
|
||||
fun ByteArray.toDoubleArray(offset: Int, size: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): DoubleArray {
|
||||
if (offset + size * 8 > this.size) throw IndexOutOfBoundsException()
|
||||
val newArray = DoubleArray(size)
|
||||
repeat(newArray.size) {
|
||||
@ -219,7 +219,7 @@ fun Long.ntoh(): Long {
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(char: Char, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteArray.put(char: Char, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
val value = char.code
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
@ -233,7 +233,7 @@ fun ByteArray.put(char: Char, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(short: Short, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteArray.put(short: Short, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
val value = short.toInt()
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
@ -247,7 +247,7 @@ fun ByteArray.put(short: Short, offset: Int = 0, byteOrder: ByteOrder = ByteOrde
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(int: Int, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteArray.put(int: Int, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = int.toByte()
|
||||
@ -264,7 +264,7 @@ fun ByteArray.put(int: Int, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.na
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(long: Long, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteArray.put(long: Long, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
this[offset] = long.toByte()
|
||||
@ -289,11 +289,11 @@ fun ByteArray.put(long: Long, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.
|
||||
}
|
||||
}
|
||||
|
||||
fun ByteArray.put(float: Float, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteArray.put(float: Float, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
put(float.toBits(), offset, byteOrder)
|
||||
}
|
||||
|
||||
fun ByteArray.put(double: Double, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()) {
|
||||
fun ByteArray.put(double: Double, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN) {
|
||||
put(double.toBits(), offset, byteOrder)
|
||||
}
|
||||
|
||||
@ -301,7 +301,7 @@ fun ByteArray.put(
|
||||
charArray: CharArray,
|
||||
offset: Int = 0,
|
||||
addLength: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
var index = offset
|
||||
if (addLength) {
|
||||
@ -318,7 +318,7 @@ fun ByteArray.put(
|
||||
shortArray: ShortArray,
|
||||
offset: Int = 0,
|
||||
addLength: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
var index = offset
|
||||
if (addLength) {
|
||||
@ -335,7 +335,7 @@ fun ByteArray.put(
|
||||
intArray: IntArray,
|
||||
offset: Int = 0,
|
||||
addLength: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
var index = offset
|
||||
if (addLength) {
|
||||
@ -352,7 +352,7 @@ fun ByteArray.put(
|
||||
longArray: LongArray,
|
||||
offset: Int = 0,
|
||||
addLength: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
var index = offset
|
||||
if (addLength) {
|
||||
@ -369,7 +369,7 @@ fun ByteArray.put(
|
||||
floatArray: FloatArray,
|
||||
offset: Int = 0,
|
||||
addLength: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
var index = offset
|
||||
if (addLength) {
|
||||
@ -386,7 +386,7 @@ fun ByteArray.put(
|
||||
doubleArray: DoubleArray,
|
||||
offset: Int = 0,
|
||||
addLength: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
var index = offset
|
||||
if (addLength) {
|
||||
@ -403,7 +403,7 @@ fun ByteArray.put(
|
||||
str: String,
|
||||
offset: Int = 0,
|
||||
addLength: Boolean = false,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): Int {
|
||||
val utf8Array = str.toByteArray()
|
||||
return if (addLength) {
|
||||
@ -422,7 +422,7 @@ fun ByteArray.pop(
|
||||
offset: Int = 0,
|
||||
fromIndex: Int = 0,
|
||||
size: Int = (this.size - fromIndex) / 2,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
repeat(size) {
|
||||
array[offset + it] = toChar(fromIndex + it * 2, byteOrder)
|
||||
@ -434,7 +434,7 @@ fun ByteArray.pop(
|
||||
offset: Int = 0,
|
||||
fromIndex: Int = 0,
|
||||
size: Int = (this.size - fromIndex) / 2,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
repeat(size) {
|
||||
array[offset + it] = toShort(fromIndex + it * 2, byteOrder)
|
||||
@ -446,7 +446,7 @@ fun ByteArray.pop(
|
||||
offset: Int = 0,
|
||||
fromIndex: Int = 0,
|
||||
size: Int = (this.size - fromIndex) / 4,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
repeat(size) {
|
||||
array[offset + it] = toInt(fromIndex + it * 4, byteOrder)
|
||||
@ -458,7 +458,7 @@ fun ByteArray.pop(
|
||||
offset: Int = 0,
|
||||
fromIndex: Int = 0,
|
||||
size: Int = (this.size - fromIndex) / 8,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
repeat(size) {
|
||||
array[offset + it] = toLong(fromIndex + it * 8, byteOrder)
|
||||
@ -470,7 +470,7 @@ fun ByteArray.pop(
|
||||
offset: Int = 0,
|
||||
fromIndex: Int = 0,
|
||||
size: Int = (this.size - fromIndex) / 4,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
repeat(size) {
|
||||
array[offset + it] = toFloat(fromIndex + it * 4, byteOrder)
|
||||
@ -482,7 +482,7 @@ fun ByteArray.pop(
|
||||
offset: Int = 0,
|
||||
fromIndex: Int = 0,
|
||||
size: Int = (this.size - fromIndex) / 8,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
repeat(size) {
|
||||
array[offset + it] = toDouble(fromIndex + it * 8, byteOrder)
|
||||
@ -502,7 +502,7 @@ fun Int.asFloat(): Float = Float.fromBits(this)
|
||||
fun Long.asDouble(): Double = Double.fromBits(this)
|
||||
|
||||
|
||||
fun ByteArray.put(obj: Any, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.nativeOrder()): Int {
|
||||
fun ByteArray.put(obj: Any, offset: Int = 0, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Int {
|
||||
return when (obj) {
|
||||
is Byte -> if (offset < size) {
|
||||
this[offset] = obj
|
||||
@ -664,7 +664,7 @@ inline fun DoubleArray.forEachIndex(fromIndex: Int, toIndex: Int, action: (Doubl
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Char.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action: (Byte) -> Unit) {
|
||||
inline fun Char.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
val value = code
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
@ -678,7 +678,7 @@ inline fun Char.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action:
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Short.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action: (Byte) -> Unit) {
|
||||
inline fun Short.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
val value = toInt()
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
@ -692,7 +692,7 @@ inline fun Short.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action:
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Int.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action: (Byte) -> Unit) {
|
||||
inline fun Int.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(this.toByte())
|
||||
@ -709,7 +709,7 @@ inline fun Int.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action: (
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Long.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action: (Byte) -> Unit) {
|
||||
inline fun Long.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
action(this.toByte())
|
||||
@ -734,15 +734,15 @@ inline fun Long.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action:
|
||||
}
|
||||
}
|
||||
|
||||
inline fun Float.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action: (Byte) -> Unit) {
|
||||
inline fun Float.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
toBits().toBytes(byteOrder, action)
|
||||
}
|
||||
|
||||
inline fun Double.toBytes(byteOrder: ByteOrder = ByteOrder.nativeOrder(), action: (Byte) -> Unit) {
|
||||
inline fun Double.toBytes(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, action: (Byte) -> Unit) {
|
||||
toBits().toBytes(byteOrder, action)
|
||||
}
|
||||
|
||||
inline fun toChar(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byte): Char {
|
||||
inline fun toChar(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Char {
|
||||
return when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() or (get().toInt() shl 8)
|
||||
@ -757,7 +757,7 @@ inline fun toChar(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byt
|
||||
}.toChar()
|
||||
}
|
||||
|
||||
inline fun toShort(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byte): Short {
|
||||
inline fun toShort(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Short {
|
||||
return when (byteOrder) {
|
||||
ByteOrder.LITTLE_ENDIAN -> {
|
||||
get().toInt() or (get().toInt() shl 8)
|
||||
@ -770,7 +770,7 @@ inline fun toShort(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> By
|
||||
}.toShort()
|
||||
}
|
||||
|
||||
inline fun toInt(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byte): Int {
|
||||
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
|
||||
@ -787,7 +787,7 @@ inline fun toInt(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byte
|
||||
}
|
||||
}
|
||||
|
||||
inline fun toLong(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byte): Long {
|
||||
inline fun toLong(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Long {
|
||||
return if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
|
||||
toInt(byteOrder, get).toLong() or (toInt(byteOrder, get).toLong().shl(32) and 0xffff_ffffL.inv())
|
||||
} else {
|
||||
@ -796,11 +796,11 @@ inline fun toLong(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byt
|
||||
}.toLong()
|
||||
}
|
||||
|
||||
inline fun toFloat(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byte): Float {
|
||||
inline fun toFloat(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Float {
|
||||
return Float.fromBits(toInt(byteOrder, get))
|
||||
}
|
||||
|
||||
inline fun toDouble(byteOrder: ByteOrder = ByteOrder.nativeOrder(), get: () -> Byte): Double {
|
||||
inline fun toDouble(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN, get: () -> Byte): Double {
|
||||
return Double.fromBits(toLong(byteOrder, get))
|
||||
}
|
||||
|
||||
@ -811,8 +811,8 @@ private val LOWER_HEX_ARRAY = "0123456789abcdef".toCharArray()
|
||||
private inline fun toHexString(
|
||||
upper: Boolean,
|
||||
length: Int,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder(),
|
||||
toBytes: (ByteOrder, (Byte) -> Unit) -> Unit
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
toBytes: (ByteOrder, (Byte) -> Unit) -> Unit,
|
||||
): String {
|
||||
val hexArray = if (upper) UPPER_HEX_ARRAY else LOWER_HEX_ARRAY
|
||||
val hexChars = CharArray(length)
|
||||
@ -828,31 +828,31 @@ private inline fun toHexString(
|
||||
|
||||
fun Char.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 4, byteOrder, ::toBytes)
|
||||
|
||||
fun Short.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 4, byteOrder, ::toBytes)
|
||||
|
||||
fun Int.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 8, byteOrder, ::toBytes)
|
||||
|
||||
fun Long.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 16, byteOrder, ::toBytes)
|
||||
|
||||
fun Float.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 8, byteOrder, ::toBytes)
|
||||
|
||||
fun Double.toHexString(
|
||||
upper: Boolean = true,
|
||||
byteOrder: ByteOrder = ByteOrder.nativeOrder()
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
): String = toHexString(upper, 16, byteOrder, ::toBytes)
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
package cn.tursom.core.buffer
|
||||
|
||||
import cn.tursom.core.*
|
||||
import cn.tursom.core.AsyncFile
|
||||
import cn.tursom.core.Utils.bufferThreadLocal
|
||||
import cn.tursom.core.forEachIndex
|
||||
import java.io.Closeable
|
||||
import java.io.IOException
|
||||
import java.io.InputStream
|
||||
@ -93,21 +94,47 @@ interface ByteBuffer : Closeable {
|
||||
}
|
||||
|
||||
fun get(): Byte = read { it.get() }
|
||||
fun getChar(): Char = read { it.char }
|
||||
fun getShort(): Short = read { it.short }
|
||||
fun getInt(): Int = read { it.int }
|
||||
fun getLong(): Long = read { it.long }
|
||||
fun getFloat(): Float = read { it.float }
|
||||
fun getDouble(): Double = read { it.double }
|
||||
//fun getChar(): Char = read { it.char }
|
||||
//fun getShort(): Short = read { it.short }
|
||||
//fun getInt(): Int = read { it.int }
|
||||
//fun getLong(): Long = read { it.long }
|
||||
//fun getFloat(): Float = read { it.float }
|
||||
//fun getDouble(): Double = read { it.double }
|
||||
|
||||
fun getChar(byteOrder: ByteOrder): Char = toChar(byteOrder) { get() }
|
||||
fun getShort(byteOrder: ByteOrder): Short = toShort(byteOrder) { get() }
|
||||
fun getInt(byteOrder: ByteOrder): Int = toInt(byteOrder) { get() }
|
||||
fun getLong(byteOrder: ByteOrder): Long = toLong(byteOrder) { get() }
|
||||
fun getFloat(byteOrder: ByteOrder): Float = toFloat(byteOrder) { get() }
|
||||
fun getDouble(byteOrder: ByteOrder): Double = toDouble(byteOrder) { get() }
|
||||
fun getChar(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Char = read { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.char
|
||||
}
|
||||
|
||||
fun getShort(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Short = read { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.short
|
||||
}
|
||||
|
||||
fun getInt(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Int = read { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.int
|
||||
}
|
||||
|
||||
fun getLong(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Long = read { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.long
|
||||
}
|
||||
|
||||
fun getFloat(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Float = read { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.float
|
||||
}
|
||||
|
||||
fun getDouble(byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Double = read { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.double
|
||||
}
|
||||
|
||||
fun getBytes(size: Int = readable): ByteArray = read {
|
||||
if (it.limit() - it.position() < size) {
|
||||
throw IndexOutOfBoundsException()
|
||||
}
|
||||
val bytes = ByteArray(size)
|
||||
it.get(bytes)
|
||||
bytes
|
||||
@ -177,19 +204,42 @@ interface ByteBuffer : Closeable {
|
||||
*/
|
||||
|
||||
fun put(byte: Byte): Unit = write { it.put(byte) }
|
||||
fun put(char: Char): Unit = write { it.putChar(char) }
|
||||
fun put(short: Short): Unit = write { it.putShort(short) }
|
||||
fun put(int: Int): Unit = write { it.putInt(int) }
|
||||
fun put(long: Long): Unit = write { it.putLong(long) }
|
||||
fun put(float: Float): Unit = write { it.putFloat(float) }
|
||||
fun put(double: Double): Unit = write { it.putDouble(double) }
|
||||
//fun put(char: Char): Unit = write { it.putChar(char) }
|
||||
//fun put(short: Short): Unit = write { it.putShort(short) }
|
||||
//fun put(int: Int): Unit = write { it.putInt(int) }
|
||||
//fun put(long: Long): Unit = write { it.putLong(long) }
|
||||
//fun put(float: Float): Unit = write { it.putFloat(float) }
|
||||
//fun put(double: Double): Unit = write { it.putDouble(double) }
|
||||
|
||||
fun put(char: Char, byteOrder: ByteOrder): Unit = char.toBytes(byteOrder) { put(it) }
|
||||
fun put(short: Short, byteOrder: ByteOrder): Unit = short.toBytes(byteOrder) { put(it) }
|
||||
fun put(int: Int, byteOrder: ByteOrder): Unit = int.toBytes(byteOrder) { put(it) }
|
||||
fun put(long: Long, byteOrder: ByteOrder): Unit = long.toBytes(byteOrder) { put(it) }
|
||||
fun put(float: Float, byteOrder: ByteOrder): Unit = float.toBytes(byteOrder) { put(it) }
|
||||
fun put(double: Double, byteOrder: ByteOrder): Unit = double.toBytes(byteOrder) { put(it) }
|
||||
fun put(char: Char, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = write { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.putChar(char)
|
||||
}
|
||||
|
||||
fun put(short: Short, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = write { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.putShort(short)
|
||||
}
|
||||
|
||||
fun put(int: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = write { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.putInt(int)
|
||||
}
|
||||
|
||||
fun put(long: Long, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = write { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.putLong(long)
|
||||
}
|
||||
|
||||
fun put(float: Float, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = write { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.putFloat(float)
|
||||
}
|
||||
|
||||
fun put(double: Double, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = write { buf ->
|
||||
buf.order(byteOrder)
|
||||
buf.putDouble(double)
|
||||
}
|
||||
|
||||
fun put(str: String): Int = put(str.toByteArray())
|
||||
fun put(buffer: ByteBuffer): Int = buffer.writeTo(this)
|
||||
@ -207,28 +257,58 @@ interface ByteBuffer : Closeable {
|
||||
}
|
||||
}
|
||||
|
||||
fun put(array: CharArray, index: Int = 0, size: Int = array.size - index) {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
fun put(
|
||||
array: CharArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
array.forEachIndex(index, index + size - 1) { put(it, byteOrder) }
|
||||
}
|
||||
|
||||
fun put(array: ShortArray, index: Int = 0, size: Int = array.size - index) {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
fun put(
|
||||
array: ShortArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
array.forEachIndex(index, index + size - 1) { put(it, byteOrder) }
|
||||
}
|
||||
|
||||
fun put(array: IntArray, index: Int = 0, size: Int = array.size - index) {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
fun put(
|
||||
array: IntArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
array.forEachIndex(index, index + size - 1) { put(it, byteOrder) }
|
||||
}
|
||||
|
||||
fun put(array: LongArray, index: Int = 0, size: Int = array.size - index) {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
fun put(
|
||||
array: LongArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
array.forEachIndex(index, index + size - 1) { put(it, byteOrder) }
|
||||
}
|
||||
|
||||
fun put(array: FloatArray, index: Int = 0, size: Int = array.size - index) {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
fun put(
|
||||
array: FloatArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
array.forEachIndex(index, index + size - 1) { put(it, byteOrder) }
|
||||
}
|
||||
|
||||
fun put(array: DoubleArray, index: Int = 0, size: Int = array.size - index) {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
fun put(
|
||||
array: DoubleArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) {
|
||||
array.forEachIndex(index, index + size - 1) { put(it, byteOrder) }
|
||||
}
|
||||
|
||||
fun put(inputStream: InputStream): Int {
|
||||
@ -263,23 +343,59 @@ interface ByteBuffer : Closeable {
|
||||
}
|
||||
|
||||
fun putByte(byte: Byte): Unit = put(byte)
|
||||
fun putChar(char: Char): Unit = put(char)
|
||||
fun putShort(short: Short): Unit = put(short)
|
||||
fun putInt(int: Int): Unit = put(int)
|
||||
fun putLong(long: Long): Unit = put(long)
|
||||
fun putFloat(float: Float): Unit = put(float)
|
||||
fun putDouble(double: Double): Unit = put(double)
|
||||
fun putChar(char: Char, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = put(char, byteOrder)
|
||||
fun putShort(short: Short, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = put(short, byteOrder)
|
||||
fun putInt(int: Int, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = put(int, byteOrder)
|
||||
fun putLong(long: Long, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = put(long, byteOrder)
|
||||
fun putFloat(float: Float, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = put(float, byteOrder)
|
||||
fun putDouble(double: Double, byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN): Unit = put(double, byteOrder)
|
||||
|
||||
fun putString(str: String): Int = put(str)
|
||||
fun putBuffer(buffer: ByteBuffer): Int = put(buffer)
|
||||
fun putBytes(byteArray: ByteArray, startIndex: Int = 0, endIndex: Int = byteArray.size - startIndex) =
|
||||
put(byteArray, startIndex, endIndex)
|
||||
|
||||
fun putChars(array: CharArray, index: Int = 0, size: Int = array.size - index) = put(array, index, size)
|
||||
fun putShorts(array: ShortArray, index: Int = 0, size: Int = array.size - index) = put(array, index, size)
|
||||
fun putInts(array: IntArray, index: Int = 0, size: Int = array.size - index) = put(array, index, size)
|
||||
fun putLongs(array: LongArray, index: Int = 0, size: Int = array.size - index) = put(array, index, size)
|
||||
fun putFloats(array: FloatArray, index: Int = 0, size: Int = array.size - index) = put(array, index, size)
|
||||
fun putDoubles(array: DoubleArray, index: Int = 0, size: Int = array.size - index) = put(array, index, size)
|
||||
fun putChars(
|
||||
array: CharArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) = put(array, index, size, byteOrder)
|
||||
|
||||
fun putShorts(
|
||||
array: ShortArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) = put(array, index, size, byteOrder)
|
||||
|
||||
fun putInts(
|
||||
array: IntArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) = put(array, index, size, byteOrder)
|
||||
|
||||
fun putLongs(
|
||||
array: LongArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) = put(array, index, size, byteOrder)
|
||||
|
||||
fun putFloats(
|
||||
array: FloatArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) = put(array, index, size, byteOrder)
|
||||
|
||||
fun putDoubles(
|
||||
array: DoubleArray,
|
||||
index: Int = 0,
|
||||
size: Int = array.size - index,
|
||||
byteOrder: ByteOrder = ByteOrder.BIG_ENDIAN,
|
||||
) = put(array, index, size, byteOrder)
|
||||
|
||||
fun fill(byte: Byte) {
|
||||
readPosition = 0
|
||||
@ -292,7 +408,7 @@ interface ByteBuffer : Closeable {
|
||||
writePosition = 0
|
||||
}
|
||||
|
||||
fun split(maxSize: Int): Array<out ByteBuffer> {
|
||||
fun split(maxSize: Int): List<ByteBuffer> {
|
||||
val size = (((capacity - 1) / maxSize) + 1).and(0x7fff_ffff)
|
||||
return Array(size) {
|
||||
if (it != size - 1) {
|
||||
@ -300,7 +416,7 @@ interface ByteBuffer : Closeable {
|
||||
} else {
|
||||
slice(it * maxSize, capacity - it * maxSize)
|
||||
}
|
||||
}
|
||||
}.asList()
|
||||
}
|
||||
|
||||
fun readAllSize(): Int {
|
||||
|
@ -87,7 +87,7 @@ fun ScatteringByteChannel.read(buffers: Array<out ByteBuffer>): Long {
|
||||
val bufferList = ArrayList<java.nio.ByteBuffer>()
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.buffers.forEach {
|
||||
bufferList.add(it.writeBuffer())
|
||||
}
|
||||
} else {
|
||||
@ -101,7 +101,7 @@ fun ScatteringByteChannel.read(buffers: Array<out ByteBuffer>): Long {
|
||||
var index = 0
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.buffers.forEach {
|
||||
it.finishWrite(bufferArray[index])
|
||||
}
|
||||
} else {
|
||||
@ -116,7 +116,7 @@ fun GatheringByteChannel.write(buffers: Array<out ByteBuffer>): Long {
|
||||
val bufferList = ArrayList<java.nio.ByteBuffer>()
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.buffers.forEach {
|
||||
bufferList.add(it.readBuffer())
|
||||
}
|
||||
} else {
|
||||
@ -130,7 +130,7 @@ fun GatheringByteChannel.write(buffers: Array<out ByteBuffer>): Long {
|
||||
var index = 0
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.buffers.forEach {
|
||||
it.finishRead(bufferArray[index])
|
||||
}
|
||||
} else {
|
||||
|
@ -3,15 +3,17 @@ package cn.tursom.buffer
|
||||
import cn.tursom.core.buffer.ByteBuffer
|
||||
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
|
||||
import java.nio.ByteOrder
|
||||
|
||||
@Suppress("unused")
|
||||
interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
val buffers: Array<out ByteBuffer> get() = toTypedArray()
|
||||
interface MultipleByteBuffer : Closeable, ByteBuffer {
|
||||
val buffers: List<ByteBuffer>
|
||||
val buffersArray: Array<out ByteBuffer> get() = buffers.toTypedArray()
|
||||
|
||||
fun append(buffer: ByteBuffer)
|
||||
|
||||
/**
|
||||
* 使用读 buffer,ByteBuffer 实现类有义务维护指针正常推进
|
||||
@ -41,7 +43,7 @@ interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
val bufferList = ArrayList<java.nio.ByteBuffer>()
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.buffers.forEach {
|
||||
bufferList.add(it.readBuffer())
|
||||
}
|
||||
} else {
|
||||
@ -55,7 +57,7 @@ interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
val bufferList = ArrayList<java.nio.ByteBuffer>()
|
||||
buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.buffers.forEach {
|
||||
bufferList.add(it.writeBuffer())
|
||||
}
|
||||
} else {
|
||||
@ -67,9 +69,9 @@ interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
|
||||
fun finishRead(buffers: List<java.nio.ByteBuffer>) {
|
||||
var index = 0
|
||||
forEach {
|
||||
this.buffers.forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.buffers.forEach {
|
||||
it.finishRead(buffers[index])
|
||||
index++
|
||||
}
|
||||
@ -82,39 +84,38 @@ interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
|
||||
fun finishWrite(buffers: List<java.nio.ByteBuffer>) {
|
||||
var index = 0
|
||||
forEach {
|
||||
if (it is MultipleByteBuffer) {
|
||||
it.forEach {
|
||||
it.finishWrite(buffers[index])
|
||||
this.buffers.forEach { subBuf ->
|
||||
if (subBuf is MultipleByteBuffer) {
|
||||
subBuf.buffers.forEach { writeBuf ->
|
||||
writeBuf.finishWrite(buffers[index])
|
||||
index++
|
||||
}
|
||||
} else {
|
||||
it.finishWrite(buffers[index])
|
||||
subBuf.finishWrite(buffers[index])
|
||||
index++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun close() = forEach(ByteBuffer::close)
|
||||
override fun slice(position: Int, size: Int): MultipleByteBuffer = ListByteBuffer(subList(position, position + size))
|
||||
override fun fill(byte: Byte) = forEach { it.fill(byte) }
|
||||
override fun clear() = forEach(ByteBuffer::clear)
|
||||
override fun reset() = forEach(ByteBuffer::reset)
|
||||
override fun close() = buffers.forEach(ByteBuffer::close)
|
||||
override fun slice(position: Int, size: Int): MultipleByteBuffer {
|
||||
return ListByteBuffer(ArrayList(buffers.subList(position, position + size)))
|
||||
}
|
||||
|
||||
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 val resized: Boolean get() = false
|
||||
override val hasArray: Boolean get() = false
|
||||
override val array: ByteArray
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val array: ByteArray get() = throw UnsupportedOperationException()
|
||||
override val arrayOffset: Int get() = 0
|
||||
override val capacity: Int
|
||||
get() {
|
||||
var capacity = 0
|
||||
forEach {
|
||||
capacity += it.capacity
|
||||
}
|
||||
return capacity
|
||||
}
|
||||
override val capacity: Int get() = buffers.sumOf { it.capacity }
|
||||
override val isReadable: Boolean get() = buffers.any { it.isReadable }
|
||||
override val isWriteable: Boolean get() = buffers.any { it.isWriteable }
|
||||
override val readable: Int get() = buffers.sumOf { it.readable }
|
||||
override val writeable: Int get() = buffers.sumOf { it.writeable }
|
||||
|
||||
override fun readBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
override fun writeBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
@ -124,12 +125,14 @@ interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
override fun resize(newSize: Int): Boolean = false
|
||||
|
||||
override fun get(): Byte
|
||||
override fun getChar(byteOrder: ByteOrder): Char = cn.tursom.core.toChar(byteOrder, ::get)
|
||||
override fun getShort(byteOrder: ByteOrder): Short = cn.tursom.core.toShort(byteOrder, ::get)
|
||||
override fun getInt(byteOrder: ByteOrder): Int = cn.tursom.core.toInt(byteOrder, ::get)
|
||||
override fun getLong(byteOrder: ByteOrder): Long = cn.tursom.core.toLong(byteOrder, ::get)
|
||||
override fun getFloat(byteOrder: ByteOrder): Float = cn.tursom.core.toFloat(byteOrder, ::get)
|
||||
override fun getDouble(byteOrder: ByteOrder): Double = cn.tursom.core.toDouble(byteOrder, ::get)
|
||||
|
||||
override fun getChar(byteOrder: ByteOrder): Char = cn.tursom.core.toChar(byteOrder) { get() }
|
||||
override fun getShort(byteOrder: ByteOrder): Short = cn.tursom.core.toShort(byteOrder) { get() }
|
||||
override fun getInt(byteOrder: ByteOrder): Int = cn.tursom.core.toInt(byteOrder) { get() }
|
||||
override fun getLong(byteOrder: ByteOrder): Long = cn.tursom.core.toLong(byteOrder) { get() }
|
||||
override fun getFloat(byteOrder: ByteOrder): Float = cn.tursom.core.toFloat(byteOrder) { get() }
|
||||
override fun getDouble(byteOrder: ByteOrder): Double = cn.tursom.core.toDouble(byteOrder) { get() }
|
||||
|
||||
override fun getBytes(size: Int): ByteArray {
|
||||
val buffer = ByteArray(size)
|
||||
buffer.indices.forEach {
|
||||
@ -174,13 +177,8 @@ interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
return write
|
||||
}
|
||||
|
||||
override fun put(byte: Byte): Unit
|
||||
override fun put(char: Char) = char.toBytes { put(it) }
|
||||
override fun put(short: Short) = short.toBytes { put(it) }
|
||||
override fun put(int: Int) = int.toBytes { put(it) }
|
||||
override fun put(long: Long) = long.toBytes { put(it) }
|
||||
override fun put(float: Float) = float.toBytes { put(it) }
|
||||
override fun put(double: Double) = double.toBytes { put(it) }
|
||||
override fun put(byte: Byte)
|
||||
|
||||
override fun put(byteArray: ByteArray, offset: Int, len: Int): Int {
|
||||
var write = 0
|
||||
byteArray.forEachIndex(offset, offset + len) {
|
||||
@ -200,6 +198,6 @@ interface MultipleByteBuffer : List<ByteBuffer>, Closeable, ByteBuffer {
|
||||
return read
|
||||
}
|
||||
|
||||
override fun split(maxSize: Int): Array<out ByteBuffer> = throw UnsupportedOperationException()
|
||||
override fun split(maxSize: Int): List<ByteBuffer> = throw UnsupportedOperationException()
|
||||
override fun readAllSize(): Int = throw UnsupportedOperationException()
|
||||
}
|
@ -3,5 +3,5 @@ package cn.tursom.core.buffer.impl
|
||||
import cn.tursom.core.buffer.ByteBuffer
|
||||
|
||||
class ArrayByteBuffer(
|
||||
override vararg val buffers: ByteBuffer
|
||||
) : ListByteBuffer(buffers.asList())
|
||||
vararg buffers: ByteBuffer,
|
||||
) : ListByteBuffer(ArrayList(buffers.asList()))
|
@ -2,59 +2,217 @@ package cn.tursom.core.buffer.impl
|
||||
|
||||
import cn.tursom.buffer.MultipleByteBuffer
|
||||
import cn.tursom.core.buffer.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
|
||||
open class ListByteBuffer(
|
||||
final override val buffers: MutableList<ByteBuffer> = ArrayList(),
|
||||
) : MultipleByteBuffer {
|
||||
private var readOperator = buffers.firstOrNull()
|
||||
private var writeOperator = buffers.firstOrNull()
|
||||
|
||||
private var buffersArrayCache: Array<out ByteBuffer>? = null
|
||||
override val buffersArray: Array<out ByteBuffer>
|
||||
get() {
|
||||
if (buffersArrayCache == null) {
|
||||
buffersArrayCache = buffers.toTypedArray()
|
||||
}
|
||||
return buffersArrayCache!!
|
||||
}
|
||||
|
||||
open class ListByteBuffer(val bufferList: List<ByteBuffer>) : MultipleByteBuffer, List<ByteBuffer> by bufferList {
|
||||
private var readOperator = bufferList.firstOrNull()
|
||||
private var writeOperator = bufferList.firstOrNull()
|
||||
override val hasArray: Boolean get() = false
|
||||
override val array: ByteArray
|
||||
get() = throw UnsupportedOperationException()
|
||||
override val array: ByteArray get() = throw UnsupportedOperationException()
|
||||
override val capacity: Int
|
||||
get() {
|
||||
var capacity = 0
|
||||
bufferList.forEach {
|
||||
buffers.forEach {
|
||||
capacity += it.capacity
|
||||
}
|
||||
return capacity
|
||||
}
|
||||
override val arrayOffset: Int get() = 0
|
||||
override var writePosition: Int = 0
|
||||
override var readPosition: Int = 0
|
||||
|
||||
var writeArrayPosition: Int = 0
|
||||
var readArrayPosition: Int = 0
|
||||
|
||||
override var writePosition: Int = buffers.sumOf { it.writePosition }
|
||||
override var readPosition: Int = buffers.sumOf { it.readPosition }
|
||||
|
||||
override val resized: Boolean get() = false
|
||||
|
||||
override val isReadable: Boolean
|
||||
get() {
|
||||
updateRead()
|
||||
return readOperator?.isReadable ?: false
|
||||
}
|
||||
override val isWriteable: Boolean
|
||||
get() {
|
||||
updateWrite()
|
||||
return writeOperator?.isWriteable ?: false
|
||||
}
|
||||
|
||||
override fun readBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
override fun writeBuffer(): java.nio.ByteBuffer = throw UnsupportedOperationException()
|
||||
|
||||
override fun append(buffer: ByteBuffer) {
|
||||
buffers.add(buffer)
|
||||
buffersArrayCache = null
|
||||
}
|
||||
|
||||
override fun slice(position: Int, size: Int, readPosition: Int, writePosition: Int): ByteBuffer =
|
||||
throw UnsupportedOperationException()
|
||||
|
||||
override fun resize(newSize: Int): Boolean = throw UnsupportedOperationException()
|
||||
|
||||
fun updateRead() {
|
||||
if (readOperator == null || readOperator!!.readable == 0) {
|
||||
readOperator = bufferList[readPosition++]
|
||||
while (readArrayPosition < buffers.size && (readOperator == null || !readOperator!!.isReadable)) {
|
||||
readOperator = buffers[readArrayPosition++]
|
||||
}
|
||||
}
|
||||
|
||||
fun updateWrite() {
|
||||
if (writeOperator == null || writeOperator!!.readable == 0) {
|
||||
writeOperator = bufferList[writePosition++]
|
||||
while (writeArrayPosition < buffers.size && (writeOperator == null || !writeOperator!!.isReadable)) {
|
||||
writeOperator = buffers[writeArrayPosition++]
|
||||
}
|
||||
}
|
||||
|
||||
override fun get(): Byte {
|
||||
updateRead()
|
||||
return readOperator!!.get()
|
||||
val get = readOperator!!.get()
|
||||
readPosition++
|
||||
return get
|
||||
}
|
||||
|
||||
override fun getChar(byteOrder: ByteOrder): Char {
|
||||
updateRead()
|
||||
return if (readOperator!!.readable >= 2) {
|
||||
val char = readOperator!!.getChar(byteOrder)
|
||||
readPosition += 2
|
||||
char
|
||||
} else {
|
||||
super.getChar(byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getShort(byteOrder: ByteOrder): Short {
|
||||
updateRead()
|
||||
return if (readOperator!!.readable >= 2) {
|
||||
val short = readOperator!!.getShort(byteOrder)
|
||||
readPosition += 2
|
||||
short
|
||||
} else {
|
||||
super.getShort(byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getInt(byteOrder: ByteOrder): Int {
|
||||
updateRead()
|
||||
return if (readOperator!!.readable >= 4) {
|
||||
val int = readOperator!!.getInt(byteOrder)
|
||||
readPosition += 4
|
||||
int
|
||||
} else {
|
||||
super.getInt(byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getLong(byteOrder: ByteOrder): Long {
|
||||
updateRead()
|
||||
return if (readOperator!!.readable >= 8) {
|
||||
val long = readOperator!!.getLong(byteOrder)
|
||||
readPosition += 8
|
||||
long
|
||||
} else {
|
||||
super.getLong(byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getFloat(byteOrder: ByteOrder): Float {
|
||||
updateRead()
|
||||
return if (readOperator!!.readable >= 4) {
|
||||
val float = readOperator!!.getFloat(byteOrder)
|
||||
readPosition += 4
|
||||
float
|
||||
} else {
|
||||
super.getFloat(byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getDouble(byteOrder: ByteOrder): Double {
|
||||
updateRead()
|
||||
return if (readOperator!!.readable >= 8) {
|
||||
val double = readOperator!!.getDouble(byteOrder)
|
||||
readPosition += 8
|
||||
double
|
||||
} else {
|
||||
super.getDouble(byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBytes(size: Int): ByteArray {
|
||||
return if (readOperator!!.readable >= size) {
|
||||
val bytes = readOperator!!.getBytes(size)
|
||||
readPosition += size
|
||||
bytes
|
||||
} else {
|
||||
super.getBytes(size)
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(byte: Byte) {
|
||||
TODO("Not yet implemented")
|
||||
updateWrite()
|
||||
writeOperator!!.put(byte)
|
||||
}
|
||||
|
||||
override fun split(maxSize: Int): Array<out ByteBuffer> {
|
||||
TODO("Not yet implemented")
|
||||
override fun put(char: Char, byteOrder: ByteOrder) {
|
||||
updateWrite()
|
||||
if (writeOperator!!.writeable > 2) {
|
||||
writeOperator!!.put(char, byteOrder)
|
||||
} else {
|
||||
super.put(char, byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun readAllSize(): Int {
|
||||
TODO("Not yet implemented")
|
||||
override fun put(short: Short, byteOrder: ByteOrder) {
|
||||
updateWrite()
|
||||
if (writeOperator!!.writeable > 2) {
|
||||
writeOperator!!.put(short, byteOrder)
|
||||
} else {
|
||||
super.put(short, byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(int: Int, byteOrder: ByteOrder) {
|
||||
updateWrite()
|
||||
if (writeOperator!!.writeable > 4) {
|
||||
writeOperator!!.put(int, byteOrder)
|
||||
} else {
|
||||
super.put(int, byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(long: Long, byteOrder: ByteOrder) {
|
||||
updateWrite()
|
||||
if (writeOperator!!.writeable > 8) {
|
||||
writeOperator!!.put(long, byteOrder)
|
||||
} else {
|
||||
super.put(long, byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(float: Float, byteOrder: ByteOrder) {
|
||||
updateWrite()
|
||||
if (writeOperator!!.writeable > 4) {
|
||||
writeOperator!!.put(float, byteOrder)
|
||||
} else {
|
||||
super.put(float, byteOrder)
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(double: Double, byteOrder: ByteOrder) {
|
||||
updateWrite()
|
||||
if (writeOperator!!.writeable > 8) {
|
||||
writeOperator!!.put(double, byteOrder)
|
||||
} else {
|
||||
super.put(double, byteOrder)
|
||||
}
|
||||
}
|
||||
}
|
@ -128,12 +128,48 @@ class NettyByteBuffer(
|
||||
}
|
||||
|
||||
override fun get(): Byte = byteBuf.readByte()
|
||||
override fun getChar(byteOrder: ByteOrder): Char = byteBuf.readChar()
|
||||
override fun getShort(byteOrder: ByteOrder): Short = byteBuf.readShort()
|
||||
override fun getInt(byteOrder: ByteOrder): Int = byteBuf.readInt()
|
||||
override fun getLong(byteOrder: ByteOrder): Long = byteBuf.readLong()
|
||||
override fun getFloat(byteOrder: ByteOrder): Float = byteBuf.readFloat()
|
||||
override fun getDouble(byteOrder: ByteOrder): Double = byteBuf.readDouble()
|
||||
//override fun getChar(): Char = byteBuf.readChar()
|
||||
//override fun getShort(): Short = byteBuf.readShort()
|
||||
//override fun getInt(): Int = byteBuf.readInt()
|
||||
//override fun getLong(): Long = byteBuf.readLong()
|
||||
//override fun getFloat(): Float = byteBuf.readFloat()
|
||||
//override fun getDouble(): Double = byteBuf.readDouble()
|
||||
|
||||
override fun getChar(byteOrder: ByteOrder): Char = when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.readChar()
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.readShortLE().toInt().toChar()
|
||||
else -> throw IllegalArgumentException("")
|
||||
}
|
||||
|
||||
override fun getShort(byteOrder: ByteOrder): Short = when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.readShort()
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.readShortLE()
|
||||
else -> throw IllegalArgumentException("")
|
||||
}
|
||||
|
||||
override fun getInt(byteOrder: ByteOrder): Int = when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.readInt()
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.readIntLE()
|
||||
else -> throw IllegalArgumentException("")
|
||||
}
|
||||
|
||||
override fun getLong(byteOrder: ByteOrder): Long = when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.readLong()
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.readLongLE()
|
||||
else -> throw IllegalArgumentException("")
|
||||
}
|
||||
|
||||
override fun getFloat(byteOrder: ByteOrder): Float = when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.readFloat()
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.readFloatLE()
|
||||
else -> throw IllegalArgumentException("")
|
||||
}
|
||||
|
||||
override fun getDouble(byteOrder: ByteOrder): Double = when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.readDouble()
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.readDoubleLE()
|
||||
else -> throw IllegalArgumentException("")
|
||||
}
|
||||
|
||||
override fun getBytes(size: Int): ByteArray {
|
||||
val bytes = ByteArray(size)
|
||||
@ -163,28 +199,52 @@ class NettyByteBuffer(
|
||||
byteBuf.writeByte(byte.toInt())
|
||||
}
|
||||
|
||||
override fun put(char: Char) {
|
||||
byteBuf.writeChar(char.code)
|
||||
override fun put(char: Char, byteOrder: ByteOrder) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.writeChar(char.code)
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.writeShortLE(char.code)
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(short: Short) {
|
||||
byteBuf.writeShort(short.toInt())
|
||||
override fun put(short: Short, byteOrder: ByteOrder) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.writeShort(short.toInt())
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.writeShortLE(short.toInt())
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(int: Int) {
|
||||
byteBuf.writeInt(int)
|
||||
override fun put(int: Int, byteOrder: ByteOrder) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.writeInt(int)
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.writeIntLE(int)
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(long: Long) {
|
||||
byteBuf.writeLong(long)
|
||||
override fun put(long: Long, byteOrder: ByteOrder) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.writeLong(long)
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.writeLongLE(long)
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(float: Float) {
|
||||
byteBuf.writeFloat(float)
|
||||
override fun put(float: Float, byteOrder: ByteOrder) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.writeFloat(float)
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.writeFloatLE(float)
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(double: Double) {
|
||||
byteBuf.writeDouble(double)
|
||||
override fun put(double: Double, byteOrder: ByteOrder) {
|
||||
when (byteOrder) {
|
||||
ByteOrder.BIG_ENDIAN -> byteBuf.writeDouble(double)
|
||||
ByteOrder.LITTLE_ENDIAN -> byteBuf.writeDoubleLE(double)
|
||||
else -> throw IllegalArgumentException()
|
||||
}
|
||||
}
|
||||
|
||||
override fun put(str: String): Int {
|
||||
|
@ -17,23 +17,25 @@ interface AsyncChannel : Closeable {
|
||||
|
||||
suspend fun write(buffer: Array<out ByteBuffer>, timeout: Long = 0L): Long
|
||||
suspend fun read(buffer: Array<out ByteBuffer>, timeout: Long = 0L): Long
|
||||
suspend fun write(buffer: List<ByteBuffer>, timeout: Long = 0L): Long = write(buffer.toTypedArray(), timeout)
|
||||
suspend fun read(buffer: List<ByteBuffer>, timeout: Long = 0L): Long = read(buffer.toTypedArray(), timeout)
|
||||
suspend fun write(buffer: ByteBuffer, timeout: Long = 0L): Int = write(arrayOf(buffer), timeout).toInt()
|
||||
suspend fun read(buffer: ByteBuffer, timeout: Long = 0L): Int = read(arrayOf(buffer), timeout).toInt()
|
||||
suspend fun write(buffer: MultipleByteBuffer, timeout: Long = 0L): Long = write(buffer.buffers, timeout)
|
||||
suspend fun read(buffer: MultipleByteBuffer, timeout: Long = 0L): Long = read(buffer.buffers, timeout)
|
||||
suspend fun write(buffer: MultipleByteBuffer, timeout: Long = 0L): Long = write(buffer.buffersArray, timeout)
|
||||
suspend fun read(buffer: MultipleByteBuffer, timeout: Long = 0L): Long = read(buffer.buffersArray, timeout)
|
||||
|
||||
suspend fun write(
|
||||
file: FileChannel,
|
||||
position: Long,
|
||||
count: Long,
|
||||
timeout: Long = 0
|
||||
timeout: Long = 0,
|
||||
): Long
|
||||
|
||||
suspend fun read(
|
||||
file: FileChannel,
|
||||
position: Long,
|
||||
count: Long,
|
||||
timeout: Long = 0
|
||||
timeout: Long = 0,
|
||||
): Long
|
||||
|
||||
suspend fun write(bytes: ByteArray, offset: Int = 0, len: Int = bytes.size - offset): Int {
|
||||
|
Loading…
Reference in New Issue
Block a user