|
1 /* |
|
2 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.jfr.internal.tool; |
|
27 |
|
28 import java.io.PrintWriter; |
|
29 |
|
30 abstract class StructuredWriter { |
|
31 private final static String LINE_SEPARATOR = String.format("%n"); |
|
32 |
|
33 private final PrintWriter out; |
|
34 private final StringBuilder builder = new StringBuilder(4000); |
|
35 |
|
36 private char[] indentionArray = new char[0]; |
|
37 private int indent = 0; |
|
38 private int column; |
|
39 // print first event immediately so tool feels responsive |
|
40 private boolean first = true; |
|
41 |
|
42 StructuredWriter(PrintWriter p) { |
|
43 out = p; |
|
44 } |
|
45 |
|
46 final protected int getColumn() { |
|
47 return column; |
|
48 } |
|
49 |
|
50 // Flush to print writer |
|
51 public final void flush(boolean hard) { |
|
52 if (hard) { |
|
53 out.print(builder.toString()); |
|
54 builder.setLength(0); |
|
55 return; |
|
56 } |
|
57 if (first || builder.length() > 100_000) { |
|
58 out.print(builder.toString()); |
|
59 builder.setLength(0); |
|
60 first = false; |
|
61 } |
|
62 } |
|
63 |
|
64 final public void printIndent() { |
|
65 builder.append(indentionArray, 0, indent); |
|
66 column += indent; |
|
67 } |
|
68 |
|
69 final public void println() { |
|
70 builder.append(LINE_SEPARATOR); |
|
71 column = 0; |
|
72 } |
|
73 |
|
74 final public void print(String... texts) { |
|
75 for (String text : texts) { |
|
76 print(text); |
|
77 } |
|
78 } |
|
79 |
|
80 final public void printAsString(Object o) { |
|
81 print(String.valueOf(o)); |
|
82 } |
|
83 |
|
84 final public void print(String text) { |
|
85 builder.append(text); |
|
86 column += text.length(); |
|
87 } |
|
88 |
|
89 final public void print(char c) { |
|
90 builder.append(c); |
|
91 column++; |
|
92 } |
|
93 |
|
94 final public void print(int value) { |
|
95 print(String.valueOf(value)); |
|
96 } |
|
97 |
|
98 final public void indent() { |
|
99 indent += 2; |
|
100 updateIndent(); |
|
101 } |
|
102 |
|
103 final public void retract() { |
|
104 indent -= 2; |
|
105 updateIndent(); |
|
106 } |
|
107 |
|
108 final public void println(String text) { |
|
109 print(text); |
|
110 println(); |
|
111 } |
|
112 |
|
113 private void updateIndent() { |
|
114 if (indent > indentionArray.length) { |
|
115 indentionArray = new char[indent]; |
|
116 for (int i = 0; i < indentionArray.length; i++) { |
|
117 indentionArray[i] = ' '; |
|
118 } |
|
119 } |
|
120 } |
|
121 } |