-
Enhancement
-
Resolution: Fixed
-
P3
-
1.2.0
-
1.2beta4
-
sparc
-
solaris_2.5
-
Not verified
There is currently no convenient way to write a subclass of PrintWriter that
adds custom behavior related to the beginnings or endings of lines written
downstream, such as to insert prefixes to all lines written, or to perform
automatic indentation, for example. This had been possible with the old
PrintStream class by overriding the write() method and looking for '\n' go
by (although such a technique, in a sense, illustrates the limitations of
the old PrintStream class). Currently, this customization is only possible by
doing something like overriding all 10 of the println() methods (and hoping no
new ones are added), or overriding write() or inserting something downstream
that watches for the (arbitrary?) line.separator string going by; both of these
approaches are tedious.
This RFE could be satisfied by tightening the spec for PrintWriter so that
all of println() methods that take arguments must call the no-arg println()
method to perform the newline, so that only that one method needs to be
overridden to customize the line break behavior.
Following is an e-mail exchange with Mark Reinhold on this subject, where
he suggested that I file this issue as an RFE. Note that as an alternative to
the above solution, the private newLine() method could be public (or protected)
similar to the one provided in BufferedWriter, making that the central point of
customization. Taking this idea a little further, in the e-mail below I had
wondered by BufferedWriter's newLine() method wasn't a method available to call
on all Writers, since it is generally useful text function that doesn't feel
particularly specific to buffered streams. This is another solution to the
RFE, although Mark answered why it is wasn't done that way to begin with:
symmetry with BufferedReader, and to encourage programmers to use buffered
output streams.
From Mark.Reinhold@Eng Thu Dec 4 23:17:57 1997
To: ###@###.### (Peter Jones - JavaSoft East )
Subject: Re: minor Writer question
From: Mark Reinhold <Mark.Reinhold@Eng>
[ More old e-mail ... ]
> Date: Wed, 8 Oct 1997 17:31:10 -0400
> From: ###@###.### (Peter Jones - JavaSoft East )
> I was wondering why the newLine() method is only in BufferedWriter and
> not in the Writer superclass. At a first glance, it seems more
> generally useful than just for BufferedWriter subclasses as *the* way
> to write a newline to an arbitrary Writer. I guess I can see that
> where it is, it is symmetric with readLine() being in BufferedReader.
That's pretty much why it wound up there: Symmetry. Also, it encourages people
to use BufferedWriters -- too many people use raw streams and expect good
performance. I wouldn't be opposed to adding the newLine method to Writer, but
you're the first to suggest it.
> But the angle I'm coming from is with respect to PrintWriter (which
> doesn't extend BufferedWriter; should it?)
No, it shouldn't: PrintWriters are not buffered.
> : I'd like to be able to
> provide a subclass of PrintWriter that does some custom behavior at
> each newline (such as prefix each line sent downstream with
> something). I don't see a really straightforward way of doing this,
> because PrintWriter's own newLine() is private, and all of the
> println() methods don't go through the no-arg println() for generating
> the newline, so I can't just override that method either. The current
> possibilities seem to be overriding all ten of the println() methods,
> or overriding the write() methods to watch for the "line.separator"
> going by. (That could be tedious depending on how general that string
> could be; if we know that it can only be "\n", "\r\n", or "\r", then it
> would be more feasible.)
>
> Anyway, this isn't anything really important; what I want to do is
> possible, it's just that the subclass interface of PrintWriter doesn't
> make it terribly convenient. I just wanted to hear your reaction or
> suggestions,
This looks like another one of those little design oversights that are so hard
to flush out. I see no problem with tightening the specification of
PrintWriter to say that all of its one-arg println methods use the no-arg
println method to terminate the line. Please file an RFE.
- Mark
adds custom behavior related to the beginnings or endings of lines written
downstream, such as to insert prefixes to all lines written, or to perform
automatic indentation, for example. This had been possible with the old
PrintStream class by overriding the write() method and looking for '\n' go
by (although such a technique, in a sense, illustrates the limitations of
the old PrintStream class). Currently, this customization is only possible by
doing something like overriding all 10 of the println() methods (and hoping no
new ones are added), or overriding write() or inserting something downstream
that watches for the (arbitrary?) line.separator string going by; both of these
approaches are tedious.
This RFE could be satisfied by tightening the spec for PrintWriter so that
all of println() methods that take arguments must call the no-arg println()
method to perform the newline, so that only that one method needs to be
overridden to customize the line break behavior.
Following is an e-mail exchange with Mark Reinhold on this subject, where
he suggested that I file this issue as an RFE. Note that as an alternative to
the above solution, the private newLine() method could be public (or protected)
similar to the one provided in BufferedWriter, making that the central point of
customization. Taking this idea a little further, in the e-mail below I had
wondered by BufferedWriter's newLine() method wasn't a method available to call
on all Writers, since it is generally useful text function that doesn't feel
particularly specific to buffered streams. This is another solution to the
RFE, although Mark answered why it is wasn't done that way to begin with:
symmetry with BufferedReader, and to encourage programmers to use buffered
output streams.
From Mark.Reinhold@Eng Thu Dec 4 23:17:57 1997
To: ###@###.### (Peter Jones - JavaSoft East )
Subject: Re: minor Writer question
From: Mark Reinhold <Mark.Reinhold@Eng>
[ More old e-mail ... ]
> Date: Wed, 8 Oct 1997 17:31:10 -0400
> From: ###@###.### (Peter Jones - JavaSoft East )
> I was wondering why the newLine() method is only in BufferedWriter and
> not in the Writer superclass. At a first glance, it seems more
> generally useful than just for BufferedWriter subclasses as *the* way
> to write a newline to an arbitrary Writer. I guess I can see that
> where it is, it is symmetric with readLine() being in BufferedReader.
That's pretty much why it wound up there: Symmetry. Also, it encourages people
to use BufferedWriters -- too many people use raw streams and expect good
performance. I wouldn't be opposed to adding the newLine method to Writer, but
you're the first to suggest it.
> But the angle I'm coming from is with respect to PrintWriter (which
> doesn't extend BufferedWriter; should it?)
No, it shouldn't: PrintWriters are not buffered.
> : I'd like to be able to
> provide a subclass of PrintWriter that does some custom behavior at
> each newline (such as prefix each line sent downstream with
> something). I don't see a really straightforward way of doing this,
> because PrintWriter's own newLine() is private, and all of the
> println() methods don't go through the no-arg println() for generating
> the newline, so I can't just override that method either. The current
> possibilities seem to be overriding all ten of the println() methods,
> or overriding the write() methods to watch for the "line.separator"
> going by. (That could be tedious depending on how general that string
> could be; if we know that it can only be "\n", "\r\n", or "\r", then it
> would be more feasible.)
>
> Anyway, this isn't anything really important; what I want to do is
> possible, it's just that the subclass interface of PrintWriter doesn't
> make it terribly convenient. I just wanted to hear your reaction or
> suggestions,
This looks like another one of those little design oversights that are so hard
to flush out. I see no problem with tightening the specification of
PrintWriter to say that all of its one-arg println methods use the no-arg
println method to terminate the line. Please file an RFE.
- Mark