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

View File

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

View File

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

View File

@@ -67,10 +67,10 @@ class Request(
"Content-Length", "Content-Length",
"" + this.data!!.size.toString(), "" + this.data!!.size.toString(),
) )
val wr = DataOutputStream(httpURLConnection.getOutputStream()) DataOutputStream(httpURLConnection.getOutputStream()).use { wr ->
wr.write(this.data, 0, this.data!!.size) wr.write(this.data, 0, this.data!!.size)
wr.flush() wr.flush()
wr.close() }
} else { } else {
httpURLConnection.setRequestMethod("GET") 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.JsonNode
import com.fasterxml.jackson.databind.json.JsonMapper import com.fasterxml.jackson.databind.json.JsonMapper
import java.io.ByteArrayOutputStream import java.io.ByteArrayOutputStream
import java.io.Closeable
import java.io.IOException import java.io.IOException
import java.io.InputStream import java.io.InputStream
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
@@ -30,7 +31,7 @@ import java.nio.charset.StandardCharsets
/** /**
* Holds the response from an HTTP request. * Holds the response from an HTTP request.
*/ */
class Response { class Response : Closeable {
private var stream: InputStream? = null private var stream: InputStream? = null
var fromUrl: String? = null var fromUrl: String? = null
@@ -79,4 +80,9 @@ class Response {
JsonMapper.builder().build() JsonMapper.builder().build()
return builder.readTree(jsonString) return builder.readTree(jsonString)
} }
override fun close() {
stream?.close()
stream = null
}
} }