Does clojure.contrib.io.slurp work with binary files?

803 views
Skip to first unread message

Matt Culbreth

unread,
Apr 29, 2010, 10:30:22 PM4/29/10
to Clojure
Howdy,

I'm writing a very simple network server that needs to send a mix of
text and binary (usually image) data over a network stream.

I'm using slurp to read data from a file and send it to a stream, but
sometimes that's failing. I've got a theory that it has to do with
slurp not reading binary data correctly. Is that true? Do I need to
go down to the lower level Java classes and build a byte array using
InputFileStream and .read()?

Thanks,

Matt

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Mike Mazur

unread,
Apr 29, 2010, 11:03:08 PM4/29/10
to clo...@googlegroups.com
Hi,

On Fri, Apr 30, 2010 at 10:30, Matt Culbreth <mattcu...@gmail.com> wrote:
> I'm using slurp to read data from a file and send it to a stream, but
> sometimes that's failing.  I've got a theory that it has to do with
> slurp not reading binary data correctly.  Is that true?  Do I need to
> go down to the lower level Java classes and build a byte array using
> InputFileStream and .read()?

That's what I did for a small project. I did actually find
c.c.io.to-byte-array[1] useful.

HTH,
Mike


[1] http://github.com/richhickey/clojure-contrib/blob/master/src/main/clojure/clojure/contrib/io.clj#L465

Alex Osborne

unread,
Apr 29, 2010, 11:12:38 PM4/29/10
to clo...@googlegroups.com
Hi Matt,

Matt Culbreth <mattcu...@gmail.com> writes:

> I'm using slurp to read data from a file and send it to a stream, but
> sometimes that's failing. I've got a theory that it has to do with
> slurp not reading binary data correctly. Is that true? Do I need to
> go down to the lower level Java classes and build a byte array using
> InputFileStream and .read()?

Yep, you're spot on. The problem is that slurp reads data as
characters, not bytes, so they'll likely be interpreted as UTF-8
(depends on your locale settings unless you set it explicitly) which
will mess them up if they're not actually UTF-8 characters.

You should indeed use byte arrays or DataInputStream or ByteBuffer or
similar (depending on what sort of manipulation you need to do) rather
than strings when working with non-textual data. Note that
c.c.duck-streams (now known as c.c.io) 'copy multimethod when given
streams or byte arrays (not reader, writers or strings) will do the
right thing.

Matt Culbreth

unread,
Apr 30, 2010, 6:16:29 AM4/30/10
to Clojure
Ok great, thanks guys. Exactly what I needed.

On Apr 30, 12:12 am, Alex Osborne <a...@meshy.org> wrote:
> Hi Matt,
Reply all
Reply to author
Forward
0 new messages