Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/rmic/IndentingWriter.java
38831 views
/*1* Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*****************************************************************************/26/* Copyright (c) IBM Corporation 1998 */27/* */28/* (C) Copyright IBM Corp. 1998 */29/* */30/*****************************************************************************/3132package sun.rmi.rmic;3334import java.io.Writer;35import java.io.BufferedWriter;36import java.io.IOException;3738/**39* IndentingWriter is a BufferedWriter subclass that supports automatic40* indentation of lines of text written to the underlying Writer.41*42* Methods are provided for compact, convenient indenting, writing text,43* and writing lines in various combinations.44*45* WARNING: The contents of this source file are not part of any46* supported API. Code that depends on them does so at its own risk:47* they are subject to change or removal without notice.48*/49public class IndentingWriter extends BufferedWriter {5051/** true if the next character written is the first on a line */52private boolean beginningOfLine = true;5354/** current number of spaces to prepend to lines */55private int currentIndent = 0;5657/** number of spaces to change indent when indenting in or out */58private int indentStep = 4;5960/** number of spaces to convert into tabs. Use MAX_VALUE to disable */61private int tabSize = 8;6263/**64* Create a new IndentingWriter that writes indented text to the65* given Writer. Use the default indent step of four spaces.66*/67public IndentingWriter(Writer out) {68super(out);69}7071/**72* Create a new IndentingWriter that writes indented text to the73* given Writer and uses the supplied indent step.74*/75public IndentingWriter(Writer out, int step) {76this(out);7778if (indentStep < 0)79throw new IllegalArgumentException("negative indent step");8081indentStep = step;82}8384/**85* Create a new IndentingWriter that writes indented text to the86* given Writer and uses the supplied indent step and tab size.87*/88public IndentingWriter(Writer out, int step, int tabSize) {89this(out);9091if (indentStep < 0)92throw new IllegalArgumentException("negative indent step");9394indentStep = step;95this.tabSize = tabSize;96}9798/**99* Write a single character.100*/101public void write(int c) throws IOException {102checkWrite();103super.write(c);104}105106/**107* Write a portion of an array of characters.108*/109public void write(char[] cbuf, int off, int len) throws IOException {110if (len > 0) {111checkWrite();112}113super.write(cbuf, off, len);114}115116/**117* Write a portion of a String.118*/119public void write(String s, int off, int len) throws IOException {120if (len > 0) {121checkWrite();122}123super.write(s, off, len);124}125126/**127* Write a line separator. The next character written will be128* preceded by an indent.129*/130public void newLine() throws IOException {131super.newLine();132beginningOfLine = true;133}134135/**136* Check if an indent needs to be written before writing the next137* character.138*139* The indent generation is optimized (and made consistent with140* certain coding conventions) by condensing groups of eight spaces141* into tab characters.142*/143protected void checkWrite() throws IOException {144if (beginningOfLine) {145beginningOfLine = false;146int i = currentIndent;147while (i >= tabSize) {148super.write('\t');149i -= tabSize;150}151while (i > 0) {152super.write(' ');153-- i;154}155}156}157158/**159* Increase the current indent by the indent step.160*/161protected void indentIn() {162currentIndent += indentStep;163}164165/**166* Decrease the current indent by the indent step.167*/168protected void indentOut() {169currentIndent -= indentStep;170if (currentIndent < 0)171currentIndent = 0;172}173174/**175* Indent in.176*/177public void pI() {178indentIn();179}180181/**182* Indent out.183*/184public void pO() {185indentOut();186}187188/**189* Write string.190*/191public void p(String s) throws IOException {192write(s);193}194195/**196* End current line.197*/198public void pln() throws IOException {199newLine();200}201202/**203* Write string; end current line.204*/205public void pln(String s) throws IOException {206p(s);207pln();208}209210/**211* Write string; end current line; indent in.212*/213public void plnI(String s) throws IOException {214p(s);215pln();216pI();217}218219/**220* Indent out; write string.221*/222public void pO(String s) throws IOException {223pO();224p(s);225}226227/**228* Indent out; write string; end current line.229*/230public void pOln(String s) throws IOException {231pO(s);232pln();233}234235/**236* Indent out; write string; end current line; indent in.237*238* This method is useful for generating lines of code that both239* end and begin nested blocks, like "} else {".240*/241public void pOlnI(String s) throws IOException {242pO(s);243pln();244pI();245}246247/**248* Write Object.249*/250public void p(Object o) throws IOException {251write(o.toString());252}253/**254* Write Object; end current line.255*/256public void pln(Object o) throws IOException {257p(o.toString());258pln();259}260261/**262* Write Object; end current line; indent in.263*/264public void plnI(Object o) throws IOException {265p(o.toString());266pln();267pI();268}269270/**271* Indent out; write Object.272*/273public void pO(Object o) throws IOException {274pO();275p(o.toString());276}277278/**279* Indent out; write Object; end current line.280*/281public void pOln(Object o) throws IOException {282pO(o.toString());283pln();284}285286/**287* Indent out; write Object; end current line; indent in.288*289* This method is useful for generating lines of code that both290* end and begin nested blocks, like "} else {".291*/292public void pOlnI(Object o) throws IOException {293pO(o.toString());294pln();295pI();296}297298}299300301