implement 'getTextureFile' and 'getTexturePreview' Netop

This commit is contained in:
CodeSteak 2019-06-03 15:50:09 +02:00
parent d7e2977f2a
commit 972d199154
1 changed files with 50 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package org.hso.texturesyncclient.controller.net
import com.google.gson.*
import javafx.scene.image.Image
import org.hso.texturesyncclient.model.Sha256
import org.hso.texturesyncclient.model.Texture
import java.io.*
import java.lang.RuntimeException
@ -164,6 +166,54 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable {
}
@Throws(IOException::class, ConnectionException::class)
@Synchronized
fun getTextureFile(hash : Sha256) : ByteArray {
val io = getStreams()
val obj = JsonObject()
obj.add("get_texture_file", {
val inner = JsonObject()
inner.addProperty("texture_hash", hash.toString())
inner
}())
JsonPackage(obj).write(io.second)
when (val pkg = Package.read(io.first)) {
is JsonPackage -> throw ConnectionUnexpecedPacketException()
is BinaryPackage -> return pkg.content
is ErrorPackage -> throw ConnectionErrorException(pkg)
else -> throw RuntimeException("Unreachable")
}
}
@Throws(IOException::class, ConnectionException::class)
@Synchronized
fun getTexturePreview(hash : Sha256) : Image {
val io = getStreams()
val obj = JsonObject()
obj.add("get_texture_file", {
val inner = JsonObject()
inner.addProperty("texture_hash", hash.toString())
inner.addProperty("desired_format", "jpeg")
inner
}())
JsonPackage(obj).write(io.second)
when (val pkg = Package.read(io.first)) {
is JsonPackage -> throw ConnectionUnexpecedPacketException()
is BinaryPackage -> {
return Image(ByteArrayInputStream(pkg.content))
}
is ErrorPackage -> throw ConnectionErrorException(pkg)
else -> throw RuntimeException("Unreachable")
}
}
@Throws(IOException::class)
@Synchronized
override fun close() {