Skip to content

Instantly share code, notes, and snippets.

@ku1ik
Last active March 1, 2017 16:01
Show Gist options
  • Save ku1ik/e712cfae1383d4dee6aa12576a66dd01 to your computer and use it in GitHub Desktop.
Save ku1ik/e712cfae1383d4dee6aa12576a66dd01 to your computer and use it in GitHub Desktop.
InputStream wrapper which calls given callback after the stream is closed.
(ns example
(:import java.io.FilterInputStream))
(defn cleanup-input-stream
"InputStream wrapper which calls given callback after
the stream is closed."
[is cleanup]
(proxy [FilterInputStream] [is]
(close []
(proxy-super close)
(cleanup))))
(defn input-stream-in-tmp-dir [...]
(let [dir (create-tmp-dir)
file (generate-file dir)
cleanup #(delete-tmp-dir dir)]
(cleanup-input-stream (input-stream file) cleanup))
(with-open [is (input-stream-in-tmp-dir ...)]
(io/copy is ....))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment