1 /*
2  * Copyright (C) 2020 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 android.util;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 
22 import java.io.PrintWriter;
23 import java.io.Writer;
24 import java.util.Arrays;
25 
26 /**
27  * Lightweight wrapper around {@link PrintWriter} that automatically indents
28  * newlines based on internal state. It also automatically wraps long lines
29  * based on given line length.
30  * <p>
31  * Delays writing indent until first actual write on a newline, enabling indent
32  * modification after newline.
33  *
34  * @hide
35  */
36 public class IndentingPrintWriter extends PrintWriter {
37     private final String mSingleIndent;
38     private final int mWrapLength;
39 
40     /** Mutable version of current indent */
41     private StringBuilder mIndentBuilder = new StringBuilder();
42     /** Cache of current {@link #mIndentBuilder} value */
43     private char[] mCurrentIndent;
44     /** Length of current line being built, excluding any indent */
45     private int mCurrentLength;
46 
47     /**
48      * Flag indicating if we're currently sitting on an empty line, and that
49      * next write should be prefixed with the current indent.
50      */
51     private boolean mEmptyLine = true;
52 
53     private char[] mSingleChar = new char[1];
54 
IndentingPrintWriter(@onNull Writer writer)55     public IndentingPrintWriter(@NonNull Writer writer) {
56         this(writer, "  ", -1);
57     }
58 
IndentingPrintWriter(@onNull Writer writer, @NonNull String singleIndent)59     public IndentingPrintWriter(@NonNull Writer writer, @NonNull String singleIndent) {
60         this(writer, singleIndent, null, -1);
61     }
62 
IndentingPrintWriter(@onNull Writer writer, @NonNull String singleIndent, String prefix)63     public IndentingPrintWriter(@NonNull Writer writer, @NonNull String singleIndent,
64             String prefix) {
65         this(writer, singleIndent, prefix, -1);
66     }
67 
IndentingPrintWriter(@onNull Writer writer, @NonNull String singleIndent, int wrapLength)68     public IndentingPrintWriter(@NonNull Writer writer, @NonNull String singleIndent,
69             int wrapLength) {
70         this(writer, singleIndent, null, wrapLength);
71     }
72 
IndentingPrintWriter(@onNull Writer writer, @NonNull String singleIndent, @Nullable String prefix, int wrapLength)73     public IndentingPrintWriter(@NonNull Writer writer, @NonNull String singleIndent,
74             @Nullable String prefix, int wrapLength) {
75         super(writer);
76         mSingleIndent = singleIndent;
77         mWrapLength = wrapLength;
78         if (prefix != null) {
79             mIndentBuilder.append(prefix);
80         }
81     }
82 
83     /**
84      * Overrides the indent set in the constructor for the next printed line.
85      *
86      * @deprecated Use the "prefix" constructor parameter
87      * @hide
88      */
89     @NonNull
90     @Deprecated
setIndent(@onNull String indent)91     public IndentingPrintWriter setIndent(@NonNull String indent) {
92         mIndentBuilder.setLength(0);
93         mIndentBuilder.append(indent);
94         mCurrentIndent = null;
95         return this;
96     }
97 
98     /**
99      * Overrides the indent set in the constructor with {@code singleIndent} repeated {@code indent}
100      * times.
101      *
102      * @deprecated Use the "prefix" constructor parameter
103      * @hide
104      */
105     @NonNull
106     @Deprecated
setIndent(int indent)107     public IndentingPrintWriter setIndent(int indent) {
108         mIndentBuilder.setLength(0);
109         for (int i = 0; i < indent; i++) {
110             increaseIndent();
111         }
112         return this;
113     }
114 
115     /**
116      * Increases the indent starting with the next printed line.
117      */
118     @NonNull
increaseIndent()119     public IndentingPrintWriter increaseIndent() {
120         mIndentBuilder.append(mSingleIndent);
121         mCurrentIndent = null;
122         return this;
123     }
124 
125     /**
126      * Decreases the indent starting with the next printed line.
127      */
128     @NonNull
decreaseIndent()129     public IndentingPrintWriter decreaseIndent() {
130         mIndentBuilder.delete(0, mSingleIndent.length());
131         mCurrentIndent = null;
132         return this;
133     }
134 
135     /**
136      * Prints a key-value pair.
137      */
138     @NonNull
print(@onNull String key, @Nullable Object value)139     public IndentingPrintWriter print(@NonNull String key, @Nullable Object value) {
140         String string;
141         if (value == null) {
142             string = "null";
143         } else if (value.getClass().isArray()) {
144             if (value.getClass() == boolean[].class) {
145                 string = Arrays.toString((boolean[]) value);
146             } else if (value.getClass() == byte[].class) {
147                 string = Arrays.toString((byte[]) value);
148             } else if (value.getClass() == char[].class) {
149                 string = Arrays.toString((char[]) value);
150             } else if (value.getClass() == double[].class) {
151                 string = Arrays.toString((double[]) value);
152             } else if (value.getClass() == float[].class) {
153                 string = Arrays.toString((float[]) value);
154             } else if (value.getClass() == int[].class) {
155                 string = Arrays.toString((int[]) value);
156             } else if (value.getClass() == long[].class) {
157                 string = Arrays.toString((long[]) value);
158             } else if (value.getClass() == short[].class) {
159                 string = Arrays.toString((short[]) value);
160             } else {
161                 string = Arrays.toString((Object[]) value);
162             }
163         } else {
164             string = String.valueOf(value);
165         }
166         print(key + "=" + string + " ");
167         return this;
168     }
169 
170     /**
171      * Prints a key-value pair, using hexadecimal format for the value.
172      */
173     @NonNull
printHexInt(@onNull String key, int value)174     public IndentingPrintWriter printHexInt(@NonNull String key, int value) {
175         print(key + "=0x" + Integer.toHexString(value) + " ");
176         return this;
177     }
178 
179     @Override
println()180     public void println() {
181         write('\n');
182     }
183 
184     @Override
write(int c)185     public void write(int c) {
186         mSingleChar[0] = (char) c;
187         write(mSingleChar, 0, 1);
188     }
189 
190     @Override
write(@onNull String s, int off, int len)191     public void write(@NonNull String s, int off, int len) {
192         final char[] buf = new char[len];
193         s.getChars(off, len - off, buf, 0);
194         write(buf, 0, len);
195     }
196 
197     @Override
write(@onNull char[] buf, int offset, int count)198     public void write(@NonNull char[] buf, int offset, int count) {
199         final int indentLength = mIndentBuilder.length();
200         final int bufferEnd = offset + count;
201         int lineStart = offset;
202         int lineEnd = offset;
203 
204         // March through incoming buffer looking for newlines
205         while (lineEnd < bufferEnd) {
206             char ch = buf[lineEnd++];
207             mCurrentLength++;
208             if (ch == '\n') {
209                 maybeWriteIndent();
210                 super.write(buf, lineStart, lineEnd - lineStart);
211                 lineStart = lineEnd;
212                 mEmptyLine = true;
213                 mCurrentLength = 0;
214             }
215 
216             // Wrap if we've pushed beyond line length
217             if (mWrapLength > 0 && mCurrentLength >= mWrapLength - indentLength) {
218                 if (!mEmptyLine) {
219                     // Give ourselves a fresh line to work with
220                     super.write('\n');
221                     mEmptyLine = true;
222                     mCurrentLength = lineEnd - lineStart;
223                 } else {
224                     // We need more than a dedicated line, slice it hard
225                     maybeWriteIndent();
226                     super.write(buf, lineStart, lineEnd - lineStart);
227                     super.write('\n');
228                     mEmptyLine = true;
229                     lineStart = lineEnd;
230                     mCurrentLength = 0;
231                 }
232             }
233         }
234 
235         if (lineStart != lineEnd) {
236             maybeWriteIndent();
237             super.write(buf, lineStart, lineEnd - lineStart);
238         }
239     }
240 
maybeWriteIndent()241     private void maybeWriteIndent() {
242         if (mEmptyLine) {
243             mEmptyLine = false;
244             if (mIndentBuilder.length() != 0) {
245                 if (mCurrentIndent == null) {
246                     mCurrentIndent = mIndentBuilder.toString().toCharArray();
247                 }
248                 super.write(mCurrentIndent, 0, mCurrentIndent.length);
249             }
250         }
251     }
252 }
253