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 17 package com.android.internal.util; 18 19 import java.io.PrintWriter; 20 import java.io.Writer; 21 import 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 */ 31 public 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 private char[] mSingleChar = new char[1]; 49 IndentingPrintWriter(Writer writer, String singleIndent)50 public IndentingPrintWriter(Writer writer, String singleIndent) { 51 this(writer, singleIndent, -1); 52 } 53 IndentingPrintWriter(Writer writer, String singleIndent, int wrapLength)54 public IndentingPrintWriter(Writer writer, String singleIndent, int wrapLength) { 55 super(writer); 56 mSingleIndent = singleIndent; 57 mWrapLength = wrapLength; 58 } 59 setIndent(String indent)60 public IndentingPrintWriter setIndent(String indent) { 61 mIndentBuilder.setLength(0); 62 mIndentBuilder.append(indent); 63 mCurrentIndent = null; 64 return this; 65 } 66 setIndent(int indent)67 public IndentingPrintWriter setIndent(int indent) { 68 mIndentBuilder.setLength(0); 69 for (int i = 0; i < indent; i++) { 70 increaseIndent(); 71 } 72 return this; 73 } 74 increaseIndent()75 public IndentingPrintWriter increaseIndent() { 76 mIndentBuilder.append(mSingleIndent); 77 mCurrentIndent = null; 78 return this; 79 } 80 decreaseIndent()81 public IndentingPrintWriter decreaseIndent() { 82 mIndentBuilder.delete(0, mSingleIndent.length()); 83 mCurrentIndent = null; 84 return this; 85 } 86 printPair(String key, Object value)87 public IndentingPrintWriter printPair(String key, Object value) { 88 print(key + "=" + String.valueOf(value) + " "); 89 return this; 90 } 91 printPair(String key, Object[] value)92 public IndentingPrintWriter printPair(String key, Object[] value) { 93 print(key + "=" + Arrays.toString(value) + " "); 94 return this; 95 } 96 printHexPair(String key, int value)97 public IndentingPrintWriter printHexPair(String key, int value) { 98 print(key + "=0x" + Integer.toHexString(value) + " "); 99 return this; 100 } 101 102 @Override println()103 public void println() { 104 write('\n'); 105 } 106 107 @Override write(int c)108 public void write(int c) { 109 mSingleChar[0] = (char) c; 110 write(mSingleChar, 0, 1); 111 } 112 113 @Override write(String s, int off, int len)114 public void write(String s, int off, int len) { 115 final char[] buf = new char[len]; 116 s.getChars(off, len - off, buf, 0); 117 write(buf, 0, len); 118 } 119 120 @Override write(char[] buf, int offset, int count)121 public void write(char[] buf, int offset, int count) { 122 final int indentLength = mIndentBuilder.length(); 123 final int bufferEnd = offset + count; 124 int lineStart = offset; 125 int lineEnd = offset; 126 127 // March through incoming buffer looking for newlines 128 while (lineEnd < bufferEnd) { 129 char ch = buf[lineEnd++]; 130 mCurrentLength++; 131 if (ch == '\n') { 132 maybeWriteIndent(); 133 super.write(buf, lineStart, lineEnd - lineStart); 134 lineStart = lineEnd; 135 mEmptyLine = true; 136 mCurrentLength = 0; 137 } 138 139 // Wrap if we've pushed beyond line length 140 if (mWrapLength > 0 && mCurrentLength >= mWrapLength - indentLength) { 141 if (!mEmptyLine) { 142 // Give ourselves a fresh line to work with 143 super.write('\n'); 144 mEmptyLine = true; 145 mCurrentLength = lineEnd - lineStart; 146 } else { 147 // We need more than a dedicated line, slice it hard 148 maybeWriteIndent(); 149 super.write(buf, lineStart, lineEnd - lineStart); 150 super.write('\n'); 151 mEmptyLine = true; 152 lineStart = lineEnd; 153 mCurrentLength = 0; 154 } 155 } 156 } 157 158 if (lineStart != lineEnd) { 159 maybeWriteIndent(); 160 super.write(buf, lineStart, lineEnd - lineStart); 161 } 162 } 163 maybeWriteIndent()164 private void maybeWriteIndent() { 165 if (mEmptyLine) { 166 mEmptyLine = false; 167 if (mIndentBuilder.length() != 0) { 168 if (mCurrentIndent == null) { 169 mCurrentIndent = mIndentBuilder.toString().toCharArray(); 170 } 171 super.write(mCurrentIndent, 0, mCurrentIndent.length); 172 } 173 } 174 } 175 } 176