Hi! We are responding with an input stream from a ...
# http4k
j
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
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
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
Alrighty, thanks!