Use .use for streams
cleaner code
This commit is contained in:
+4
-4
@@ -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")
|
||||
}
|
||||
|
||||
+7
-1
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user