Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoiding the consumption of InputStream when detecting mime type? #20

Open
iku000888 opened this issue May 25, 2016 · 5 comments
Open

Comments

@iku000888
Copy link

Thank you for the wonderful library that works like a charm.

While using this I did notice that when detecting the mime type from an InputStream instance,
the bytes get consumed and I cannot simply reuse the same object.

There are work arounds available (mostly Java), and that is what InputStreams do,
but it would be nice if mime-type-of is free of side effects (at least from the app developer perspective).

Thanks!

P.S. Would greatly appreciate any recommended methods for re-using InputStream instances.

@michaelklishin
Copy link
Owner

@iku000888 that makes sense, please submit a PR that does that (using 100% Java interop if this is what you're most familiar with, that's fine).

@michaelklishin
Copy link
Owner

Given that nobody has messed with input stream mark, InputStream#reset should be sufficient to make it reusable for most intents and purposes. And I think it's perfectly fine for Pantomime to use it. WDYT?

@alexott
Copy link
Collaborator

alexott commented May 25, 2016

I've used it in one of the project - it mostly worked, maybe except very big files, like big Zips, or something like - can't remember right now...

@iku000888
Copy link
Author

iku000888 commented May 27, 2016

@michaelklishin Thanks for the feedback!

Here is a bit more of investigation results before I form a PR.
It looks like mime-type-of consumes the stream for raw Java Input Streams,
but not for the clojure.java.io/inpu-stream (Which is a BufferedInputStream).

Maybe there could be a suggestion to the user to coerce the input stream instead?

;stream is consumed
user> (let [stream (java.io.FileInputStream. "project.clj")] [(mime-type-of stream) (mime-type-of stream)])
["text/plain" "application/octet-stream"]

;stream is not consumed
user> (let [stream (clojure.java.io/input-stream "project.clj")] [(mime-type-of stream) (mime-type-of stream)])
["text/plain" "text/plain"]

@michaelklishin
Copy link
Owner

@iku000888 as long as the result is an InputStream (which is an interface, not an implementation), it should work. If buffering helps your case, I think we can introduce it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants