mirror of
https://github.com/tursom/TursomServer.git
synced 2025-02-08 10:40:48 +08:00
添加了两个Demo
This commit is contained in:
parent
cc8cb7c333
commit
acfe8fda0a
42
database/src/test/kotlin/Demo.kt
Normal file
42
database/src/test/kotlin/Demo.kt
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
import cn.tursom.database.annotation.*
|
||||||
|
import cn.tursom.database.clauses.ClauseMaker
|
||||||
|
import cn.tursom.database.select
|
||||||
|
import cn.tursom.database.sqlite.SQLiteHelper
|
||||||
|
import cn.tursom.database.SqlUtils.tableName
|
||||||
|
import org.jetbrains.annotations.NotNull
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用来访问数据库的数据类
|
||||||
|
*/
|
||||||
|
// 自定义表名
|
||||||
|
@TableName("demo")
|
||||||
|
// 如果未自定义表名,则以类名作为表名
|
||||||
|
data class Demo(
|
||||||
|
// 定义一个非空,自增的整数列 `id` 作为主键
|
||||||
|
@NotNull @AutoIncrement @PrimaryKey val id: Int? = null,
|
||||||
|
// 定义一个非空,唯一的字符串列 `name`
|
||||||
|
@NotNull @Unique @FieldType("varchar(32)") val name: String,
|
||||||
|
// 定义一个非空小数列 `money`,其默认值为0
|
||||||
|
@NotNull @Default("0") @FieldName("money") val money: Double = 0.0
|
||||||
|
)
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
// 获取数据库访问协助对象
|
||||||
|
val helper = SQLiteHelper("demo.db")
|
||||||
|
|
||||||
|
// 插入数据
|
||||||
|
helper.insert(Demo(name = "tursom"))
|
||||||
|
|
||||||
|
// 更新数据
|
||||||
|
helper.update(Demo(name = "tursom", money = 100.0), where = ClauseMaker.make {
|
||||||
|
!Demo::name equal "tursom"
|
||||||
|
})
|
||||||
|
|
||||||
|
// 获取数据
|
||||||
|
val data = helper.select(Demo::class.java, where = ClauseMaker {
|
||||||
|
(!Demo::id greaterThan !0) and (!Demo::id lessThan !10)
|
||||||
|
})
|
||||||
|
|
||||||
|
// 删除数据
|
||||||
|
helper.delete(Demo::class.java.tableName, where = ClauseMaker.make { !Demo::name equal "tursom" })
|
||||||
|
}
|
30
socket/socket-async/src/test/kotlin/Demo.kt
Normal file
30
socket/socket-async/src/test/kotlin/Demo.kt
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import cn.tursom.core.bytebuffer.ByteArrayAdvanceByteBuffer
|
||||||
|
import cn.tursom.core.pool.DirectMemoryPool
|
||||||
|
import cn.tursom.core.pool.usingAdvanceByteBuffer
|
||||||
|
import cn.tursom.socket.server.AsyncNioServer
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
// 服务器端口,可任意指定
|
||||||
|
val port = 12345
|
||||||
|
|
||||||
|
// 创建一个直接内存池,每个块是1024字节,共有256个快
|
||||||
|
val memoryPool = DirectMemoryPool(1024, 256)
|
||||||
|
// 创建服务器对象
|
||||||
|
val server = AsyncNioServer(port) {
|
||||||
|
// 这里处理业务逻辑,套接字对象被以 this 的方式传进来
|
||||||
|
// 从内存池中获取一个内存块
|
||||||
|
memoryPool.usingAdvanceByteBuffer {
|
||||||
|
// 检查是否获取成功,不成功就创建一个堆缓冲
|
||||||
|
val buffer = it ?: ByteArrayAdvanceByteBuffer(1024)
|
||||||
|
// 从套接字中读数据,五秒之内没有数据就抛出异常
|
||||||
|
read(buffer, 5000)
|
||||||
|
// 输出读取到的数据
|
||||||
|
println("${System.currentTimeMillis()}: recv from ${channel.remoteAddress}: ${buffer.toString(buffer.readableSize)}")
|
||||||
|
// 原封不动的返回数据
|
||||||
|
write(buffer)
|
||||||
|
// 代码块结束后,框架会自动释放连接
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 创建一个新线程去启动服务器
|
||||||
|
Thread(server, "echoServerStarter").start()
|
||||||
|
}
|
@ -11,7 +11,7 @@ import java.util.concurrent.atomic.AtomicBoolean
|
|||||||
class ThreadPoolNioThread(
|
class ThreadPoolNioThread(
|
||||||
val threadName: String = "",
|
val threadName: String = "",
|
||||||
override val selector: Selector = Selector.open(),
|
override val selector: Selector = Selector.open(),
|
||||||
override val isDaemon: Boolean = true,
|
override val isDaemon: Boolean = false,
|
||||||
override val workLoop: (thread: INioThread) -> Unit
|
override val workLoop: (thread: INioThread) -> Unit
|
||||||
) : INioThread {
|
) : INioThread {
|
||||||
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
||||||
|
@ -6,7 +6,7 @@ import java.nio.channels.SelectableChannel
|
|||||||
class ThreadPoolWorkerGroup(
|
class ThreadPoolWorkerGroup(
|
||||||
val poolSize: Int = Runtime.getRuntime().availableProcessors(),
|
val poolSize: Int = Runtime.getRuntime().availableProcessors(),
|
||||||
val groupName: String = "",
|
val groupName: String = "",
|
||||||
override val isDaemon: Boolean = true,
|
override val isDaemon: Boolean = false,
|
||||||
val worker: (thread: INioThread) -> Unit
|
val worker: (thread: INioThread) -> Unit
|
||||||
) : IWorkerGroup {
|
) : IWorkerGroup {
|
||||||
val workerGroup = Array(poolSize) {
|
val workerGroup = Array(poolSize) {
|
||||||
|
@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean
|
|||||||
class WorkerLoopNioThread(
|
class WorkerLoopNioThread(
|
||||||
val threadName: String = "nioLoopThread",
|
val threadName: String = "nioLoopThread",
|
||||||
override val selector: Selector = Selector.open(),
|
override val selector: Selector = Selector.open(),
|
||||||
override val isDaemon: Boolean = true,
|
override val isDaemon: Boolean = false,
|
||||||
override val workLoop: (thread: INioThread) -> Unit
|
override val workLoop: (thread: INioThread) -> Unit
|
||||||
) : INioThread {
|
) : INioThread {
|
||||||
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
||||||
|
@ -30,7 +30,7 @@ class NioServer(
|
|||||||
protocol: INioProtocol,
|
protocol: INioProtocol,
|
||||||
backLog: Int = 50
|
backLog: Int = 50
|
||||||
) : this(port, protocol, backLog, { name, workLoop ->
|
) : this(port, protocol, backLog, { name, workLoop ->
|
||||||
WorkerLoopNioThread(name, workLoop = workLoop)
|
WorkerLoopNioThread(name, workLoop = workLoop, isDaemon = false)
|
||||||
})
|
})
|
||||||
|
|
||||||
override fun run() {
|
override fun run() {
|
||||||
|
@ -7,423 +7,429 @@ import java.nio.ByteBuffer
|
|||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
interface AdvanceByteBuffer {
|
interface AdvanceByteBuffer {
|
||||||
val nioBuffer: ByteBuffer
|
val nioBuffer: ByteBuffer
|
||||||
val nioBuffers: Array<out ByteBuffer> get() = arrayOf(nioBuffer)
|
val nioBuffers: Array<out ByteBuffer> get() = arrayOf(nioBuffer)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 各种位置变量
|
* 各种位置变量
|
||||||
*/
|
*/
|
||||||
val readOnly: Boolean
|
val readOnly: Boolean
|
||||||
val bufferCount: Int get() = 1
|
val bufferCount: Int get() = 1
|
||||||
|
|
||||||
var writePosition: Int
|
var writePosition: Int
|
||||||
var limit: Int
|
var limit: Int
|
||||||
val capacity: Int
|
val capacity: Int
|
||||||
val hasArray: Boolean
|
val hasArray: Boolean
|
||||||
val array: ByteArray
|
val array: ByteArray
|
||||||
val arrayOffset: Int
|
val arrayOffset: Int
|
||||||
var readPosition: Int
|
var readPosition: Int
|
||||||
val readOffset: Int get() = arrayOffset + readPosition
|
val readOffset: Int get() = arrayOffset + readPosition
|
||||||
val readableSize: Int
|
val readableSize: Int
|
||||||
val available: Int get() = readableSize
|
val available: Int get() = readableSize
|
||||||
val writeOffset: Int get() = arrayOffset + writePosition
|
val writeOffset: Int get() = arrayOffset + writePosition
|
||||||
val writeableSize: Int get() = limit - writePosition
|
val writeableSize: Int get() = limit - writePosition
|
||||||
val size: Int
|
val size: Int
|
||||||
val readMode: Boolean
|
val readMode: Boolean
|
||||||
|
|
||||||
|
|
||||||
fun readMode()
|
fun readMode()
|
||||||
fun resumeWriteMode(usedSize: Int = 0)
|
fun resumeWriteMode(usedSize: Int = 0)
|
||||||
|
|
||||||
fun needReadSize(size: Int) {
|
fun needReadSize(size: Int) {
|
||||||
if (readableSize < size) throw OutOfBufferException()
|
if (readableSize < size) throw OutOfBufferException()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun useReadSize(size: Int): Int {
|
fun useReadSize(size: Int): Int {
|
||||||
needReadSize(size)
|
needReadSize(size)
|
||||||
readPosition += size
|
readPosition += size
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
fun take(size: Int): Int {
|
fun take(size: Int): Int {
|
||||||
needReadSize(size)
|
needReadSize(size)
|
||||||
val offset = readOffset
|
val offset = readOffset
|
||||||
readPosition += size
|
readPosition += size
|
||||||
return offset
|
return offset
|
||||||
}
|
}
|
||||||
|
|
||||||
fun push(size: Int): Int {
|
fun push(size: Int): Int {
|
||||||
val offset = writeOffset
|
val offset = writeOffset
|
||||||
writePosition += size
|
writePosition += size
|
||||||
return offset
|
return offset
|
||||||
}
|
}
|
||||||
|
|
||||||
fun readAllSize() = useReadSize(readableSize)
|
fun readAllSize() = useReadSize(readableSize)
|
||||||
fun takeAll() = take(readableSize)
|
fun takeAll() = take(readableSize)
|
||||||
|
|
||||||
fun clear()
|
fun clear()
|
||||||
|
|
||||||
fun reset() {
|
fun reset() {
|
||||||
if (hasArray) {
|
if (hasArray) {
|
||||||
array.copyInto(array, arrayOffset, readOffset, arrayOffset + writePosition)
|
array.copyInto(array, arrayOffset, readOffset, arrayOffset + writePosition)
|
||||||
writePosition = readableSize
|
writePosition = readableSize
|
||||||
readPosition = 0
|
readPosition = 0
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
nioBuffer.compact()
|
nioBuffer.compact()
|
||||||
val writePosition = readPosition
|
val writePosition = readPosition
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
readPosition = 0
|
readPosition = 0
|
||||||
this.writePosition = writePosition
|
this.writePosition = writePosition
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun reset(outputStream: OutputStream) {
|
fun reset(outputStream: OutputStream) {
|
||||||
if (hasArray) {
|
if (hasArray) {
|
||||||
outputStream.write(array, readOffset, arrayOffset + writePosition)
|
outputStream.write(array, readOffset, arrayOffset + writePosition)
|
||||||
} else {
|
} else {
|
||||||
outputStream.write(getBytes())
|
outputStream.write(getBytes())
|
||||||
}
|
}
|
||||||
writePosition = 0
|
writePosition = 0
|
||||||
readPosition = 0
|
readPosition = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
fun requireAvailableSize(size: Int) {
|
fun requireAvailableSize(size: Int) {
|
||||||
if (limit - readPosition < size) reset()
|
if (limit - readPosition < size) reset()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 数据获取方法
|
* 数据获取方法
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
fun get(): Byte = if (readMode) {
|
fun get(): Byte = if (readMode) {
|
||||||
nioBuffer.get()
|
nioBuffer.get()
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val value = nioBuffer.get()
|
val value = nioBuffer.get()
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getChar(): Char = if (readMode) {
|
fun getChar(): Char = if (readMode) {
|
||||||
nioBuffer.char
|
nioBuffer.char
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val value = nioBuffer.char
|
val value = nioBuffer.char
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getShort(): Short = if (readMode) {
|
fun getShort(): Short = if (readMode) {
|
||||||
nioBuffer.short
|
nioBuffer.short
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val value = nioBuffer.short
|
val value = nioBuffer.short
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getInt(): Int = if (readMode) {
|
fun getInt(): Int = if (readMode) {
|
||||||
nioBuffer.int
|
nioBuffer.int
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val value = nioBuffer.int
|
val value = nioBuffer.int
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getLong(): Long = if (readMode) {
|
fun getLong(): Long = if (readMode) {
|
||||||
nioBuffer.long
|
nioBuffer.long
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val value = nioBuffer.long
|
val value = nioBuffer.long
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getFloat(): Float = if (readMode) {
|
fun getFloat(): Float = if (readMode) {
|
||||||
nioBuffer.float
|
nioBuffer.float
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val value = nioBuffer.float
|
val value = nioBuffer.float
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getDouble(): Double = if (readMode) {
|
fun getDouble(): Double = if (readMode) {
|
||||||
nioBuffer.double
|
nioBuffer.double
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val value = nioBuffer.double
|
val value = nioBuffer.double
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
value
|
value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun getBytes(): ByteArray = if (readMode) {
|
fun getBytes(): ByteArray = if (readMode) {
|
||||||
val bytes = ByteArray(readableSize)
|
val bytes = ByteArray(readableSize)
|
||||||
nioBuffer.get(bytes)
|
nioBuffer.get(bytes)
|
||||||
readPosition = writePosition
|
readPosition = writePosition
|
||||||
bytes
|
bytes
|
||||||
} else {
|
} else {
|
||||||
readMode()
|
readMode()
|
||||||
val bytes = ByteArray(readableSize)
|
val bytes = ByteArray(readableSize)
|
||||||
nioBuffer.get(bytes)
|
nioBuffer.get(bytes)
|
||||||
readPosition = writePosition
|
readPosition = writePosition
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
bytes
|
bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun getString(size: Int = readableSize): String = String(getBytes())
|
fun getString(size: Int = readableSize): String = String(getBytes())
|
||||||
|
fun toString(size: Int): String {
|
||||||
|
val rp = readPosition
|
||||||
|
val bytes = getBytes()
|
||||||
|
readPosition = rp
|
||||||
|
return String(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
fun writeTo(buffer: ByteArray, bufferOffset: Int = 0, size: Int = min(readableSize, buffer.size)): Int {
|
fun writeTo(buffer: ByteArray, bufferOffset: Int = 0, size: Int = min(readableSize, buffer.size)): Int {
|
||||||
val readSize = min(readableSize, size)
|
val readSize = min(readableSize, size)
|
||||||
if (hasArray) {
|
if (hasArray) {
|
||||||
array.copyInto(buffer, bufferOffset, readOffset, readOffset + readSize)
|
array.copyInto(buffer, bufferOffset, readOffset, readOffset + readSize)
|
||||||
readPosition += readOffset
|
readPosition += readOffset
|
||||||
reset()
|
reset()
|
||||||
} else {
|
} else {
|
||||||
readBuffer {
|
readBuffer {
|
||||||
it.put(buffer, bufferOffset, readSize)
|
it.put(buffer, bufferOffset, readSize)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return readSize
|
return readSize
|
||||||
}
|
}
|
||||||
|
|
||||||
fun writeTo(os: OutputStream): Int {
|
fun writeTo(os: OutputStream): Int {
|
||||||
val size = readableSize
|
val size = readableSize
|
||||||
if (hasArray) {
|
if (hasArray) {
|
||||||
os.write(array, arrayOffset + readPosition, size)
|
os.write(array, arrayOffset + readPosition, size)
|
||||||
readPosition += size
|
readPosition += size
|
||||||
reset()
|
reset()
|
||||||
} else {
|
} else {
|
||||||
val buffer = ByteArray(1024)
|
val buffer = ByteArray(1024)
|
||||||
readBuffer {
|
readBuffer {
|
||||||
while (it.remaining() > 0) {
|
while (it.remaining() > 0) {
|
||||||
it.put(buffer)
|
it.put(buffer)
|
||||||
os.write(buffer)
|
os.write(buffer)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
fun writeTo(buffer: AdvanceByteBuffer): Int {
|
fun writeTo(buffer: AdvanceByteBuffer): Int {
|
||||||
val size = min(readableSize, buffer.writeableSize)
|
val size = min(readableSize, buffer.writeableSize)
|
||||||
if (hasArray && buffer.hasArray) {
|
if (hasArray && buffer.hasArray) {
|
||||||
array.copyInto(buffer.array, buffer.writeOffset, readOffset, readOffset + size)
|
array.copyInto(buffer.array, buffer.writeOffset, readOffset, readOffset + size)
|
||||||
buffer.writePosition += size
|
buffer.writePosition += size
|
||||||
readPosition += size
|
readPosition += size
|
||||||
reset()
|
reset()
|
||||||
} else {
|
} else {
|
||||||
readBuffer {
|
readBuffer {
|
||||||
buffer.nioBuffer.put(it)
|
buffer.nioBuffer.put(it)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return size
|
return size
|
||||||
}
|
}
|
||||||
|
|
||||||
fun toByteArray() = getBytes()
|
fun toByteArray() = getBytes()
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 数据写入方法
|
* 数据写入方法
|
||||||
*/
|
*/
|
||||||
|
|
||||||
fun put(byte: Byte) {
|
fun put(byte: Byte) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.put(byte)
|
nioBuffer.put(byte)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.put(byte)
|
nioBuffer.put(byte)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(char: Char) {
|
fun put(char: Char) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.putChar(char)
|
nioBuffer.putChar(char)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.putChar(char)
|
nioBuffer.putChar(char)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(short: Short) {
|
fun put(short: Short) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.putShort(short)
|
nioBuffer.putShort(short)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.putShort(short)
|
nioBuffer.putShort(short)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(int: Int) {
|
fun put(int: Int) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.putInt(int)
|
nioBuffer.putInt(int)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.putInt(int)
|
nioBuffer.putInt(int)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(long: Long) {
|
fun put(long: Long) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.putLong(long)
|
nioBuffer.putLong(long)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.putLong(long)
|
nioBuffer.putLong(long)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(float: Float) {
|
fun put(float: Float) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.putFloat(float)
|
nioBuffer.putFloat(float)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.putFloat(float)
|
nioBuffer.putFloat(float)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(double: Double) {
|
fun put(double: Double) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.putDouble(double)
|
nioBuffer.putDouble(double)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.putDouble(double)
|
nioBuffer.putDouble(double)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(str: String) {
|
fun put(str: String) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.put(str.toByteArray())
|
nioBuffer.put(str.toByteArray())
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.put(str.toByteArray())
|
nioBuffer.put(str.toByteArray())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(byteArray: ByteArray, startIndex: Int = 0, endIndex: Int = byteArray.size) {
|
fun put(byteArray: ByteArray, startIndex: Int = 0, endIndex: Int = byteArray.size) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(array: CharArray, index: Int = 0, size: Int = array.size - index) {
|
fun put(array: CharArray, index: Int = 0, size: Int = array.size - index) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(array: ShortArray, index: Int = 0, size: Int = array.size - index) {
|
fun put(array: ShortArray, index: Int = 0, size: Int = array.size - index) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(array: IntArray, index: Int = 0, size: Int = array.size - index) {
|
fun put(array: IntArray, index: Int = 0, size: Int = array.size - index) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(array: LongArray, index: Int = 0, size: Int = array.size - index) {
|
fun put(array: LongArray, index: Int = 0, size: Int = array.size - index) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(array: FloatArray, index: Int = 0, size: Int = array.size - index) {
|
fun put(array: FloatArray, index: Int = 0, size: Int = array.size - index) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun put(array: DoubleArray, index: Int = 0, size: Int = array.size - index) {
|
fun put(array: DoubleArray, index: Int = 0, size: Int = array.size - index) {
|
||||||
if (readMode) {
|
if (readMode) {
|
||||||
resumeWriteMode()
|
resumeWriteMode()
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
readMode()
|
readMode()
|
||||||
} else {
|
} else {
|
||||||
array.forEachIndex(index, index + size - 1, this::put)
|
array.forEachIndex(index, index + size - 1, this::put)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun peekString(size: Int = readableSize): String {
|
fun peekString(size: Int = readableSize): String {
|
||||||
val readP = readPosition
|
val readP = readPosition
|
||||||
val str = getString(size)
|
val str = getString(size)
|
||||||
readPosition = readP
|
readPosition = readP
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T> readBuffer(action: (nioBuffer: ByteBuffer) -> T): T = readNioBuffer(action)
|
fun <T> readBuffer(action: (nioBuffer: ByteBuffer) -> T): T = readNioBuffer(action)
|
||||||
fun <T> writeBuffer(action: (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer(action)
|
fun <T> writeBuffer(action: (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer(action)
|
||||||
|
|
||||||
suspend fun <T> readSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = readNioBuffer { action(it) }
|
suspend fun <T> readSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = readNioBuffer { action(it) }
|
||||||
suspend fun <T> writeSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer { action(it) }
|
suspend fun <T> writeSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer { action(it) }
|
||||||
|
|
||||||
fun split(from: Int = readPosition, to: Int = writePosition): AdvanceByteBuffer {
|
fun split(from: Int = readPosition, to: Int = writePosition): AdvanceByteBuffer {
|
||||||
return if (hasArray) {
|
return if (hasArray) {
|
||||||
ByteArrayAdvanceByteBuffer(array, arrayOffset + readPosition, to - from)
|
ByteArrayAdvanceByteBuffer(array, arrayOffset + readPosition, to - from)
|
||||||
} else {
|
} else {
|
||||||
throw NotImplementedException()
|
throw NotImplementedException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T> AdvanceByteBuffer.readNioBuffer(action: (nioBuffer: ByteBuffer) -> T): T {
|
inline fun <T> AdvanceByteBuffer.readNioBuffer(action: (nioBuffer: ByteBuffer) -> T): T {
|
||||||
readMode()
|
readMode()
|
||||||
val buffer = nioBuffer
|
val buffer = nioBuffer
|
||||||
val position = nioBuffer.position()
|
val position = nioBuffer.position()
|
||||||
return try {
|
return try {
|
||||||
action(buffer)
|
action(buffer)
|
||||||
} finally {
|
} finally {
|
||||||
resumeWriteMode(buffer.position() - position)
|
resumeWriteMode(buffer.position() - position)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline fun <T> AdvanceByteBuffer.writeNioBuffer(action: (nioBuffer: ByteBuffer) -> T): T {
|
inline fun <T> AdvanceByteBuffer.writeNioBuffer(action: (nioBuffer: ByteBuffer) -> T): T {
|
||||||
val buffer = nioBuffer
|
val buffer = nioBuffer
|
||||||
val position = writePosition
|
val position = writePosition
|
||||||
val bufferPosition = nioBuffer.position()
|
val bufferPosition = nioBuffer.position()
|
||||||
return try {
|
return try {
|
||||||
action(buffer)
|
action(buffer)
|
||||||
} finally {
|
} finally {
|
||||||
writePosition = position + (buffer.position() - bufferPosition)
|
writePosition = position + (buffer.position() - bufferPosition)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user