Merge pull request #1467 from davotoula/prevent-resource-leaks-with-streams
Prevent resource leaks with streams
This commit is contained in:
+9
-9
@@ -50,8 +50,8 @@ class Request(
|
||||
|
||||
try {
|
||||
val httpURLConnection = url.openConnection() as HttpURLConnection
|
||||
httpURLConnection.setReadTimeout(10000)
|
||||
httpURLConnection.setConnectTimeout(10000)
|
||||
httpURLConnection.readTimeout = 10000
|
||||
httpURLConnection.connectTimeout = 10000
|
||||
httpURLConnection.setRequestProperty("User-Agent", "OpenTimestamps Java")
|
||||
httpURLConnection.setRequestProperty("Accept", "application/json")
|
||||
httpURLConnection.setRequestProperty("Accept-Encoding", "gzip")
|
||||
@@ -62,17 +62,17 @@ class Request(
|
||||
|
||||
if (data != null) {
|
||||
httpURLConnection.setDoOutput(true)
|
||||
httpURLConnection.setRequestMethod("POST")
|
||||
httpURLConnection.requestMethod = "POST"
|
||||
httpURLConnection.setRequestProperty(
|
||||
"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")
|
||||
httpURLConnection.requestMethod = "GET"
|
||||
}
|
||||
|
||||
httpURLConnection.connect()
|
||||
@@ -84,7 +84,7 @@ class Request(
|
||||
response.status = responseCode
|
||||
response.fromUrl = url.toString()
|
||||
var `is` = httpURLConnection.getInputStream()
|
||||
if ("gzip" == httpURLConnection.getContentEncoding()) {
|
||||
if ("gzip" == httpURLConnection.contentEncoding) {
|
||||
`is` = GZIPInputStream(`is`)
|
||||
}
|
||||
response.setStream(`is`)
|
||||
|
||||
+8
-2
@@ -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
|
||||
@@ -53,7 +54,7 @@ class Response {
|
||||
|
||||
@get:Throws(IOException::class)
|
||||
val string: String
|
||||
get() = kotlin.text.String(this.bytes, StandardCharsets.UTF_8)
|
||||
get() = String(this.bytes, StandardCharsets.UTF_8)
|
||||
|
||||
@get:Throws(IOException::class)
|
||||
val bytes: ByteArray
|
||||
@@ -79,4 +80,9 @@ class Response {
|
||||
JsonMapper.builder().build()
|
||||
return builder.readTree(jsonString)
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
stream?.close()
|
||||
stream = null
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -62,7 +62,7 @@ abstract class OpCrypto internal constructor() : OpUnary() {
|
||||
val digest = MessageDigest.getInstance(this.hashLibName())
|
||||
var chunk = ctx.read(1048576)
|
||||
|
||||
while (chunk != null && chunk.size > 0) {
|
||||
while (chunk.isNotEmpty()) {
|
||||
digest.update(chunk)
|
||||
chunk = ctx.read(1048576)
|
||||
}
|
||||
@@ -73,7 +73,7 @@ abstract class OpCrypto internal constructor() : OpUnary() {
|
||||
}
|
||||
|
||||
@Throws(IOException::class, NoSuchAlgorithmException::class)
|
||||
fun hashFd(file: File?): ByteArray = hashFd(FileInputStream(file))
|
||||
fun hashFd(file: File?): ByteArray = FileInputStream(file).use { inputStream -> hashFd(inputStream) }
|
||||
|
||||
@Throws(IOException::class, NoSuchAlgorithmException::class)
|
||||
fun hashFd(bytes: ByteArray): ByteArray {
|
||||
@@ -93,6 +93,7 @@ abstract class OpCrypto internal constructor() : OpUnary() {
|
||||
count = inputStream.read(chunk, 0, 1048576)
|
||||
}
|
||||
|
||||
// TODO: Is this needed? Closing of stream should be callers responsibility?
|
||||
inputStream.close()
|
||||
val hash = digest.digest()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user