public class IndentingOutputStream extends java.io.FilterOutputStream implements Indentable
OutputStream
semantics with caller-controlled
indenting. An indent is written (inserted) between each newline and any
subsequent (non-newline) bytes that are written.
This class is not thread-safe.
Modifier and Type | Field and Description |
---|---|
private Indentable |
indent
Indent state manager.
|
private byte |
last
The last byte written to the stream.
|
Constructor and Description |
---|
IndentingOutputStream(java.io.OutputStream out)
Creates an instance with the associated output stream as the base and
a default 0 indent level.
|
IndentingOutputStream(java.io.OutputStream out,
int indent)
Creates an instance with the associated output stream 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(byte[] b)
Writes the entire contents of the specified byte array to this output
stream.
|
void |
write(byte[] b,
int off,
int len)
Writes
len bytes from the specified byte array starting
at offset off in this output stream. |
void |
write(int b)
Writes the specified byte to this output stream (only the low order 8
bits are written, the high order 24 bits are ignored).
|
private Indentable indent
private byte last
public IndentingOutputStream(java.io.OutputStream out)
out
- The stream on which we filter.public IndentingOutputStream(java.io.OutputStream out, int indent)
out
- The stream 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 java.lang.String getPad()
getPad
in interface Indentable
public void setPad(int indent, int width)
setPad
in interface Indentable
indent
- New indent level.width
- New indent width.public void write(int b) throws java.io.IOException
The current pad is inserted into the stream between all newline characters and any subsequent non-newline bytes.
write
in class java.io.FilterOutputStream
b
- The byte to write.java.io.IOException
- If an I/O error occurs. In particular, an IOException may be
thrown if the output stream has been closed.public void write(byte[] b) throws java.io.IOException, java.lang.NullPointerException
The current pad is inserted into the stream between all newline characters and any subsequent non-newline bytes.
write
in class java.io.FilterOutputStream
b
- The byte array to write.java.io.IOException
- If an I/O error occurs. In particular, an IOException may be
thrown if the output stream has been closed.java.lang.NullPointerException
- If the array is null
.public void write(byte[] b, int off, int len) throws java.io.IOException, java.lang.NullPointerException, java.lang.IndexOutOfBoundsException
len
bytes from the specified byte array starting
at offset off
in this output stream. Element b[off] is
the first byte written and b[off+len-1] is the last byte written.
The current pad is inserted into the stream between all newline characters and any subsequent non-newline bytes.
write
in class java.io.FilterOutputStream
b
- The array of bytes to write.off
- The start offset in the array.len
- The number of bytes to write.java.io.IOException
- If an I/O error occurs. In particular, an IOException may be
thrown if the output stream 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.