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

View File

@@ -1842,15 +1842,15 @@ object LocalCache : ILocalCache {
note.addRelay(relay)
}
var isVerified =
val isVerified =
try {
val cachePath = Amethyst.instance.nip95cache
cachePath.mkdirs()
val file = File(cachePath, event.id)
if (!file.exists() && (wasVerified || justVerify(event))) {
val stream = FileOutputStream(file)
stream.write(event.decode())
stream.close()
FileOutputStream(file).use { stream ->
stream.write(event.decode())
}
Log.i(
"FileStorageEvent",
"NIP95 File received from $relay and saved to disk as $file",
@@ -2134,7 +2134,7 @@ object LocalCache : ILocalCache {
}
}
suspend fun findStatusesForUser(user: User): ImmutableList<AddressableNote> {
fun findStatusesForUser(user: User): ImmutableList<AddressableNote> {
checkNotInMainThread()
return addressables
@@ -2151,7 +2151,7 @@ object LocalCache : ILocalCache {
.toImmutableList()
}
suspend fun findEarliestOtsForNote(
fun findEarliestOtsForNote(
note: Note,
resolverBuilder: OtsResolverBuilder,
): Long? {
@@ -2178,7 +2178,7 @@ object LocalCache : ILocalCache {
fun cachedModificationEventsForNote(note: Note): List<Note>? = modificationCache[note.idHex]
suspend fun findLatestModificationForNote(note: Note): List<Note> {
fun findLatestModificationForNote(note: Note): List<Note> {
checkNotInMainThread()
val noteAuthor = note.author ?: return emptyList()
@@ -2340,9 +2340,9 @@ object LocalCache : ILocalCache {
val children =
if (noteEvent is WrappedEvent) {
noteEvent.host?.id?.let {
getNoteIfExists(it)?.let {
removeFromCache(it)
it.removeAllChildNotes()
getNoteIfExists(it)?.let { it2 ->
removeFromCache(it2)
it2.removeAllChildNotes()
}
}
} else {

View File

@@ -96,19 +96,21 @@ class BlossomUploader {
checkNotNull(imageInputStream) { "Can't open the image input stream" }
return upload(
imageInputStream,
hash,
payload.size,
fileName,
myContentType,
alt,
sensitiveContent,
serverBaseUrl,
okHttpClient,
httpAuth,
context,
)
return imageInputStream.use { stream ->
upload(
stream,
hash,
payload.size,
fileName,
myContentType,
alt,
sensitiveContent,
serverBaseUrl,
okHttpClient,
httpAuth,
context,
)
}
}
fun encodeAuth(event: BlossomAuthorizationEvent): String {

View File

@@ -109,18 +109,20 @@ class Nip96Uploader {
checkNotNull(imageInputStream) { "Can't open the image input stream" }
return upload(
imageInputStream,
length,
myContentType,
alt,
sensitiveContent,
server,
okHttpClient,
onProgress,
httpAuth,
context,
)
return imageInputStream.use { stream ->
upload(
stream,
length,
myContentType,
alt,
sensitiveContent,
server,
okHttpClient,
onProgress,
httpAuth,
context,
)
}
}
suspend fun upload(

View File

@@ -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")
}

View File

@@ -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
}
}