blob: dd5918b966dc812f16c41cadcae8f8efa3ade84e [file] [log] [blame]
Jeff Sharkey63abc372012-01-11 18:38:16 -08001/*
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;
21
22/**
23 * Lightweight wrapper around {@link PrintWriter} that automatically indents
24 * newlines based on internal state. Delays writing indent until first actual
25 * write on a newline, enabling indent modification after newline.
26 */
27public class IndentingPrintWriter extends PrintWriter {
28 private final String mIndent;
29
30 private StringBuilder mBuilder = new StringBuilder();
Jeff Browncbad9762012-09-04 21:57:59 -070031 private char[] mCurrent;
Jeff Sharkey63abc372012-01-11 18:38:16 -080032 private boolean mEmptyLine = true;
33
34 public IndentingPrintWriter(Writer writer, String indent) {
35 super(writer);
36 mIndent = indent;
37 }
38
39 public void increaseIndent() {
40 mBuilder.append(mIndent);
Jeff Browncbad9762012-09-04 21:57:59 -070041 mCurrent = null;
Jeff Sharkey63abc372012-01-11 18:38:16 -080042 }
43
44 public void decreaseIndent() {
45 mBuilder.delete(0, mIndent.length());
Jeff Browncbad9762012-09-04 21:57:59 -070046 mCurrent = null;
Jeff Sharkey63abc372012-01-11 18:38:16 -080047 }
48
Jeff Sharkey2f036c52012-04-13 12:18:13 -070049 public void printPair(String key, Object value) {
50 print(key + "=" + String.valueOf(value) + " ");
51 }
52
Jeff Sharkey63abc372012-01-11 18:38:16 -080053 @Override
Jeff Browncbad9762012-09-04 21:57:59 -070054 public void write(char[] buf, int offset, int count) {
55 final int bufferEnd = offset + count;
56 int lineStart = offset;
57 int lineEnd = offset;
58 while (lineEnd < bufferEnd) {
59 char ch = buf[lineEnd++];
60 if (ch == '\n') {
61 writeIndent();
62 super.write(buf, lineStart, lineEnd - lineStart);
63 lineStart = lineEnd;
64 mEmptyLine = true;
65 }
66 }
67
68 if (lineStart != lineEnd) {
69 writeIndent();
70 super.write(buf, lineStart, lineEnd - lineStart);
71 }
Jeff Sharkey63abc372012-01-11 18:38:16 -080072 }
73
Jeff Browncbad9762012-09-04 21:57:59 -070074 private void writeIndent() {
Jeff Sharkey63abc372012-01-11 18:38:16 -080075 if (mEmptyLine) {
76 mEmptyLine = false;
Jeff Browncbad9762012-09-04 21:57:59 -070077 if (mBuilder.length() != 0) {
78 if (mCurrent == null) {
79 mCurrent = mBuilder.toString().toCharArray();
80 }
81 super.write(mCurrent, 0, mCurrent.length);
82 }
Jeff Sharkey63abc372012-01-11 18:38:16 -080083 }
Jeff Sharkey63abc372012-01-11 18:38:16 -080084 }
85}