mirror of
https://github.com/tursom/TursomServer.git
synced 2025-02-01 07:11:18 +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(
|
||||
val threadName: String = "",
|
||||
override val selector: Selector = Selector.open(),
|
||||
override val isDaemon: Boolean = true,
|
||||
override val isDaemon: Boolean = false,
|
||||
override val workLoop: (thread: INioThread) -> Unit
|
||||
) : INioThread {
|
||||
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
||||
|
@ -6,7 +6,7 @@ import java.nio.channels.SelectableChannel
|
||||
class ThreadPoolWorkerGroup(
|
||||
val poolSize: Int = Runtime.getRuntime().availableProcessors(),
|
||||
val groupName: String = "",
|
||||
override val isDaemon: Boolean = true,
|
||||
override val isDaemon: Boolean = false,
|
||||
val worker: (thread: INioThread) -> Unit
|
||||
) : IWorkerGroup {
|
||||
val workerGroup = Array(poolSize) {
|
||||
|
@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean
|
||||
class WorkerLoopNioThread(
|
||||
val threadName: String = "nioLoopThread",
|
||||
override val selector: Selector = Selector.open(),
|
||||
override val isDaemon: Boolean = true,
|
||||
override val isDaemon: Boolean = false,
|
||||
override val workLoop: (thread: INioThread) -> Unit
|
||||
) : INioThread {
|
||||
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
||||
|
@ -30,7 +30,7 @@ class NioServer(
|
||||
protocol: INioProtocol,
|
||||
backLog: Int = 50
|
||||
) : this(port, protocol, backLog, { name, workLoop ->
|
||||
WorkerLoopNioThread(name, workLoop = workLoop)
|
||||
WorkerLoopNioThread(name, workLoop = workLoop, isDaemon = false)
|
||||
})
|
||||
|
||||
override fun run() {
|
||||
|
@ -7,423 +7,429 @@ import java.nio.ByteBuffer
|
||||
import kotlin.math.min
|
||||
|
||||
interface AdvanceByteBuffer {
|
||||
val nioBuffer: ByteBuffer
|
||||
val nioBuffers: Array<out ByteBuffer> get() = arrayOf(nioBuffer)
|
||||
val nioBuffer: ByteBuffer
|
||||
val nioBuffers: Array<out ByteBuffer> get() = arrayOf(nioBuffer)
|
||||
|
||||
/**
|
||||
* 各种位置变量
|
||||
*/
|
||||
val readOnly: Boolean
|
||||
val bufferCount: Int get() = 1
|
||||
/**
|
||||
* 各种位置变量
|
||||
*/
|
||||
val readOnly: Boolean
|
||||
val bufferCount: Int get() = 1
|
||||
|
||||
var writePosition: Int
|
||||
var limit: Int
|
||||
val capacity: Int
|
||||
val hasArray: Boolean
|
||||
val array: ByteArray
|
||||
val arrayOffset: Int
|
||||
var readPosition: Int
|
||||
val readOffset: Int get() = arrayOffset + readPosition
|
||||
val readableSize: Int
|
||||
val available: Int get() = readableSize
|
||||
val writeOffset: Int get() = arrayOffset + writePosition
|
||||
val writeableSize: Int get() = limit - writePosition
|
||||
val size: Int
|
||||
val readMode: Boolean
|
||||
var writePosition: Int
|
||||
var limit: Int
|
||||
val capacity: Int
|
||||
val hasArray: Boolean
|
||||
val array: ByteArray
|
||||
val arrayOffset: Int
|
||||
var readPosition: Int
|
||||
val readOffset: Int get() = arrayOffset + readPosition
|
||||
val readableSize: Int
|
||||
val available: Int get() = readableSize
|
||||
val writeOffset: Int get() = arrayOffset + writePosition
|
||||
val writeableSize: Int get() = limit - writePosition
|
||||
val size: Int
|
||||
val readMode: Boolean
|
||||
|
||||
|
||||
fun readMode()
|
||||
fun resumeWriteMode(usedSize: Int = 0)
|
||||
fun readMode()
|
||||
fun resumeWriteMode(usedSize: Int = 0)
|
||||
|
||||
fun needReadSize(size: Int) {
|
||||
if (readableSize < size) throw OutOfBufferException()
|
||||
}
|
||||
fun needReadSize(size: Int) {
|
||||
if (readableSize < size) throw OutOfBufferException()
|
||||
}
|
||||
|
||||
fun useReadSize(size: Int): Int {
|
||||
needReadSize(size)
|
||||
readPosition += size
|
||||
return size
|
||||
}
|
||||
fun useReadSize(size: Int): Int {
|
||||
needReadSize(size)
|
||||
readPosition += size
|
||||
return size
|
||||
}
|
||||
|
||||
fun take(size: Int): Int {
|
||||
needReadSize(size)
|
||||
val offset = readOffset
|
||||
readPosition += size
|
||||
return offset
|
||||
}
|
||||
fun take(size: Int): Int {
|
||||
needReadSize(size)
|
||||
val offset = readOffset
|
||||
readPosition += size
|
||||
return offset
|
||||
}
|
||||
|
||||
fun push(size: Int): Int {
|
||||
val offset = writeOffset
|
||||
writePosition += size
|
||||
return offset
|
||||
}
|
||||
fun push(size: Int): Int {
|
||||
val offset = writeOffset
|
||||
writePosition += size
|
||||
return offset
|
||||
}
|
||||
|
||||
fun readAllSize() = useReadSize(readableSize)
|
||||
fun takeAll() = take(readableSize)
|
||||
fun readAllSize() = useReadSize(readableSize)
|
||||
fun takeAll() = take(readableSize)
|
||||
|
||||
fun clear()
|
||||
fun clear()
|
||||
|
||||
fun reset() {
|
||||
if (hasArray) {
|
||||
array.copyInto(array, arrayOffset, readOffset, arrayOffset + writePosition)
|
||||
writePosition = readableSize
|
||||
readPosition = 0
|
||||
} else {
|
||||
readMode()
|
||||
nioBuffer.compact()
|
||||
val writePosition = readPosition
|
||||
resumeWriteMode()
|
||||
readPosition = 0
|
||||
this.writePosition = writePosition
|
||||
}
|
||||
}
|
||||
fun reset() {
|
||||
if (hasArray) {
|
||||
array.copyInto(array, arrayOffset, readOffset, arrayOffset + writePosition)
|
||||
writePosition = readableSize
|
||||
readPosition = 0
|
||||
} else {
|
||||
readMode()
|
||||
nioBuffer.compact()
|
||||
val writePosition = readPosition
|
||||
resumeWriteMode()
|
||||
readPosition = 0
|
||||
this.writePosition = writePosition
|
||||
}
|
||||
}
|
||||
|
||||
fun reset(outputStream: OutputStream) {
|
||||
if (hasArray) {
|
||||
outputStream.write(array, readOffset, arrayOffset + writePosition)
|
||||
} else {
|
||||
outputStream.write(getBytes())
|
||||
}
|
||||
writePosition = 0
|
||||
readPosition = 0
|
||||
}
|
||||
fun reset(outputStream: OutputStream) {
|
||||
if (hasArray) {
|
||||
outputStream.write(array, readOffset, arrayOffset + writePosition)
|
||||
} else {
|
||||
outputStream.write(getBytes())
|
||||
}
|
||||
writePosition = 0
|
||||
readPosition = 0
|
||||
}
|
||||
|
||||
fun requireAvailableSize(size: Int) {
|
||||
if (limit - readPosition < size) reset()
|
||||
}
|
||||
fun requireAvailableSize(size: Int) {
|
||||
if (limit - readPosition < size) reset()
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 数据获取方法
|
||||
*/
|
||||
/*
|
||||
* 数据获取方法
|
||||
*/
|
||||
|
||||
|
||||
fun get(): Byte = if (readMode) {
|
||||
nioBuffer.get()
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.get()
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
fun get(): Byte = if (readMode) {
|
||||
nioBuffer.get()
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.get()
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
|
||||
fun getChar(): Char = if (readMode) {
|
||||
nioBuffer.char
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.char
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
fun getChar(): Char = if (readMode) {
|
||||
nioBuffer.char
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.char
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
|
||||
fun getShort(): Short = if (readMode) {
|
||||
nioBuffer.short
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.short
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
fun getShort(): Short = if (readMode) {
|
||||
nioBuffer.short
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.short
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
|
||||
fun getInt(): Int = if (readMode) {
|
||||
nioBuffer.int
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.int
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
fun getInt(): Int = if (readMode) {
|
||||
nioBuffer.int
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.int
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
|
||||
fun getLong(): Long = if (readMode) {
|
||||
nioBuffer.long
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.long
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
fun getLong(): Long = if (readMode) {
|
||||
nioBuffer.long
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.long
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
|
||||
fun getFloat(): Float = if (readMode) {
|
||||
nioBuffer.float
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.float
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
fun getFloat(): Float = if (readMode) {
|
||||
nioBuffer.float
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.float
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
|
||||
fun getDouble(): Double = if (readMode) {
|
||||
nioBuffer.double
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.double
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
fun getDouble(): Double = if (readMode) {
|
||||
nioBuffer.double
|
||||
} else {
|
||||
readMode()
|
||||
val value = nioBuffer.double
|
||||
resumeWriteMode()
|
||||
value
|
||||
}
|
||||
|
||||
|
||||
fun getBytes(): ByteArray = if (readMode) {
|
||||
val bytes = ByteArray(readableSize)
|
||||
nioBuffer.get(bytes)
|
||||
readPosition = writePosition
|
||||
bytes
|
||||
} else {
|
||||
readMode()
|
||||
val bytes = ByteArray(readableSize)
|
||||
nioBuffer.get(bytes)
|
||||
readPosition = writePosition
|
||||
resumeWriteMode()
|
||||
bytes
|
||||
}
|
||||
fun getBytes(): ByteArray = if (readMode) {
|
||||
val bytes = ByteArray(readableSize)
|
||||
nioBuffer.get(bytes)
|
||||
readPosition = writePosition
|
||||
bytes
|
||||
} else {
|
||||
readMode()
|
||||
val bytes = ByteArray(readableSize)
|
||||
nioBuffer.get(bytes)
|
||||
readPosition = writePosition
|
||||
resumeWriteMode()
|
||||
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 {
|
||||
val readSize = min(readableSize, size)
|
||||
if (hasArray) {
|
||||
array.copyInto(buffer, bufferOffset, readOffset, readOffset + readSize)
|
||||
readPosition += readOffset
|
||||
reset()
|
||||
} else {
|
||||
readBuffer {
|
||||
it.put(buffer, bufferOffset, readSize)
|
||||
}
|
||||
}
|
||||
return readSize
|
||||
}
|
||||
fun writeTo(buffer: ByteArray, bufferOffset: Int = 0, size: Int = min(readableSize, buffer.size)): Int {
|
||||
val readSize = min(readableSize, size)
|
||||
if (hasArray) {
|
||||
array.copyInto(buffer, bufferOffset, readOffset, readOffset + readSize)
|
||||
readPosition += readOffset
|
||||
reset()
|
||||
} else {
|
||||
readBuffer {
|
||||
it.put(buffer, bufferOffset, readSize)
|
||||
}
|
||||
}
|
||||
return readSize
|
||||
}
|
||||
|
||||
fun writeTo(os: OutputStream): Int {
|
||||
val size = readableSize
|
||||
if (hasArray) {
|
||||
os.write(array, arrayOffset + readPosition, size)
|
||||
readPosition += size
|
||||
reset()
|
||||
} else {
|
||||
val buffer = ByteArray(1024)
|
||||
readBuffer {
|
||||
while (it.remaining() > 0) {
|
||||
it.put(buffer)
|
||||
os.write(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
return size
|
||||
}
|
||||
fun writeTo(os: OutputStream): Int {
|
||||
val size = readableSize
|
||||
if (hasArray) {
|
||||
os.write(array, arrayOffset + readPosition, size)
|
||||
readPosition += size
|
||||
reset()
|
||||
} else {
|
||||
val buffer = ByteArray(1024)
|
||||
readBuffer {
|
||||
while (it.remaining() > 0) {
|
||||
it.put(buffer)
|
||||
os.write(buffer)
|
||||
}
|
||||
}
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
fun writeTo(buffer: AdvanceByteBuffer): Int {
|
||||
val size = min(readableSize, buffer.writeableSize)
|
||||
if (hasArray && buffer.hasArray) {
|
||||
array.copyInto(buffer.array, buffer.writeOffset, readOffset, readOffset + size)
|
||||
buffer.writePosition += size
|
||||
readPosition += size
|
||||
reset()
|
||||
} else {
|
||||
readBuffer {
|
||||
buffer.nioBuffer.put(it)
|
||||
}
|
||||
}
|
||||
return size
|
||||
}
|
||||
fun writeTo(buffer: AdvanceByteBuffer): Int {
|
||||
val size = min(readableSize, buffer.writeableSize)
|
||||
if (hasArray && buffer.hasArray) {
|
||||
array.copyInto(buffer.array, buffer.writeOffset, readOffset, readOffset + size)
|
||||
buffer.writePosition += size
|
||||
readPosition += size
|
||||
reset()
|
||||
} else {
|
||||
readBuffer {
|
||||
buffer.nioBuffer.put(it)
|
||||
}
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
fun toByteArray() = getBytes()
|
||||
fun toByteArray() = getBytes()
|
||||
|
||||
|
||||
/*
|
||||
* 数据写入方法
|
||||
*/
|
||||
/*
|
||||
* 数据写入方法
|
||||
*/
|
||||
|
||||
fun put(byte: Byte) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.put(byte)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.put(byte)
|
||||
}
|
||||
}
|
||||
fun put(byte: Byte) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.put(byte)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.put(byte)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(char: Char) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putChar(char)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putChar(char)
|
||||
}
|
||||
}
|
||||
fun put(char: Char) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putChar(char)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putChar(char)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(short: Short) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putShort(short)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putShort(short)
|
||||
}
|
||||
}
|
||||
fun put(short: Short) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putShort(short)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putShort(short)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(int: Int) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putInt(int)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putInt(int)
|
||||
}
|
||||
}
|
||||
fun put(int: Int) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putInt(int)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putInt(int)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(long: Long) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putLong(long)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putLong(long)
|
||||
}
|
||||
}
|
||||
fun put(long: Long) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putLong(long)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putLong(long)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(float: Float) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putFloat(float)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putFloat(float)
|
||||
}
|
||||
}
|
||||
fun put(float: Float) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putFloat(float)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putFloat(float)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(double: Double) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putDouble(double)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putDouble(double)
|
||||
}
|
||||
}
|
||||
fun put(double: Double) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.putDouble(double)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.putDouble(double)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(str: String) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.put(str.toByteArray())
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.put(str.toByteArray())
|
||||
}
|
||||
}
|
||||
fun put(str: String) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.put(str.toByteArray())
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.put(str.toByteArray())
|
||||
}
|
||||
}
|
||||
|
||||
fun put(byteArray: ByteArray, startIndex: Int = 0, endIndex: Int = byteArray.size) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
||||
}
|
||||
}
|
||||
fun put(byteArray: ByteArray, startIndex: Int = 0, endIndex: Int = byteArray.size) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
||||
readMode()
|
||||
} else {
|
||||
nioBuffer.put(byteArray, startIndex, endIndex - startIndex)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(array: CharArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
fun put(array: CharArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(array: ShortArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
fun put(array: ShortArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(array: IntArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
fun put(array: IntArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(array: LongArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
fun put(array: LongArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(array: FloatArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
fun put(array: FloatArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
|
||||
fun put(array: DoubleArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
fun put(array: DoubleArray, index: Int = 0, size: Int = array.size - index) {
|
||||
if (readMode) {
|
||||
resumeWriteMode()
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
readMode()
|
||||
} else {
|
||||
array.forEachIndex(index, index + size - 1, this::put)
|
||||
}
|
||||
}
|
||||
|
||||
fun peekString(size: Int = readableSize): String {
|
||||
val readP = readPosition
|
||||
val str = getString(size)
|
||||
readPosition = readP
|
||||
return str
|
||||
}
|
||||
fun peekString(size: Int = readableSize): String {
|
||||
val readP = readPosition
|
||||
val str = getString(size)
|
||||
readPosition = readP
|
||||
return str
|
||||
}
|
||||
|
||||
fun <T> readBuffer(action: (nioBuffer: ByteBuffer) -> T): T = readNioBuffer(action)
|
||||
fun <T> writeBuffer(action: (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer(action)
|
||||
fun <T> readBuffer(action: (nioBuffer: ByteBuffer) -> T): T = readNioBuffer(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> writeSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer { 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) }
|
||||
|
||||
fun split(from: Int = readPosition, to: Int = writePosition): AdvanceByteBuffer {
|
||||
return if (hasArray) {
|
||||
ByteArrayAdvanceByteBuffer(array, arrayOffset + readPosition, to - from)
|
||||
} else {
|
||||
throw NotImplementedException()
|
||||
}
|
||||
}
|
||||
fun split(from: Int = readPosition, to: Int = writePosition): AdvanceByteBuffer {
|
||||
return if (hasArray) {
|
||||
ByteArrayAdvanceByteBuffer(array, arrayOffset + readPosition, to - from)
|
||||
} else {
|
||||
throw NotImplementedException()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> AdvanceByteBuffer.readNioBuffer(action: (nioBuffer: ByteBuffer) -> T): T {
|
||||
readMode()
|
||||
val buffer = nioBuffer
|
||||
val position = nioBuffer.position()
|
||||
return try {
|
||||
action(buffer)
|
||||
} finally {
|
||||
resumeWriteMode(buffer.position() - position)
|
||||
}
|
||||
readMode()
|
||||
val buffer = nioBuffer
|
||||
val position = nioBuffer.position()
|
||||
return try {
|
||||
action(buffer)
|
||||
} finally {
|
||||
resumeWriteMode(buffer.position() - position)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <T> AdvanceByteBuffer.writeNioBuffer(action: (nioBuffer: ByteBuffer) -> T): T {
|
||||
val buffer = nioBuffer
|
||||
val position = writePosition
|
||||
val bufferPosition = nioBuffer.position()
|
||||
return try {
|
||||
action(buffer)
|
||||
} finally {
|
||||
writePosition = position + (buffer.position() - bufferPosition)
|
||||
}
|
||||
val buffer = nioBuffer
|
||||
val position = writePosition
|
||||
val bufferPosition = nioBuffer.position()
|
||||
return try {
|
||||
action(buffer)
|
||||
} finally {
|
||||
writePosition = position + (buffer.position() - bufferPosition)
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user