Some struct IO

This commit is contained in:
DBotThePony 2024-02-04 20:15:13 +07:00
parent 9a345447e1
commit a1f7eaeee8
Signed by: DBot
GPG Key ID: DCC23B5715498507
4 changed files with 191 additions and 9 deletions

View File

@ -4,7 +4,7 @@ kotlin.code.style=official
specifyKotlinAsDependency=false
projectGroup=ru.dbotthepony.kommons
projectVersion=1.7.6
projectVersion=1.7.7
guavaDepVersion=33.0.0
gsonDepVersion=2.8.9

View File

@ -0,0 +1,36 @@
package ru.dbotthepony.kommons.io
import ru.dbotthepony.kommons.vector.Vector2d
import ru.dbotthepony.kommons.vector.Vector2f
import ru.dbotthepony.kommons.vector.Vector2i
import ru.dbotthepony.kommons.vector.Vector3d
import ru.dbotthepony.kommons.vector.Vector3f
import ru.dbotthepony.kommons.vector.Vector3i
import ru.dbotthepony.kommons.vector.Vector4d
import ru.dbotthepony.kommons.vector.Vector4f
import ru.dbotthepony.kommons.vector.Vector4i
import java.io.InputStream
private inline fun <T, I> InputStream.read2(factory: (I, I) -> T, reader: (InputStream) -> I): T {
return factory(reader(this), reader(this))
}
private inline fun <T, I> InputStream.read3(factory: (I, I, I) -> T, reader: (InputStream) -> I): T {
return factory(reader(this), reader(this), reader(this))
}
private inline fun <T, I> InputStream.read4(factory: (I, I, I, I) -> T, reader: (InputStream) -> I): T {
return factory(reader(this), reader(this), reader(this), reader(this))
}
fun InputStream.readVector2i() = read2(::Vector2i) { it.readInt() }
fun InputStream.readVector2d() = read2(::Vector2d) { it.readDouble() }
fun InputStream.readVector2f() = read2(::Vector2f) { it.readFloat() }
fun InputStream.readVector3i() = read3(::Vector3i) { it.readInt() }
fun InputStream.readVector3d() = read3(::Vector3d) { it.readDouble() }
fun InputStream.readVector3f() = read3(::Vector3f) { it.readFloat() }
fun InputStream.readVector4i() = read4(::Vector4i) { it.readInt() }
fun InputStream.readVector4d() = read4(::Vector4d) { it.readDouble() }
fun InputStream.readVector4f() = read4(::Vector4f) { it.readFloat() }

View File

