Initial commit
This commit is contained in:
25 files changed
+1411
No files matched your search
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.mcping.platform
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
|
||||
internal actual class PlatformBuffer actual constructor() {
|
||||
private val stream = ByteArrayOutputStream()
|
||||
private var readOffset = 0
|
||||
|
||||
actual fun writeByte(value: Byte) = stream.write(value.toInt())
|
||||
actual fun writeBytes(bytes: ByteArray) = stream.write(bytes)
|
||||
actual fun readByte(): Byte {
|
||||
val array = stream.toByteArray()
|
||||
if (readOffset >= array.size) throw IndexOutOfBoundsException("Buffer underflow")
|
||||
return array[readOffset++]
|
||||
}
|
||||
|
||||
actual fun readBytes(length: Int): ByteArray {
|
||||
val array = stream.toByteArray()
|
||||
if (readOffset + length > array.size) throw IndexOutOfBoundsException("Buffer underflow")
|
||||
val result = array.copyOfRange(readOffset, readOffset + length)
|
||||
readOffset += length
|
||||
return result
|
||||
}
|
||||
|
||||
actual fun toByteArray(): ByteArray = stream.toByteArray()
|
||||
|
||||
actual val size: Int
|
||||
get() = stream.size()
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
|
||||
package cn.rtast.mcping.platform
|
||||
|
||||
import java.io.InputStream
|
||||
import java.io.OutputStream
|
||||
|
||||
internal actual class PlatformReadChannel {
|
||||
private val _inputStream: InputStream
|
||||
|
||||
constructor(inputStream: InputStream) {
|
||||
_inputStream = inputStream
|
||||
}
|
||||
|
||||
actual fun readByte(): Byte = _inputStream.read().toByte()
|
||||
actual fun readBytes(length: Int): ByteArray = _inputStream.readNBytes(length)
|
||||
actual fun readFully(out: ByteArray, start: Int, end: Int) {
|
||||
var bytesRead = 0
|
||||
val length = end - start
|
||||
while (bytesRead < length) {
|
||||
val read = _inputStream.read(out, start + bytesRead, length - bytesRead)
|
||||
if (read == -1) throw IllegalStateException("End of stream")
|
||||
bytesRead += read
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal actual class PlatformWriteChannel {
|
||||
private val _outputStream: OutputStream
|
||||
|
||||
constructor(outputStream: OutputStream) {
|
||||
_outputStream = outputStream
|
||||
}
|
||||
|
||||
actual fun writeFully(value: ByteArray, startIndex: Int, endIndex: Int) =
|
||||
_outputStream.write(value, startIndex, endIndex - startIndex)
|
||||
|
||||
actual fun flush() = _outputStream.flush()
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright © 2026 RTAkland
|
||||
* Author: RTAkland
|
||||
* Date: 2026/9/3
|
||||
*/
|
||||
|
||||
package cn.rtast.mcping.platform
|
||||
|
||||
import java.net.Socket
|
||||
|
||||
internal actual class PlatformSocket internal actual constructor(host: String, port: Int) {
|
||||
private val socket = Socket(host, port)
|
||||
|
||||
actual fun openReadChannel(): PlatformReadChannel = PlatformReadChannel(socket.getInputStream())
|
||||
actual fun openWriteChannel(): PlatformWriteChannel = PlatformWriteChannel(socket.getOutputStream())
|
||||
actual fun close() = socket.close()
|
||||
}
|
||||
Reference in New Issue
Block a user