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