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