https://kotlinlang.org logo
#http4k
Title
# http4k
j

jh

02/13/2020, 9:49 AM
Hi! We are responding with an input stream from a temporary file on disk.
Copy code
Response(OK).body(tempFile.inputStream())
Is there any way to detect when the response has been fully read so that we can then delete the temp file on disk?
d

dave

02/13/2020, 8:20 PM
this is more of a generic JVM question than anything http4k specific, but you could probably find a better answer on SO. Maybe like this one.. 🙂 https://stackoverflow.com/questions/4693968/is-there-an-existing-fileinputstream-delete-on-close
(ie. there's nothing built in to http4k 🙂 )
t

taer

02/13/2020, 9:55 PM
If the file is on a unix, once you have a file handle open to it, you can remove the file. The OS will deallocate any inodes after the last file handle is closed. This is assuming that inputStream() has an open handle, and it's properly closed at the end. This unit test worked
Copy code
val f = File("/tmp/test")
    val inputStream = f.inputStream()
    f.delete()
    inputStream.reader().forEachLine {
      Thread.sleep(500)
      println(it)
    }
    inputStream.close()
I had a terminal open. /tmp/test vanished immediately, but the program kept going.
seq 1 100 > /tmp/test
sorta OS specific though
j

jh

02/14/2020, 10:37 AM
Alrighty, thanks!
14 Views