Remove unused read/write type
This commit is contained in:
parent
2489266a40
commit
b1eb99198a
@ -365,73 +365,3 @@ fun OutputStream.writeBinaryString(input: String) {
|
|||||||
writeVarIntLE(bytes.size)
|
writeVarIntLE(bytes.size)
|
||||||
write(bytes)
|
write(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
private data class IndexedStreamCodec<T>(
|
|
||||||
val condition: Predicate<Any?>,
|
|
||||||
val id: Int,
|
|
||||||
val codec: IStreamCodec<T>
|
|
||||||
) {
|
|
||||||
fun read(stream: DataInputStream, sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 KiB */)): T {
|
|
||||||
return codec.read(stream, sizeLimit)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun write(stream: DataOutputStream, value: Any?) {
|
|
||||||
codec.write(stream, value as T)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private inline fun <reified T> IndexedStreamCodec(id: Int, codec: StreamCodec<T>): IndexedStreamCodec<T> {
|
|
||||||
return IndexedStreamCodec({ it is T }, id, codec)
|
|
||||||
}
|
|
||||||
|
|
||||||
private val codecs: List<IndexedStreamCodec<*>> = com.google.common.collect.ImmutableList.builder<IndexedStreamCodec<*>>().let {
|
|
||||||
var codecID = 0
|
|
||||||
|
|
||||||
it.add(IndexedStreamCodec({ it == null }, codecID++, NullValueCodec))
|
|
||||||
|
|
||||||
it.add(IndexedStreamCodec(codecID++, BooleanValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, ByteValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, ShortValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, VarIntValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, VarLongValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, FloatValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, DoubleValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, ItemStackValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, DecimalValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, BigDecimalValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, UUIDValueCodec))
|
|
||||||
it.add(IndexedStreamCodec(codecID++, BinaryStringCodec))
|
|
||||||
|
|
||||||
it.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
class NotSerializableValueException(message: String) : Exception(message)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Write arbitrary data to this stream, in exploit-free way
|
|
||||||
*/
|
|
||||||
fun DataOutputStream.writeType(value: Any?) {
|
|
||||||
for (codec in codecs) {
|
|
||||||
if (codec.condition.test(value)) {
|
|
||||||
write(codec.id)
|
|
||||||
codec.write(this, value)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
throw NotSerializableValueException("Value $value <${value?.let { it::class.qualifiedName }}> can not be serialized")
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Read arbitrary data from this stream, in exploit-free way
|
|
||||||
*/
|
|
||||||
fun DataInputStream.readType(sizeLimit: NbtAccounter = NbtAccounter(1L shl 18 /* 256 KiB */)): Any? {
|
|
||||||
sizeLimit.accountBytes(1L)
|
|
||||||
val id = read()
|
|
||||||
|
|
||||||
if (id >= codecs.size) {
|
|
||||||
throw IndexOutOfBoundsException("No codec for network type $id")
|
|
||||||
}
|
|
||||||
|
|
||||||
return codecs[id].read(this, sizeLimit)
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user