Class JustifyTextWriter
- java.lang.Object
-
- java.io.Writer
-
- java.io.PrintWriter
-
- org.clapper.util.io.JustifyTextWriter
-
- All Implemented Interfaces:
- java.io.Closeable, java.io.Flushable, java.lang.Appendable, java.lang.AutoCloseable
public class JustifyTextWriter extends java.io.PrintWriterThe JustifyTextWriter class is a filter class. A JustifyTextWriter object wraps a Writer or OutputStream object, filtering output to the wrapped object so that output lines are right-justified, left-justified or centered within a field. (Strictly speaking, there's no need for this class to support left-justifying lines, since that's the default. It's supported here solely for completeness.)
For instance, given the following input:
This is the first line. This, a longer line, is the second.
A JustifyTextWriter that's right-justifying lines in a 50-character field would produce:
This is the first line.| <-- This, a longer line, is the second.| <--(The arrows and vertical bars would obviously not be output. They're there to show where the field ends.)
A JustifyTextWriter that's centering lines in a 50-character field would produce:
This is the first line. | <-- This, a longer line, is the second. | <--For an interesting effect, consider wrapping a JustifyTextWriter inside a
WordWrapWriterto get word-wrapped, justified output. For instance, to wrap words on a 60-character boundary, and then center them in the same field, use code like this:final int WIDTH = 60; WordWrapWriter out = new WordWrapWriter (new JustifyWriter (JustifyWriter.CENTER, WIDTH), WIDTH);
Notes
- The class does not do any special processing of tab characters. Embedded tab characters and newline characters can have surprising (and unwanted) effects on the rendered output.
- Technically, this class probably ought to extend the java.io.FilterWriter class, since performs filtering of output written to another underlying Writer or OutputStream. However, I wanted JustifyTextWriter to be polymorphically compatible with java.io.PrintWriter, so it could be passed to methods expecting a PrintWriter object; there are more methods that expect a PrintWriter than there are methods that expect a FilterWriter.
- See Also:
WordWrapWriter,TextUtil.rightJustifyString(String,int),TextUtil.leftJustifyString(String,int),TextUtil.centerString(String,int),Writer,PrintWriter
-
-
Field Summary
Fields Modifier and Type Field and Description static intDEFAULT_LINE_LENGTHThe default line length.
-
Constructor Summary
Constructors Constructor and Description JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification)Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the default line length of 79.JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification, int lineLength)Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the specified line length.JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification)Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the default line length of 79.JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification, int lineLength)Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the specified line length.JustifyTextWriter(java.io.Writer output, JustifyStyle justification)Build an JustifyTextWriter object that will write its output to the specified Writer object, using the default line length of 79.JustifyTextWriter(java.io.Writer output, JustifyStyle justification, int lineLength)Build an JustifyTextWriter object that will write its output to the specified Writer object, using the specified line length.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description booleancheckError()Flush the stream and check its error state.voidclose()Close the stream, flushing it first.voidflush()Flush the stream.JustifyStylegetJustification()Retrieve the current justification style.intgetLineLength()Retrieve the current line length.voidprint(boolean b)Print a boolean.voidprint(char c)Print a character.voidprint(char[] s)Print an array of characters.voidprint(double d)Print a double.voidprint(float f)Print a float.voidprint(int i)Print an integer.voidprint(long l)Print a long.voidprint(java.lang.Object x)Print an Object.voidprint(short s)Print a short.voidprint(java.lang.String s)Print a String.voidprintln()End the current line.voidprintln(boolean b)Print a boolean and finish the line.voidprintln(char c)Print a character and finish the line.voidprintln(char[] s)Print an array of characters.voidprintln(double d)Print a double and finish the line.voidprintln(float f)Print a float and finish the line.voidprintln(int i)Print an integer.voidprintln(long l)Print a long and finish the line.voidprintln(java.lang.Object x)Print an Object and finish the line.voidprintln(short s)Print a short and finish the line.voidprintln(java.lang.String s)Print a String and finish the line.voidsetJustification(JustifyStyle style)Set or change the current justification style.voidsetLineLength(int newLineLength)Set the line length.voidwrite(char[] cbuf)Write an array of characters.voidwrite(char[] cbuf, int off, int len)Write a portion of an array of characters to the underlying output object.voidwrite(int c)Write a single character.voidwrite(java.lang.String s)Write a string.voidwrite(java.lang.String s, int off, int len)Write a portion of a String of characters to the underlying output object.
-
-
-
Field Detail
-
DEFAULT_LINE_LENGTH
public static final int DEFAULT_LINE_LENGTH
The default line length.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
JustifyTextWriter
public JustifyTextWriter(java.io.Writer output, JustifyStyle justification)Build an JustifyTextWriter object that will write its output to the specified Writer object, using the default line length of 79.- Parameters:
output- Where the output goes.justification- How to justify the output- See Also:
DEFAULT_LINE_LENGTH,JustifyTextWriter(Writer,JustifyStyle,int),JustifyTextWriter(PrintWriter,JustifyStyle),JustifyTextWriter(OutputStream,JustifyStyle),JustifyStyle,Writer
-
JustifyTextWriter
public JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification)Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the default line length of 79.- Parameters:
output- Where the output goes.justification- How to justify the output- See Also:
DEFAULT_LINE_LENGTH,JustifyTextWriter(Writer,JustifyStyle),JustifyTextWriter(PrintWriter,JustifyStyle,int),JustifyTextWriter(OutputStream,JustifyStyle),JustifyStyle,Writer
-
JustifyTextWriter
public JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification)Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the default line length of 79.- Parameters:
output- Where the output goes.justification- How to justify the output- See Also:
DEFAULT_LINE_LENGTH,JustifyTextWriter(OutputStream,JustifyStyle,int),JustifyTextWriter(Writer,JustifyStyle),JustifyTextWriter(PrintWriter,JustifyStyle),JustifyStyle,OutputStream
-
JustifyTextWriter
public JustifyTextWriter(java.io.Writer output, JustifyStyle justification, int lineLength)Build an JustifyTextWriter object that will write its output to the specified Writer object, using the specified line length.- Parameters:
output- Where the output goes.justification- How to justify the outputlineLength- The desired line length.- See Also:
DEFAULT_LINE_LENGTH,JustifyTextWriter(Writer,JustifyStyle),JustifyTextWriter(PrintWriter,JustifyStyle,int),JustifyTextWriter(OutputStream,JustifyStyle,int),JustifyStyle,Writer
-
JustifyTextWriter
public JustifyTextWriter(java.io.PrintWriter output, JustifyStyle justification, int lineLength)Build an JustifyTextWriter object that will write its output to the specified PrintWriter object, using the specified line length.- Parameters:
output- Where the output goes.justification- How to justify the outputlineLength- The desired line length.- See Also:
DEFAULT_LINE_LENGTH,JustifyTextWriter(PrintWriter,JustifyStyle),JustifyTextWriter(Writer,JustifyStyle,int),JustifyTextWriter(OutputStream,JustifyStyle,int),JustifyStyle,Writer
-
JustifyTextWriter
public JustifyTextWriter(java.io.OutputStream output, JustifyStyle justification, int lineLength)Build an JustifyTextWriter object that will write its output to the specified OutputStream object, using the specified line length.- Parameters:
output- Where the output goes.justification- How to justify the outputlineLength- The desired line length.- See Also:
DEFAULT_LINE_LENGTH,JustifyTextWriter(OutputStream,JustifyStyle),JustifyTextWriter(Writer,JustifyStyle,int),JustifyTextWriter(PrintWriter,JustifyStyle,int),JustifyStyle,Writer
-
-
Method Detail
-
checkError
public boolean checkError()
Flush the stream and check its error state.- Overrides:
checkErrorin classjava.io.PrintWriter
-
close
public void close()
Close the stream, flushing it first. Closing a previously-closed stream has no effect.- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfacejava.lang.AutoCloseable- Overrides:
closein classjava.io.PrintWriter
-
flush
public void flush()
Flush the stream. If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.PrintWriter
-
getJustification
public JustifyStyle getJustification()
Retrieve the current justification style.- Returns:
- The current justification style
- See Also:
setJustification(org.clapper.util.io.JustifyStyle)
-
setJustification
public void setJustification(JustifyStyle style)
Set or change the current justification style.- Parameters:
style- The new justification style- See Also:
setJustification(org.clapper.util.io.JustifyStyle)
-
getLineLength
public int getLineLength()
Retrieve the current line length.- Returns:
- The line length.
-
setLineLength
public void setLineLength(int newLineLength) throws java.lang.IndexOutOfBoundsExceptionSet the line length.- Parameters:
newLineLength- The new line length to use. A value of 0 disables wrapping.- Throws:
java.lang.IndexOutOfBoundsException- the value is negative
-
print
public void print(boolean b)
Print a boolean.- Overrides:
printin classjava.io.PrintWriter- Parameters:
b- The boolean to print
-
print
public void print(char c)
Print a character.- Overrides:
printin classjava.io.PrintWriter- Parameters:
c- The character to print
-
print
public void print(char[] s)
Print an array of characters.- Overrides:
printin classjava.io.PrintWriter- Parameters:
s- The array of characters to print
-
print
public void print(double d)
Print a double.- Overrides:
printin classjava.io.PrintWriter- Parameters:
d- The double floating point number to print
-
print
public void print(float f)
Print a float.- Overrides:
printin classjava.io.PrintWriter- Parameters:
f- The floating point number to print
-
print
public void print(int i)
Print an integer.- Overrides:
printin classjava.io.PrintWriter- Parameters:
i- The integer to print
-
print
public void print(long l)
Print a long.- Overrides:
printin classjava.io.PrintWriter- Parameters:
l- The long to print
-
print
public void print(short s)
Print a short.- Parameters:
s- The short to print
-
print
public void print(java.lang.String s)
Print a String.- Overrides:
printin classjava.io.PrintWriter- Parameters:
s- The String to print.
-
print
public void print(java.lang.Object x)
Print an Object.- Overrides:
printin classjava.io.PrintWriter- Parameters:
x- The object to print.
-
println
public void println()
End the current line.- Overrides:
printlnin classjava.io.PrintWriter
-
println
public void println(boolean b)
Print a boolean and finish the line.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
b- The boolean to print
-
println
public void println(char c)
Print a character and finish the line.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
c- The character to print
-
println
public void println(char[] s)
Print an array of characters.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
s- The array of characters to print
-
println
public void println(double d)
Print a double and finish the line.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
d- The double floating point number to print
-
println
public void println(float f)
Print a float and finish the line.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
f- The floating point number to print
-
println
public void println(int i)
Print an integer.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
i- The integer to print
-
println
public void println(long l)
Print a long and finish the line.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
l- The long to print
-
println
public void println(short s)
Print a short and finish the line.- Parameters:
s- The short to print
-
println
public void println(java.lang.String s)
Print a String and finish the line.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
s- The String to print.
-
println
public void println(java.lang.Object x)
Print an Object and finish the line.- Overrides:
printlnin classjava.io.PrintWriter- Parameters:
x- The object to print.
-
write
public void write(int c)
Write a single character. This method is called by all other write(), print() and println() methods. Thus, subclasses that wish to preprocess (or intercept) the output only have to override this method.- Overrides:
writein classjava.io.PrintWriter- Parameters:
c- Character to write
-
write
public void write(char[] cbuf, int off, int len)Write a portion of an array of characters to the underlying output object. Assumes the characters represent the start of a new line. Each line is indented according to this object's defined indentation level.- Overrides:
writein classjava.io.PrintWriter- Parameters:
cbuf- Array of charactersoff- Offset from which to start writing characterslen- Number of characters to write
-
write
public void write(java.lang.String s, int off, int len)Write a portion of a String of characters to the underlying output object. Assumes the characters represent the start of a new line. Each line is indented according to this object's defined indentation level.- Overrides:
writein classjava.io.PrintWriter- Parameters:
s- String from which to writeoff- Offset from which to start writing characterslen- Number of characters to write
-
write
public void write(java.lang.String s)
Write a string.- Overrides:
writein classjava.io.PrintWriter- Parameters:
s- String to write
-
write
public void write(char[] cbuf)
Write an array of characters.- Overrides:
writein classjava.io.PrintWriter- Parameters:
cbuf- Array of characters to write
-
-
DMelt 3.0 © DataMelt by jWork.ORG