The WordWrapWriter class is a filter class. A WordWrapWriter object wraps a Writer or OutputStream object, filtering output to the wrapped object so that output lines are broken on word boundaries and fit nicely within the proscribed output width. Messages may be written with prefixes or without them, depending upon various settings in the WordWrapWriter object; the columnar width of the output object can be controlled, as well.
For example, the long message
Unable to open file /usr/local/etc/wombat: No such file or directory
might appear like this without a prefix:
Unable to open file /usr/local/etc/wombat: No such file or directory
and like this if the prefix is "myprog:"
myprog: Unable to open file /usr/local/etc/wombat: No such file or directory
Alternatively, if the output width is shortened, the same message can be made to wrap something like this:
myprog: Unable to open file /usr/local/etc/wombat: No such file or directory
Note how the WordWrapWriter output's logic will "tab" past the prefix on wrapped lines.
WordWrapWriter objects also support the notion of an indentation level, which is independent of the prefix. A non-zero indentation level causes each line, including the first line, to be indented that many characters. Thus, calling {@link #setIndentation(int) setIndentation()} with a value of4 will cause each output line to be preceded by 4 blanks. (It's also possible to change the indentation character from a blank to any other character. See {@link #setIndentationChar(char) setIndentationChar()}for details.)
The logic is predicated on the notion of a "message"; that is, a WordWrapWriter object has to know where a message begins and ends, so it can tell when to write the prefix, reset its internal column count, etc. The class's notion of a message is straightforward: A message consists of all characters written to the object via one of the write() methods, up to and including either an embedded newline or a call to the {@link #flush()} method. The println() methodsimplicitly call flush(), as does any write() method that encounters an embedded newline. Thus, it's rarely necessary to call flush() manually.
Notes
|
|