implement 'getTextureFile' and 'getTexturePreview' Netop
This commit is contained in:
		@ -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() {
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user