IndentingPrintWriter.java revision 1b8ef7e3165ff9aa52a4905dafc8d0f83e7403f9
1/*
2 * Copyright (C) 2012 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
17package com.android.internal.util;
18
19import java.io.PrintWriter;
20import java.io.Writer;
21import java.util.Arrays;
22
23/**
24 * Lightweight wrapper around {@link PrintWriter} that automatically indents
25 * newlines based on internal state. It also automatically wraps long lines
26 * based on given line length.
27 * <p>
28 * Delays writing indent until first actual write on a newline, enabling indent
29 * modification after newline.
30 */
31public class IndentingPrintWriter extends PrintWriter {
32    private final String mSingleIndent;
33    private final int mWrapLength;
34
35    /** Mutable version of current indent */
36    private StringBuilder mIndentBuilder = new StringBuilder();
37    /** Cache of current {@link #mIndentBuilder} value */
38    private char[] mCurrentIndent;
39    /** Length of current line being built, excluding any indent */
40    private int mCurrentLength;
41
42    /**
43     * Flag indicating if we're currently sitting on an empty line, and that
44     * next write should be prefixed with the current indent.
45     */
46    private boolean mEmptyLine = true;
47
48    public IndentingPrintWriter(Writer writer, String singleIndent) {
49        this(writer, singleIndent, -1);
50    }
51
52    public IndentingPrintWriter(Writer writer, String singleIndent, int wrapLength) {
53        super(writer);
54        mSingleIndent = singleIndent;
55        mWrapLength = wrapLength;
56    }
57
58    public void increaseIndent() {
59        mIndentBuilder.append(mSingleIndent);
60        mCurrentIndent = null;
61    }
62
63    public void decreaseIndent() {
64        mIndentBuilder.delete(0, mSingleIndent.length());
65        mCurrentIndent = null;
66    }
67
68    public void printPair(String key, Object value) {
69        print(key + "=" + String.valueOf(value) + " ");
70    }
71
72    public void printPair(String key, Object[] value) {
73        print(key + "=" + Arrays.toString(value) + " ");
74    }
75
76    public void printHexPair(String key, int value) {
77        print(key + "=0x" + Integer.toHexString(value) + " ");
78    }
79
80    @Override
81    public void write(char[] buf, int offset, int count) {
82        final int indentLength = mIndentBuilder.length();
83        final int bufferEnd = offset + count;
84        int lineStart = offset;
85        int lineEnd = offset;
86
87        // March through incoming buffer looking for newlines
88        while (lineEnd < bufferEnd) {
89            char ch = buf[lineEnd++];
90            mCurrentLength++;
91            if (ch == '\n') {
92                maybeWriteIndent();
93                super.write(buf, lineStart, lineEnd - lineStart);
94                lineStart = lineEnd;
95                mEmptyLine = true;
96                mCurrentLength = 0;
97            }
98
99            // Wrap if we've pushed beyond line length
100            if (mWrapLength > 0 && mCurrentLength >= mWrapLength - indentLength) {
101                if (!mEmptyLine) {
102                    // Give ourselves a fresh line to work with
103                    super.write('\n');
104                    mEmptyLine = true;
105                    mCurrentLength = lineEnd - lineStart;
106                } else {
107                    // We need more than a dedicated line, slice it hard
108                    maybeWriteIndent();
109                    super.write(buf, lineStart, lineEnd - lineStart);
110                    super.write('\n');
111                    mEmptyLine = true;
112                    lineStart = lineEnd;
113                    mCurrentLength = 0;
114                }
115            }
116        }
117
118        if (lineStart != lineEnd) {
119            maybeWriteIndent();
120            super.write(buf, lineStart, lineEnd - lineStart);
121        }
122    }
123
124    private void maybeWriteIndent() {
125        if (mEmptyLine) {
126            mEmptyLine = false;
127            if (mIndentBuilder.length() != 0) {
128                if (mCurrentIndent == null) {
129                    mCurrentIndent = mIndentBuilder.toString().toCharArray();
130                }
131                super.write(mCurrentIndent, 0, mCurrentIndent.length);
132            }
133        }
134    }
135}
136