IndentingWriterpublic final class IndentingWriter extends FilterWriter Writer that wraps another writer and passes width-limited and
optionally-prefixed output to its subordinate. When lines are
wrapped they are automatically indented based on the start of the
line. |
Fields Summary |
---|
private final String | prefix{@code null-ok;} optional prefix for every line | private final int | width{@code > 0;} the maximum output width | private final int | maxIndent{@code > 0;} the maximum indent | private int | column{@code >= 0;} current output column (zero-based) | private boolean | collectingIndentwhether indent spaces are currently being collected | private int | indent{@code >= 0;} current indent amount |
Constructors Summary |
---|
public IndentingWriter(Writer out, int width, String prefix)Constructs an instance.
super(out);
if (out == null) {
throw new NullPointerException("out == null");
}
if (width < 0) {
throw new IllegalArgumentException("width < 0");
}
if (prefix == null) {
throw new NullPointerException("prefix == null");
}
this.width = (width != 0) ? width : Integer.MAX_VALUE;
this.maxIndent = width >> 1;
this.prefix = (prefix.length() == 0) ? null : prefix;
bol();
| public IndentingWriter(Writer out, int width)Constructs a no-prefix instance.
this(out, width, "");
|
Methods Summary |
---|
private void | bol()Indicates that output is at the beginning of a line.
column = 0;
collectingIndent = (maxIndent != 0);
indent = 0;
| public void | write(int c){@inheritDoc}
synchronized (lock) {
if (collectingIndent) {
if (c == ' ") {
indent++;
if (indent >= maxIndent) {
indent = maxIndent;
collectingIndent = false;
}
} else {
collectingIndent = false;
}
}
if ((column == width) && (c != '\n")) {
out.write('\n");
column = 0;
/*
* Note: No else, so this should fall through to the next
* if statement.
*/
}
if (column == 0) {
if (prefix != null) {
out.write(prefix);
}
if (!collectingIndent) {
for (int i = 0; i < indent; i++) {
out.write(' ");
}
column = indent;
}
}
out.write(c);
if (c == '\n") {
bol();
} else {
column++;
}
}
| public void | write(char[] cbuf, int off, int len){@inheritDoc}
synchronized (lock) {
while (len > 0) {
write(cbuf[off]);
off++;
len--;
}
}
| public void | write(java.lang.String str, int off, int len){@inheritDoc}
synchronized (lock) {
while (len > 0) {
write(str.charAt(off));
off++;
len--;
}
}
|
|