@ -2,6 +2,7 @@ package ru.dbotthepony.kommons.io
import it.unimi.dsi.fastutil.bytes.ByteArrayList
import java.io.DataInput
import java.io.EOFException
import java.io.IOException
import java.io.InputStream
import java.io.RandomAccessFile
@ -33,12 +34,21 @@ fun <S : InputStream, V, C : MutableCollection<V>> S.readCollection(reader: S.()
fun <S : InputStream, V> S.readCollection(reader: S.() -> V) = readCollection(reader, ::ArrayList)
private fun InputStream.readOrError(): Int {
val read = read()
if (read == -1)
throw EOFException()
return read
}
fun InputStream.readInt(): Int {
if (this is DataInput) {
return readInt()
}
return (read() shl 24) or (read() shl 16) or (read() shl 8) or read()
return (readOrError() shl 24) or (readOrError() shl 16) or (readOrError() shl 8) or readOrError()
}
fun InputStream.readLong(): Long {
@ -46,13 +56,13 @@ fun InputStream.readLong(): Long {
return readLong()
}
return (read().toLong() shl 48) or
(read().toLong() shl 40) or
(read().toLong() shl 32) or
(read().toLong() shl 24) or
(read().toLong() shl 16) or
(read().toLong() shl 8) or
read().toLong()
return (readOrError().toLong() shl 48) or
(readOrError().toLong() shl 40) or
(readOrError().toLong() shl 32) or
(readOrError().toLong() shl 24) or
(readOrError().toLong() shl 16) or
(readOrError().toLong() shl 8) or
readOrError().toLong()
}
fun InputStream.readFloat() = Float.fromBits(readInt())

View File

@ -1,5 +1,23 @@
package ru.dbotthepony.kommons.io
import ru.dbotthepony.kommons.core.IStruct2b
import ru.dbotthepony.kommons.core.IStruct2d
import ru.dbotthepony.kommons.core.IStruct2f
import ru.dbotthepony.kommons.core.IStruct2i
import ru.dbotthepony.kommons.core.IStruct2l
import ru.dbotthepony.kommons.core.IStruct2s
import ru.dbotthepony.kommons.core.IStruct3b
import ru.dbotthepony.kommons.core.IStruct3d
import ru.dbotthepony.kommons.core.IStruct3f
import ru.dbotthepony.kommons.core.IStruct3i
import ru.dbotthepony.kommons.core.IStruct3l
import ru.dbotthepony.kommons.core.IStruct3s
import ru.dbotthepony.kommons.core.IStruct4b
import ru.dbotthepony.kommons.core.IStruct4d
import ru.dbotthepony.kommons.core.IStruct4f
import ru.dbotthepony.kommons.core.IStruct4i
import ru.dbotthepony.kommons.core.IStruct4l
import ru.dbotthepony.kommons.core.IStruct4s
import java.io.DataOutput
import java.io.OutputStream
import java.io.RandomAccessFile
@ -34,6 +52,16 @@ fun OutputStream.writeInt(value: Int) {
write(value)
}
fun OutputStream.writeShort(value: Int) {
if (this is DataOutput) {
writeShort(value)
return
}
write(value ushr 8)
write(value)
}
fun OutputStream.writeLong(value: Long) {
if (this is DataOutput) {
writeLong(value)
@ -114,3 +142,111 @@ fun OutputStream.writeUUID(value: UUID) {
writeLong(value.mostSignificantBits)
writeLong(value.leastSignificantBits)
}
fun OutputStream.writeStruct2i(value: IStruct2i) {
writeInt(value.component1())
writeInt(value.component2())
}
fun OutputStream.writeStruct2f(value: IStruct2f) {
writeFloat(value.component1())
writeFloat(value.component2())
}
fun OutputStream.writeStruct2d(value: IStruct2d) {
writeDouble(value.component1())
writeDouble(value.component2())
}
fun OutputStream.writeStruct2l(value: IStruct2l) {
writeLong(value.component1())
writeLong(value.component2())
}
fun OutputStream.writeStruct2s(value: IStruct2s) {
writeShort(value.component1().toInt() and 0xFFFF)
writeShort(value.component2().toInt() and 0xFFFF)
}
fun OutputStream.writeStruct2b(value: IStruct2b) {
writeShort(value.component1().toInt() and 0xFF)
writeShort(value.component2().toInt() and 0xFF)
}
fun OutputStream.writeStruct3i(value: IStruct3i) {
writeInt(value.component1())
writeInt(value.component2())
writeInt(value.component3())
}
fun OutputStream.writeStruct3f(value: IStruct3f) {
writeFloat(value.component1())
writeFloat(value.component2())
writeFloat(value.component3())
}
fun OutputStream.writeStruct3d(value: IStruct3d) {
writeDouble(value.component1())
writeDouble(value.component2())
writeDouble(value.component3())
}
fun OutputStream.writeStruct3l(value: IStruct3l) {
writeLong(value.component1())
writeLong(value.component2())
writeLong(value.component3())
}
fun OutputStream.writeStruct3s(value: IStruct3s) {
writeShort(value.component1().toInt() and 0xFFFF)
writeShort(value.component2().toInt() and 0xFFFF)
writeShort(value.component3().toInt() and 0xFFFF)
}
fun OutputStream.writeStruct3b(value: IStruct3b) {
writeShort(value.component1().toInt() and 0xFF)
writeShort(value.component2().toInt() and 0xFF)
writeShort(value.component3().toInt() and 0xFF)
}
fun OutputStream.writeStruct4i(value: IStruct4i) {
writeInt(value.component1())
writeInt(value.component2())
writeInt(value.component3())
writeInt(value.component4())
}
fun OutputStream.writeStruct4f(value: IStruct4f) {
writeFloat(value.component1())
writeFloat(value.component2())
writeFloat(value.component3())
writeFloat(value.component4())
}
fun OutputStream.writeStruct4d(value: IStruct4d) {
writeDouble(value.component1())
writeDouble(value.component2())
writeDouble(value.component3())
writeDouble(value.component4())
}
fun OutputStream.writeStruct4l(value: IStruct4l) {
writeLong(value.component1())
writeLong(value.component2())
writeLong(value.component3())
writeLong(value.component4())
}
fun OutputStream.writeStruct4s(value: IStruct4s) {
writeShort(value.component1().toInt() and 0xFFFF)
writeShort(value.component2().toInt() and 0xFFFF)
writeShort(value.component3().toInt() and 0xFFFF)
writeShort(value.component4().toInt() and 0xFFFF)
}
fun OutputStream.writeStruct4b(value: IStruct4b) {
writeShort(value.component1().toInt() and 0xFF)
writeShort(value.component2().toInt() and 0xFF)
writeShort(value.component3().toInt() and 0xFF)
writeShort(value.component4().toInt() and 0xFF)
}