FileDocCategorySizeDatePackage
IndentingWriter.javaAPI DocAndroid 1.5 API4737Wed May 06 22:41:02 BST 2009com.android.dx.util

IndentingWriter

public 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
null-ok; optional prefix for every line
private final int
width
> 0; the maximum output width
private final int
maxIndent
> 0; the maximum indent
private int
column
>= 0; current output column (zero-based)
private boolean
collectingIndent
whether indent spaces are currently being collected
private int
indent
>= 0; current indent amount
Constructors Summary
public IndentingWriter(Writer out, int width, String prefix)
Constructs an instance.

param
out non-null; writer to send final output to
param
width >= 0; the maximum output width (not including prefix), or 0 for no maximum
param
prefix non-null; the prefix for each line

        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.

param
out non-null; writer to send final output to
param
width >= 0; the maximum output width (not including prefix), or 0 for no maximum

        this(out, width, "");
    
Methods Summary
private voidbol()
Indicates that output is at the beginning of a line.

        column = 0;
        collectingIndent = (maxIndent != 0);
        indent = 0;
    
public voidwrite(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 voidwrite(char[] cbuf, int off, int len)
{@inheritDoc}

        synchronized (lock) {
            while (len > 0) {
                write(cbuf[off]);
                off++;
                len--;
            }
        }
    
public voidwrite(java.lang.String str, int off, int len)
{@inheritDoc}

        synchronized (lock) {
            while (len > 0) {
                write(str.charAt(off));
                off++;
                len--;
            }
        }