public class WordWrapWriter
extends java.io.PrintWriter
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
setIndentation()
with a value of
4 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 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 flush()
method. The println() methods
implicitly call flush(), as does any write() method
that encounters an embedded newline. Thus, it's rarely necessary to call
flush() manually.
Notes
Writer
,
PrintWriter
Modifier and Type | Field and Description |
---|---|
static int |
DEFAULT_LINE_LENGTH
The default line length.
|
Constructor and Description |
---|
WordWrapWriter(java.io.OutputStream output)
Build an
WordWrapWriter object that will write its
output to the specified OutputStream object, using the
default line length of 80. |
WordWrapWriter(java.io.OutputStream output,
int lineLength)
Build an
WordWrapWriter object that will write its
output to the specified OutputStream object, using the
specified line length. |
WordWrapWriter(java.io.OutputStream output,
int lineLength,
int indentSpaces)
Build an
WordWrapWriter object that will write its
output to the specified OutputStream object, using the
specified line length. |
WordWrapWriter(java.io.PrintWriter output)
Build an
WordWrapWriter object that will write its
output to the specified PrintWriter object, using the
default line length of 80. |
WordWrapWriter(java.io.PrintWriter output,
int lineLength)
Build an
WordWrapWriter object that will write its
output to the specified PrintWriter object, using the
specified line length. |
WordWrapWriter(java.io.PrintWriter output,
int lineLength,
int indentSpaces)
Build an
WordWrapWriter object that will write its
output to the specified PrintWriter object, using the
specified line length. |
WordWrapWriter(java.io.Writer output)
Build an
WordWrapWriter object that will write its
output to the specified Writer object, using the
default line length of 80. |
WordWrapWriter(java.io.Writer output,
int lineLength)
Build an
WordWrapWriter object that will write its
output to the specified Writer object, using the
specified line length. |
WordWrapWriter(java.io.Writer output,
int lineLength,
int indentSpaces)
Build an
WordWrapWriter object that will write its
output to the specified Writer object, using the
specified line length. |
Modifier and Type | Method and Description |
---|---|
boolean |
checkError()
Flush the stream and check its error state.
|
void |
close()
Close the stream, flushing it first.
|
void |
flush()
Flush the stream.
|
int |
getIndentation()
Retrieve the current indentation setting for wrapped lines.
|
char |
getIndentationChar()
Get the current indentation character.
|
int |
getLineLength()
Retrieve the current line length.
|
java.lang.String |
getPrefix()
Get the current prefix.
|
void |
print(boolean b)
Print a boolean.
|
void |
print(char c)
Print a character.
|
void |
print(char[] s)
Print an array of characters.
|
void |
print(double d)
Print a double.
|
void |
print(float f)
Print a float.
|
void |
print(int i)
Print an integer.
|
void |
print(long l)
Print a long.
|
void |
print(java.lang.Object x)
Print an Object.
|
void |
print(short s)
Print a short.
|
void |
print(java.lang.String s)
Print a String.
|
void |
println()
End the current line.
|
void |
println(boolean b)
Print a boolean and finish the line.
|
void |
println(char c)
Print a character and finish the line.
|
void |
println(char[] s)
Print an array of characters.
|
void |
println(double d)
Print a double and finish the line.
|
void |
println(float f)
Print a float and finish the line.
|
void |
println(int i)
Print an integer.
|
void |
println(long l)
Print a long and finish the line.
|
void |
println(java.lang.Object x)
Print an Object and finish the line.
|
void |
println(short s)
Print a short and finish the line.
|
void |
println(java.lang.String s)
Print a String and finish the line.
|
void |
setIndentation(int newIndentation)
Set the indentation value for wrapped lines.
|
void |
setIndentationChar(char c)
Change the indentation character.
|
void |
setLineLength(int newLineLength)
Set the line length.
|
java.lang.String |
setPrefix(java.lang.String newPrefix)
Set the current prefix.
|
void |
write(char[] cbuf)
Write an array of characters.
|
void |
write(char[] cbuf,
int off,
int len)
Write a portion of an array of characters to the underlying
output object.
|
void |
write(int c)
Write a single character.
|
void |
write(java.lang.String s)
Write a string.
|
void |
write(java.lang.String s,
int off,
int len)
Write a portion of a String of characters to the underlying
output object.
|
public static final int DEFAULT_LINE_LENGTH
public WordWrapWriter(java.io.Writer output)
WordWrapWriter
object that will write its
output to the specified Writer
object, using the
default line length of 80.output
- Where the output goes.DEFAULT_LINE_LENGTH
,
WordWrapWriter(Writer,int)
,
WordWrapWriter(Writer,int,int)
,
WordWrapWriter(PrintWriter)
,
WordWrapWriter(OutputStream)
,
Writer
public WordWrapWriter(java.io.PrintWriter output)
WordWrapWriter
object that will write its
output to the specified PrintWriter
object, using the
default line length of 80.output
- Where the output goes.DEFAULT_LINE_LENGTH
,
WordWrapWriter(Writer)
,
WordWrapWriter(PrintWriter,int)
,
WordWrapWriter(PrintWriter,int,int)
,
WordWrapWriter(OutputStream)
,
Writer
public WordWrapWriter(java.io.OutputStream output)
WordWrapWriter
object that will write its
output to the specified OutputStream
object, using the
default line length of 80.output
- Where the output goes.DEFAULT_LINE_LENGTH
,
WordWrapWriter(OutputStream,int)
,
WordWrapWriter(OutputStream,int,int)
,
WordWrapWriter(Writer)
,
WordWrapWriter(PrintWriter)
,
OutputStream
public WordWrapWriter(java.io.Writer output, int lineLength)
WordWrapWriter
object that will write its
output to the specified Writer
object, using the
specified line length.output
- Where the output goes.lineLength
- The desired line length.DEFAULT_LINE_LENGTH
,
WordWrapWriter(Writer)
,
WordWrapWriter(Writer,int,int)
,
WordWrapWriter(PrintWriter,int)
,
WordWrapWriter(OutputStream,int)
,
Writer
public WordWrapWriter(java.io.PrintWriter output, int lineLength)
WordWrapWriter
object that will write its
output to the specified PrintWriter
object, using the
specified line length.output
- Where the output goes.lineLength
- The desired line length.DEFAULT_LINE_LENGTH
,
WordWrapWriter(PrintWriter)
,
WordWrapWriter(PrintWriter,int,int)
,
WordWrapWriter(Writer,int)
,
WordWrapWriter(OutputStream,int)
,
Writer
public WordWrapWriter(java.io.OutputStream output, int lineLength)
WordWrapWriter
object that will write its
output to the specified OutputStream
object, using the
specified line length.output
- Where the output goes.lineLength
- The desired line length.DEFAULT_LINE_LENGTH
,
WordWrapWriter(OutputStream)
,
WordWrapWriter(OutputStream,int,int)
,
WordWrapWriter(Writer,int)
,
WordWrapWriter(PrintWriter,int)
,
Writer
public WordWrapWriter(java.io.Writer output, int lineLength, int indentSpaces)
WordWrapWriter
object that will write its
output to the specified Writer
object, using the
specified line length. In addition, wrapped lines will be indented
by the indicated number of spaces.output
- Where the output goes.lineLength
- The desired line length.indentSpaces
- How many blanks to indent lines that are wrapped.DEFAULT_LINE_LENGTH
,
WordWrapWriter(Writer)
,
WordWrapWriter(Writer,int)
,
WordWrapWriter(PrintWriter,int,int)
,
WordWrapWriter(OutputStream,int,int)
,
setIndentationChar(char)
,
Writer
public WordWrapWriter(java.io.PrintWriter output, int lineLength, int indentSpaces)
WordWrapWriter
object that will write its
output to the specified PrintWriter
object, using the
specified line length. In addition, wrapped lines will be indented
by the indicated number of spaces.output
- Where the output goes.lineLength
- The desired line length.indentSpaces
- How many blanks to indent lines that are wrapped.DEFAULT_LINE_LENGTH
,
WordWrapWriter(PrintWriter)
,
WordWrapWriter(PrintWriter,int)
,
WordWrapWriter(Writer,int,int)
,
WordWrapWriter(OutputStream,int,int)
,
setIndentationChar(char)
,
Writer
public WordWrapWriter(java.io.OutputStream output, int lineLength, int indentSpaces)
WordWrapWriter
object that will write its
output to the specified OutputStream
object, using the
specified line length. In addition, wrapped lines will be indented
by the indicated number of spaces.output
- Where the output goes.lineLength
- The desired line length.indentSpaces
- How many blanks to indent lines that are wrapped.DEFAULT_LINE_LENGTH
,
WordWrapWriter(OutputStream)
,
WordWrapWriter(OutputStream,int)
,
WordWrapWriter(Writer,int,int)
,
WordWrapWriter(PrintWriter,int,int)
,
setIndentationChar(char)
,
Writer
public boolean checkError()
checkError
in class java.io.PrintWriter
public void close()
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.PrintWriter
public void flush()
flush
in interface java.io.Flushable
flush
in class java.io.PrintWriter
public int getIndentation()
public char getIndentationChar()
setIndentationChar(char)
public java.lang.String getPrefix()
setPrefix(java.lang.String)
public int getLineLength()
public void setIndentation(int newIndentation)
newIndentation
- The indentation setting, a count that
indicates how many leading spaces will be emitted
on wrapped lines. A value of 0 disables the
feature.java.lang.IndexOutOfBoundsException
- the value is negativepublic void setIndentationChar(char c)
c
- The new indentation character.getIndentationChar()
public void setLineLength(int newLineLength) throws java.lang.IndexOutOfBoundsException
newLineLength
- The new line length to use. A value of 0
disables wrapping.java.lang.IndexOutOfBoundsException
- the value is negativepublic java.lang.String setPrefix(java.lang.String newPrefix)
foo: Unable to access the frammistat: No such file or device.
newPrefix
- The new prefix to be setgetPrefix()
public void print(boolean b)
print
in class java.io.PrintWriter
b
- The boolean to printpublic void print(char c)
print
in class java.io.PrintWriter
c
- The character to printpublic void print(char[] s)
print
in class java.io.PrintWriter
s
- The array of characters to printpublic void print(double d)
print
in class java.io.PrintWriter
d
- The double floating point number to printpublic void print(float f)
print
in class java.io.PrintWriter
f
- The floating point number to printpublic void print(int i)
print
in class java.io.PrintWriter
i
- The integer to printpublic void print(long l)
print
in class java.io.PrintWriter
l
- The long to printpublic void print(short s)
s
- The short to printpublic void print(java.lang.String s)
print
in class java.io.PrintWriter
s
- The String to print.public void print(java.lang.Object x)
print
in class java.io.PrintWriter
x
- The object to print.public void println()
println
in class java.io.PrintWriter
public void println(boolean b)
println
in class java.io.PrintWriter
b
- The boolean to printpublic void println(char c)
println
in class java.io.PrintWriter
c
- The character to printpublic void println(char[] s)
println
in class java.io.PrintWriter
s
- The array of characters to printpublic void println(double d)
println
in class java.io.PrintWriter
d
- The double floating point number to printpublic void println(float f)
println
in class java.io.PrintWriter
f
- The floating point number to printpublic void println(int i)
println
in class java.io.PrintWriter
i
- The integer to printpublic void println(long l)
println
in class java.io.PrintWriter
l
- The long to printpublic void println(short s)
s
- The short to printpublic void println(java.lang.String s)
println
in class java.io.PrintWriter
s
- The String to print.public void println(java.lang.Object x)
println
in class java.io.PrintWriter
x
- The object to print.public void write(int c)
write
in class java.io.PrintWriter
c
- Character to writepublic void write(char[] cbuf, int off, int len)
write
in class java.io.PrintWriter
cbuf
- Array of charactersoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void write(java.lang.String s, int off, int len)
write
in class java.io.PrintWriter
s
- String from which to writeoff
- Offset from which to start writing characterslen
- Number of characters to writepublic void write(java.lang.String s)
write
in class java.io.PrintWriter
s
- String to writepublic void write(char[] cbuf)
write
in class java.io.PrintWriter
cbuf
- Array of characters to write