Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/rmi/rmic/newrmic/IndentingWriter.java
38923 views
/*1* Copyright (c) 2003, 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*/2425package sun.rmi.rmic.newrmic;2627import java.io.Writer;28import java.io.BufferedWriter;29import java.io.IOException;3031/**32* A BufferedWriter that supports automatic indentation of lines of33* text written to the underlying Writer.34*35* Methods are provided for compact/convenient indenting in and out,36* writing text, and writing lines of text in various combinations.37*38* WARNING: The contents of this source file are not part of any39* supported API. Code that depends on them does so at its own risk:40* they are subject to change or removal without notice.41*42* @author Peter Jones43**/44public class IndentingWriter extends BufferedWriter {4546/** number of spaces to change indent when indenting in or out */47private final int indentStep;4849/** number of spaces to convert into tabs (use MAX_VALUE to disable) */50private final int tabSize;5152/** true if the next character written is the first on a line */53private boolean beginningOfLine = true;5455/** current number of spaces to prepend to lines */56private int currentIndent = 0;5758/**59* Creates a new IndentingWriter that writes indented text to the60* given Writer. Use the default indent step of four spaces.61**/62public IndentingWriter(Writer out) {63this(out, 4);64}6566/**67* Creates a new IndentingWriter that writes indented text to the68* given Writer and uses the supplied indent step.69**/70public IndentingWriter(Writer out, int indentStep) {71this(out, indentStep, 8);72}7374/**75* Creates a new IndentingWriter that writes indented text to the76* given Writer and uses the supplied indent step and tab size.77**/78public IndentingWriter(Writer out, int indentStep, int tabSize) {79super(out);80if (indentStep < 0) {81throw new IllegalArgumentException("negative indent step");82}83if (tabSize < 0) {84throw new IllegalArgumentException("negative tab size");85}86this.indentStep = indentStep;87this.tabSize = tabSize;88}8990/**91* Writes a single character.92**/93public void write(int c) throws IOException {94checkWrite();95super.write(c);96}9798/**99* Writes a portion of an array of characters.100**/101public void write(char[] cbuf, int off, int len) throws IOException {102if (len > 0) {103checkWrite();104}105super.write(cbuf, off, len);106}107108/**109* Writes a portion of a String.110**/111public void write(String s, int off, int len) throws IOException {112if (len > 0) {113checkWrite();114}115super.write(s, off, len);116}117118/**119* Writes a line separator. The next character written will be120* preceded by an indent.121**/122public void newLine() throws IOException {123super.newLine();124beginningOfLine = true;125}126127/**128* Checks if an indent needs to be written before writing the next129* character.130*131* The indent generation is optimized (and made consistent with132* certain coding conventions) by condensing groups of eight133* spaces into tab characters.134**/135protected void checkWrite() throws IOException {136if (beginningOfLine) {137beginningOfLine = false;138int i = currentIndent;139while (i >= tabSize) {140super.write('\t');141i -= tabSize;142}143while (i > 0) {144super.write(' ');145i--;146}147}148}149150/**151* Increases the current indent by the indent step.152**/153protected void indentIn() {154currentIndent += indentStep;155}156157/**158* Decreases the current indent by the indent step.159**/160protected void indentOut() {161currentIndent -= indentStep;162if (currentIndent < 0)163currentIndent = 0;164}165166/**167* Indents in.168**/169public void pI() {170indentIn();171}172173/**174* Indents out.175**/176public void pO() {177indentOut();178}179180/**181* Writes string.182**/183public void p(String s) throws IOException {184write(s);185}186187/**188* Ends current line.189**/190public void pln() throws IOException {191newLine();192}193194/**195* Writes string; ends current line.196**/197public void pln(String s) throws IOException {198p(s);199pln();200}201202/**203* Writes string; ends current line; indents in.204**/205public void plnI(String s) throws IOException {206p(s);207pln();208pI();209}210211/**212* Indents out; writes string.213**/214public void pO(String s) throws IOException {215pO();216p(s);217}218219/**220* Indents out; writes string; ends current line.221**/222public void pOln(String s) throws IOException {223pO(s);224pln();225}226227/**228* Indents out; writes string; ends current line; indents in.229*230* This method is useful for generating lines of code that both231* end and begin nested blocks, like "} else {".232**/233public void pOlnI(String s) throws IOException {234pO(s);235pln();236pI();237}238239/**240* Writes object.241**/242public void p(Object o) throws IOException {243write(o.toString());244}245246/**247* Writes object; ends current line.248**/249public void pln(Object o) throws IOException {250p(o.toString());251pln();252}253254/**255* Writes object; ends current line; indents in.256**/257public void plnI(Object o) throws IOException {258p(o.toString());259pln();260pI();261}262263/**264* Indents out; writes object.265**/266public void pO(Object o) throws IOException {267pO();268p(o.toString());269}270271/**272* Indents out; writes object; ends current line.273**/274public void pOln(Object o) throws IOException {275pO(o.toString());276pln();277}278279/**280* Indents out; writes object; ends current line; indents in.281*282* This method is useful for generating lines of code that both283* end and begin nested blocks, like "} else {".284**/285public void pOlnI(Object o) throws IOException {286pO(o.toString());287pln();288pI();289}290}291292293