PrintWriters / PrintStreams ?

11 views
Skip to first unread message

David Powell

unread,
Apr 17, 2010, 7:08:20 AM4/17/10
to cloju...@googlegroups.com

Hi,

There seems to be quite a lot of use of PrintWriters in
clojure.contrib. Anyone got any opinions about these?

I'm really not a fan of these wrappers at all, as they mask
exceptions. Is there any reason we shouldn't just be using
BufferedWriters?

I think it is probably ok to use PrintWriters as the implementation of
stdout, but for writing to files and sockets, they scare me.

--
Dave

--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To post to this group, send email to cloju...@googlegroups.com.
To unsubscribe from this group, send email to clojure-dev...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/clojure-dev?hl=en.

Chas Emerick

unread,
Apr 20, 2010, 6:39:41 AM4/20/10
to cloju...@googlegroups.com
I don't do a lot of writing of text to local disk, so I actually
hadn't noticed.

I presume the usage of PrintWriters is to maximize utility when
rebinding *out* and *err*. But yes, I'd worry about using
PrintWriters for general-purpose use. Of course, c.c.io/writer (and
duck-streams/writer) has been returning PrintWriters for a long time
without things burning down. The question is, is this a problem
waiting to happen, or are we just paranoid? ;-)

Cheers,

- Chas

David Powell

unread,
Apr 20, 2010, 2:55:57 PM4/20/10
to cloju...@googlegroups.com

Hi,

> I don't do a lot of writing of text to local disk, so I actually
> hadn't noticed.

> I presume the usage of PrintWriters is to maximize utility when
> rebinding *out* and *err*. But yes, I'd worry about using
> PrintWriters for general-purpose use.

I think that it is a bit of a problem.

Clojure doesn't have any 'fprintf'-like functions, which would allow
you to print to specific streams. My understanding is that rebinding
*out* to a different java.io.Writer, and using print is the
recommended way of printing text to arbitrary streams. The presence of
the print-method multi-methods, and print-dup, I think confirms that.
Those aren't things that you want to cook up with yourself with
(.println out whatever), and they aren't very useful as they are bound
to stdout.

Given the expectation to use print for things other than the console,
and the opinion that PrintWriter isn't a suitable wrapper for writing
to files and sockets, due to it dropping exceptions on the floor, I
think it is wrong for anyone to assume that *out* is a PrintWriter.
And the documentation clearly says that it is a Writer.

I understand that there is code that assumes that *out* is a
PrintWriter, even though it is documented to be a Writer. I think that
that code needs to change.

> Of course, c.c.io/writer (and
> duck-streams/writer) has been returning PrintWriters for a long time
> without things burning down. The question is, is this a problem
> waiting to happen, or are we just paranoid? ;-)

I do my file writing from Java, and don't use contrib at all, so I
haven't hit problems from Clojure either. But I would never use
PrintWriters for anything other than trivial console output when I'm
programming in Java.

Is duckstreams is moving to clojure.contrib.io for the release
transition from 1.1 to 1.2?
If so, now would be a great time to change to clojure.contrib.io to
returning BufferedWriters.

--
Dave



--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To post to this group, send email to cloju...@googlegroups.com.

Phil Hagelberg

unread,
Apr 20, 2010, 3:01:39 PM4/20/10
to cloju...@googlegroups.com
On Tue, Apr 20, 2010 at 3:39 AM, Chas Emerick <ceme...@snowtide.com> wrote:
> Of course, c.c.io/writer (and duck-streams/writer) has
> been returning PrintWriters for a long time without things burning down.
>  The question is, is this a problem waiting to happen, or are we just
> paranoid? ;-)

I was wondering the same thing when I first read this thread. Then the
very next day we got bitten by a bug where our disk filled up and our
PrintWriters happily carried on, swallowing the relevant exceptions.

So I'd vote strongly in favour of replacing c.c.io's use of PrintWriters.

-Phil

Chas Emerick

unread,
Apr 20, 2010, 6:24:16 PM4/20/10
to cloju...@googlegroups.com

On Apr 20, 2010, at 3:01 PM, Phil Hagelberg wrote:

> On Tue, Apr 20, 2010 at 3:39 AM, Chas Emerick
> <ceme...@snowtide.com> wrote:
>> Of course, c.c.io/writer (and duck-streams/writer) has
>> been returning PrintWriters for a long time without things burning
>> down.
>> The question is, is this a problem waiting to happen, or are we just
>> paranoid? ;-)
>
> I was wondering the same thing when I first read this thread. Then the
> very next day we got bitten by a bug where our disk filled up and our
> PrintWriters happily carried on, swallowing the relevant exceptions.
>
> So I'd vote strongly in favour of replacing c.c.io's use of
> PrintWriters.

Since we have a live issue connected to this, I'd +1 that, along with
David's earlier suggestion that anything that breaks due to that
change should simply be fixed to assume a Writer on *out*, not a
PrintWriter.

- Chas
Reply all
Reply to author
Forward
0 new messages