1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * As per the Apache license requirements, this file has been modified
19 * from its original state.
20 *
21 * Such modifications are Copyright (C) 2010 Ben Gruver, and are released
22 * under the original license
23 */
24
25package org.jf.dexlib.Util;
26
27import java.io.FilterWriter;
28import java.io.IOException;
29import java.io.Writer;
30
31/**
32 * Writer that wraps another writer and passes width-limited and
33 * optionally-prefixed output to its subordinate. When lines are
34 * wrapped they are automatically indented based on the start of the
35 * line.
36 */
37public final class IndentingWriter extends FilterWriter {
38    /** null-ok; optional prefix for every line */
39    private final String prefix;
40
41    /** > 0; the maximum output width */
42    private final int width;
43
44    /** > 0; the maximum indent */
45    private final int maxIndent;
46
47    /** >= 0; current output column (zero-based) */
48    private int column;
49
50    /** whether indent spaces are currently being collected */
51    private boolean collectingIndent;
52
53    /** >= 0; current indent amount */
54    private int indent;
55
56    /**
57     * Constructs an instance.
58     *
59     * @param out non-null; writer to send final output to
60     * @param width >= 0; the maximum output width (not including
61     * <code>prefix</code>), or <code>0</code> for no maximum
62     * @param prefix non-null; the prefix for each line
63     */
64    public IndentingWriter(Writer out, int width, String prefix) {
65        super(out);
66
67        if (out == null) {
68            throw new NullPointerException("out == null");
69        }
70
71        if (width < 0) {
72            throw new IllegalArgumentException("width < 0");
73        }
74
75        if (prefix == null) {
76            throw new NullPointerException("prefix == null");
77        }
78
79        this.width = (width != 0) ? width : Integer.MAX_VALUE;
80        this.maxIndent = width >> 1;
81        this.prefix = (prefix.length() == 0) ? null : prefix;
82
83        bol();
84    }
85
86    /**
87     * Constructs a no-prefix instance.
88     *
89     * @param out non-null; writer to send final output to
90     * @param width &gt;= 0; the maximum output width (not including
91     * <code>prefix</code>), or <code>0</code> for no maximum
92     */
93    public IndentingWriter(Writer out, int width) {
94        this(out, width, "");
95    }
96
97    /** {@inheritDoc} */
98    @Override
99    public void write(int c) throws IOException {
100        synchronized (lock) {
101            if (collectingIndent) {
102                if (c == ' ') {
103                    indent++;
104                    if (indent >= maxIndent) {
105                        indent = maxIndent;
106                        collectingIndent = false;
107                    }
108                } else {
109                    collectingIndent = false;
110                }
111            }
112
113            if ((column == width) && (c != '\n')) {
114                out.write('\n');
115                column = 0;
116                /*
117                 * Note: No else, so this should fall through to the next
118                 * if statement.
119                 */
120            }
121
122            if (column == 0) {
123                if (prefix != null) {
124                    out.write(prefix);
125                }
126
127                if (!collectingIndent) {
128                    for (int i = 0; i < indent; i++) {
129                        out.write(' ');
130                    }
131                    column = indent;
132                }
133            }
134
135            out.write(c);
136
137            if (c == '\n') {
138                bol();
139            } else {
140                column++;
141            }
142        }
143    }
144
145    /** {@inheritDoc} */
146    @Override
147    public void write(char[] cbuf, int off, int len) throws IOException {
148        synchronized (lock) {
149            while (len > 0) {
150                write(cbuf[off]);
151                off++;
152                len--;
153            }
154        }
155    }
156
157    /** {@inheritDoc} */
158    @Override
159    public void write(String str, int off, int len) throws IOException {
160        synchronized (lock) {
161            while (len > 0) {
162                write(str.charAt(off));
163                off++;
164                len--;
165            }
166        }
167    }
168
169    /**
170     * Indicates that output is at the beginning of a line.
171     */
172    private void bol() {
173        column = 0;
174        collectingIndent = (maxIndent != 0);
175        indent = 0;
176    }
177}
178