public class IndentingWriter extends java.io.FilterWriter implements Indentable
Writer
semantics with caller-controlled
indenting. An indent is written (inserted) between each newline and any
subsequent (non-newline) characters that are written.
This class is not thread-safe.
Modifier and Type | Field and Description |
---|---|
private Indentable |
indent
Indent state manager.
|
private char |
last
The last byte written to the stream.
|
Constructor and Description |
---|
IndentingWriter(java.io.Writer out)
Creates an instance with the associated writer as the base and
a default 0 indent level.
|
IndentingWriter(java.io.Writer out,
int indent)
Creates an instance with the associated writer as the base.
|
Modifier and Type | Method and Description |
---|---|
void |
decrement()
Decrements the indent level by 1.
|
int |
getIndent()
Accesses the current indent level.
|
java.lang.String |
getPad()
Get the current pad.
|
int |
getWidth()
Accesses the number of spaces in a single indent level.
|
void |
increment()
Increments the indent level by 1.
|
void |
setIndent(int indent)
Sets the current indent level.
|
void |
setPad(int indent,
int width)
Sets both the indent level and the number of spaces in a single indent
level.
|
void |
setWidth(int width)
Sets the number of spaces in a single indent level.
|
void |
write(char[] ch,
int off,
int len)
Writes
len characters from the specified character array
starting at offset off in this writer. |
void |
write(int ch)
Writes the specified character to this writer (only the low order 16
bits are written, the high order 16 bits are ignored).
|
void |
write(java.lang.String str,
int off,
int len)
Writes
len characters from the specified string starting
at offset off in this writer. |
private Indentable indent
private char last
public IndentingWriter(java.io.Writer out)
out
- The writer on which we filter.public IndentingWriter(java.io.Writer out, int indent)
out
- The writer on which we filter.indent
- The initial indent level.public int getIndent()
getIndent
in interface Indentable
public void setIndent(int indent)
setIndent
in interface Indentable
indent
- New indent level.public void increment()
increment
in interface Indentable
public void decrement()
decrement
in interface Indentable
public int getWidth()
getWidth
in interface Indentable
public void setWidth(int width)
setWidth
in interface Indentable
width
- New indent width.public void setPad(int indent, int width)
setPad
in interface Indentable
indent
- New indent level.width
- New indent width.public java.lang.String getPad()
getPad
in interface Indentable
public void write(int ch) throws java.io.IOException
The current pad is inserted into the stream between all newline characters and any subsequent non-newline characters.
write
in class java.io.FilterWriter
ch
- The character to write.java.io.IOException
- if an I/O error occurs. In particular, an IOException may be
thrown if the writer has been closed.public void write(java.lang.String str, int off, int len) throws java.io.IOException, java.lang.NullPointerException, java.lang.IndexOutOfBoundsException
len
characters from the specified string starting
at offset off
in this writer. Element off
is
the first character written and off+len-1
is the last
character written.
The current pad is inserted into the stream between all newline characters and any subsequent non-newline characters.
write
in class java.io.FilterWriter
str
- The string to write.off
- The start offset in the string.len
- The number of characters to write.java.io.IOException
- If an I/O error occurs. In particular, an IOException may be
thrown if the writer has been closed.java.lang.NullPointerException
- If the string is null
.java.lang.IndexOutOfBoundsException
- If the offset or length are negative, or if offset + length
is larger than the length of the string.public void write(char[] ch, int off, int len) throws java.io.IOException, java.lang.NullPointerException, java.lang.IndexOutOfBoundsException
len
characters from the specified character array
starting at offset off
in this writer. Element ch[off] is
the first character written and ch[off+len-1] is the last character
written.
The current pad is inserted into the stream between all newline characters and any subsequent non-newline characters.
write
in class java.io.FilterWriter
ch
- The array of characters to write.off
- The start offset in the array.len
- The number of characters to write.java.io.IOException
- If an I/O error occurs. In particular, an IOException may be
thrown if the writer has been closed.java.lang.NullPointerException
- If the array is null
.java.lang.IndexOutOfBoundsException
- If the offset or length are negative, or if offset + length
is larger than the length of the array.