| 1 |
/* |
| 2 |
* Copyright (C) 2022 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.server.art; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
|
| 21 |
import java.io.PrintWriter; |
| 22 |
import java.io.Writer; |
| 23 |
|
| 24 |
/** |
| 25 |
* A minimal copy of the hidden {@link android.util.IndentingPrintWriter}. |
| 26 |
* |
| 27 |
* TODO(b/264968147): Avoid the duplication. |
| 28 |
* |
| 29 |
* @hide |
| 30 |
*/ |
| 31 |
public class IndentingPrintWriter extends PrintWriter { |
| 32 |
private final String mSingleIndent; |
| 33 |
|
| 34 |
/** Mutable version of current indent */ |
| 35 |
private StringBuilder mIndentBuilder = new StringBuilder(); |
| 36 |
/** Cache of current {@link #mIndentBuilder} value */ |
| 37 |
private char[] mCurrentIndent; |
| 38 |
/** Length of current line being built, excluding any indent */ |
| 39 |
private int mCurrentLength; |
| 40 |
|
| 41 |
/** |
| 42 |
* Flag indicating if we're currently sitting on an empty line, and that |
| 43 |
* next write should be prefixed with the current indent. |
| 44 |
*/ |
| 45 |
private boolean mEmptyLine = true; |
| 46 |
|
| 47 |
private char[] mSingleChar = new char[1]; |
| 48 |
|
| 49 |
public IndentingPrintWriter(@NonNull Writer writer) { |
| 50 |
super(writer); |
| 51 |
mSingleIndent = " "; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Increases the indent starting with the next printed line. |
| 56 |
*/ |
| 57 |
@NonNull |
| 58 |
public IndentingPrintWriter increaseIndent() { |
| 59 |
mIndentBuilder.append(mSingleIndent); |
| 60 |
mCurrentIndent = null; |
| 61 |
return this; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Decreases the indent starting with the next printed line. |
| 66 |
*/ |
| 67 |
@NonNull |
| 68 |
public IndentingPrintWriter decreaseIndent() { |
| 69 |
mIndentBuilder.delete(0, mSingleIndent.length()); |
| 70 |
mCurrentIndent = null; |
| 71 |
return this; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public void println() { |
| 76 |
write('\n'); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void write(int c) { |
| 81 |
mSingleChar[0] = (char) c; |
| 82 |
write(mSingleChar, 0, 1); |
| 83 |
} |
| 84 |
|
| 85 |
@Override |
| 86 |
public void write(@NonNull String s, int off, int len) { |
| 87 |
final char[] buf = new char[len]; |
| 88 |
s.getChars(off, len - off, buf, 0); |
| 89 |
write(buf, 0, len); |
| 90 |
} |
| 91 |
|
| 92 |
@Override |
| 93 |
public void write(@NonNull char[] buf, int offset, int count) { |
| 94 |
final int indentLength = mIndentBuilder.length(); |
| 95 |
final int bufferEnd = offset + count; |
| 96 |
int lineStart = offset; |
| 97 |
int lineEnd = offset; |
| 98 |
|
| 99 |
// March through incoming buffer looking for newlines |
| 100 |
while (lineEnd < bufferEnd) { |
| 101 |
char ch = buf[lineEnd++]; |
| 102 |
mCurrentLength++; |
| 103 |
if (ch == '\n') { |
| 104 |
maybeWriteIndent(); |
| 105 |
super.write(buf, lineStart, lineEnd - lineStart); |
| 106 |
lineStart = lineEnd; |
| 107 |
mEmptyLine = true; |
| 108 |
mCurrentLength = 0; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (lineStart != lineEnd) { |
| 113 |
maybeWriteIndent(); |
| 114 |
super.write(buf, lineStart, lineEnd - lineStart); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
private void maybeWriteIndent() { |
| 119 |
if (mEmptyLine) { |
| 120 |
mEmptyLine = false; |
| 121 |
if (mIndentBuilder.length() != 0) { |
| 122 |
if (mCurrentIndent == null) { |
| 123 |
mCurrentIndent = mIndentBuilder.toString().toCharArray(); |
| 124 |
} |
| 125 |
super.write(mCurrentIndent, 0, mCurrentIndent.length); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |