Use .use for streams

cleaner code
This commit is contained in:
davotoula
2025-09-06 16:44:29 +02:00
parent e66e71a05c
commit ec362ea0ee
5 changed files with 50 additions and 40 deletions
@@ -67,10 +67,10 @@ class Request(
"Content-Length",
"" + this.data!!.size.toString(),
)
val wr = DataOutputStream(httpURLConnection.getOutputStream())
wr.write(this.data, 0, this.data!!.size)
wr.flush()
wr.close()
DataOutputStream(httpURLConnection.getOutputStream()).use { wr ->
wr.write(this.data, 0, this.data!!.size)
wr.flush()
}
} else {
httpURLConnection.setRequestMethod("GET")
}
@@ -23,6 +23,7 @@ package com.vitorpamplona.quartz.nip03Timestamp.ots.http
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.json.JsonMapper
import java.io.ByteArrayOutputStream
import java.io.Closeable
import java.io.IOException
import java.io.InputStream
import java.nio.charset.StandardCharsets
@@ -30,7 +31,7 @@ import java.nio.charset.StandardCharsets
/**
* Holds the response from an HTTP request.
*/
class Response {
class Response : Closeable {
private var stream: InputStream? = null
var fromUrl: String? = null
@@ -79,4 +80,9 @@ class Response {
JsonMapper.builder().build()
return builder.readTree(jsonString)
}
override fun close() {
stream?.close()
stream = null
}
}