FileDocCategorySizeDatePackage
IndentingWriter.javaAPI DocAndroid 5.1 API4781Thu Mar 12 22:18:30 GMT 2015com.android.dexgen.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
{@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
collectingIndent
whether 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.

param
out {@code non-null;} writer to send final output to
param
width {@code >= 0;} the maximum output width (not including {@code prefix}), or {@code 0} for no maximum
param
prefix {@code 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 {@code non-null;} writer to send final output to
param
width {@code >= 0;} the maximum output width (not including {@code prefix}), or {@code 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--;
            }
        }