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.cmd; 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 40 StructuredWriter(PrintWriter p) { 41 out = p; 42 } 43 44 final protected int getColumn() { 45 return column; 46 } 47 48 // Flush to print writer 49 public final void flush() { 50 out.print(builder.toString()); 51 builder.setLength(0); 52 } 53 54 final public void printIndent() { 55 builder.append(indentionArray, 0, indent); 56 column += indent; 57 } 58 59 final public void println() { 60 builder.append(LINE_SEPARATOR); 61 column = 0; 62 } 63 64 final public void print(String... texts) { 65 for (String text : texts) { 66 print(text); 67 } 68 } 69 70 final public void printAsString(Object o) { 71 print(String.valueOf(o)); 72 } 73 74 final public void print(String text) { 75 builder.append(text); 76 column += text.length(); 77 } 78 79 final public void print(char c) { 80 builder.append(c); 81 column++; 82 } 83 84 final public void print(int value) { 85 print(String.valueOf(value)); 86 } 87 88 final public void indent() { 89 indent += 2; 90 updateIndent(); 91 } 92 93 final public void retract() { 94 indent -= 2; 95 updateIndent(); 96 } 97 98 final public void println(String text) { 99 print(text); 100 println(); 101 } 102 103 private void updateIndent() { 104 if (indent > indentionArray.length) { 105 indentionArray = new char[indent]; 106 for (int i = 0; i < indentionArray.length; i++) { 107 indentionArray[i] = ' '; 108 } 109 } 110 } 111 }