| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class IndentingPrintWriter extends PrintWriter { |
| 37 |
private final String mSingleIndent; |
| 38 |
private final int mWrapLength; |
| 39 |
|
| 40 |
|
| 41 |
private StringBuilder mIndentBuilder = new StringBuilder(); |
| 42 |
|
| 43 |
private char[] mCurrentIndent; |
| 44 |
|
| 45 |
private int mCurrentLength; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private boolean mEmptyLine = true; |
| 52 |
|
| 53 |
private char[] mSingleChar = new char[1]; |
| 54 |
|
| 55 |
public IndentingPrintWriter(@NonNull Writer writer) { |
| 56 |
this(writer, " ", -1); |
| 57 |
} |
| 58 |
|
| 59 |
public IndentingPrintWriter(@NonNull Writer writer, @NonNull String singleIndent) { |
| 60 |
this(writer, singleIndent, null, -1); |
| 61 |
} |
| 62 |
|
| 63 |
public IndentingPrintWriter(@NonNull Writer writer, @NonNull String singleIndent, |
| 64 |
String prefix) { |
| 65 |
this(writer, singleIndent, prefix, -1); |
| 66 |
} |
| 67 |
|
| 68 |
public IndentingPrintWriter(@NonNull Writer writer, @NonNull String singleIndent, |
| 69 |
int wrapLength) { |
| 70 |
this(writer, singleIndent, null, wrapLength); |
| 71 |
} |
| 72 |
|
| 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 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@NonNull |
| 90 |
@Deprecated |
| 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 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
@NonNull |
| 106 |
@Deprecated |
| 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 |
|
| 117 |
|
| 118 |
@NonNull |
| 119 |
public IndentingPrintWriter increaseIndent() { |
| 120 |
mIndentBuilder.append(mSingleIndent); |
| 121 |
mCurrentIndent = null; |
| 122 |
return this; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
@NonNull |
| 129 |
public IndentingPrintWriter decreaseIndent() { |
| 130 |
mIndentBuilder.delete(0, mSingleIndent.length()); |
| 131 |
mCurrentIndent = null; |
| 132 |
return this; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
@NonNull |
| 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 |
|
| 172 |
|
| 173 |
@NonNull |
| 174 |
public IndentingPrintWriter printHexInt(@NonNull String key, int value) { |
| 175 |
print(key + "=0x" + Integer.toHexString(value) + " "); |
| 176 |
return this; |
| 177 |
} |
| 178 |
|
| 179 |
@Override |
| 180 |
public void println() { |
| 181 |
write('\n'); |
| 182 |
} |
| 183 |
|
| 184 |
@Override |
| 185 |
public void write(int c) { |
| 186 |
mSingleChar[0] = (char) c; |
| 187 |
write(mSingleChar, 0, 1); |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 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 |
| 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 |
|
| 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 |
|
| 217 |
if (mWrapLength > 0 && mCurrentLength >= mWrapLength - indentLength) { |
| 218 |
if (!mEmptyLine) { |
| 219 |
|
| 220 |
super.write('\n'); |
| 221 |
mEmptyLine = true; |
| 222 |
mCurrentLength = lineEnd - lineStart; |
| 223 |
} else { |
| 224 |
|
| 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 |
|
| 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 |
} |