From acfe8fda0af784225f1869bec8601a9db555ecbd Mon Sep 17 00:00:00 2001 From: tursom Date: Thu, 10 Oct 2019 15:23:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=A4=E4=B8=AADe?= =?UTF-8?q?mo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/src/test/kotlin/Demo.kt | 42 + socket/socket-async/src/test/kotlin/Demo.kt | 30 + .../socket/niothread/ThreadPoolNioThread.kt | 2 +- .../socket/niothread/ThreadPoolWorkerGroup.kt | 2 +- .../socket/niothread/WorkerLoopNioThread.kt | 2 +- .../cn/tursom/socket/server/NioServer.kt | 2 +- .../core/bytebuffer/AdvanceByteBuffer.kt | 726 +++++++++--------- 7 files changed, 442 insertions(+), 364 deletions(-) create mode 100644 database/src/test/kotlin/Demo.kt create mode 100644 socket/socket-async/src/test/kotlin/Demo.kt diff --git a/database/src/test/kotlin/Demo.kt b/database/src/test/kotlin/Demo.kt new file mode 100644 index 0000000..ebfb763 --- /dev/null +++ b/database/src/test/kotlin/Demo.kt @@ -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" }) +} \ No newline at end of file diff --git a/socket/socket-async/src/test/kotlin/Demo.kt b/socket/socket-async/src/test/kotlin/Demo.kt new file mode 100644 index 0000000..3926d1a --- /dev/null +++ b/socket/socket-async/src/test/kotlin/Demo.kt @@ -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() +} \ No newline at end of file diff --git a/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolNioThread.kt b/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolNioThread.kt index 27b40c4..313d02c 100644 --- a/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolNioThread.kt +++ b/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolNioThread.kt @@ -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) diff --git a/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolWorkerGroup.kt b/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolWorkerGroup.kt index c9aff9a..dec4dae 100644 --- a/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolWorkerGroup.kt +++ b/socket/src/main/kotlin/cn/tursom/socket/niothread/ThreadPoolWorkerGroup.kt @@ -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) { diff --git a/socket/src/main/kotlin/cn/tursom/socket/niothread/WorkerLoopNioThread.kt b/socket/src/main/kotlin/cn/tursom/socket/niothread/WorkerLoopNioThread.kt index 2b3b410..4eaa789 100644 --- a/socket/src/main/kotlin/cn/tursom/socket/niothread/WorkerLoopNioThread.kt +++ b/socket/src/main/kotlin/cn/tursom/socket/niothread/WorkerLoopNioThread.kt @@ -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) diff --git a/socket/src/main/kotlin/cn/tursom/socket/server/NioServer.kt b/socket/src/main/kotlin/cn/tursom/socket/server/NioServer.kt index 0032a6e..f087bec 100644 --- a/socket/src/main/kotlin/cn/tursom/socket/server/NioServer.kt +++ b/socket/src/main/kotlin/cn/tursom/socket/server/NioServer.kt @@ -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() { diff --git a/src/main/kotlin/cn/tursom/core/bytebuffer/AdvanceByteBuffer.kt b/src/main/kotlin/cn/tursom/core/bytebuffer/AdvanceByteBuffer.kt index a2d4a2d..57b3eca 100644 --- a/src/main/kotlin/cn/tursom/core/bytebuffer/AdvanceByteBuffer.kt +++ b/src/main/kotlin/cn/tursom/core/bytebuffer/AdvanceByteBuffer.kt @@ -7,423 +7,429 @@ import java.nio.ByteBuffer import kotlin.math.min interface AdvanceByteBuffer { - val nioBuffer: ByteBuffer - val nioBuffers: Array get() = arrayOf(nioBuffer) + val nioBuffer: ByteBuffer + val nioBuffers: Array 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 readBuffer(action: (nioBuffer: ByteBuffer) -> T): T = readNioBuffer(action) - fun writeBuffer(action: (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer(action) + fun readBuffer(action: (nioBuffer: ByteBuffer) -> T): T = readNioBuffer(action) + fun writeBuffer(action: (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer(action) - suspend fun readSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = readNioBuffer { action(it) } - suspend fun writeSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = writeNioBuffer { action(it) } + suspend fun readSuspendBuffer(action: suspend (nioBuffer: ByteBuffer) -> T): T = readNioBuffer { action(it) } + suspend fun 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 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 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) + } }