package votorola.g.io; // Copyright 2011, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.io.*; import votorola.g.lang.*; /** A string writer based on a string builder as opposed to a string buffer. * * @see StringWriter */ public @ThreadRestricted final class StringWriterB extends Writer { /** Constructs a StringWriterB. */ public StringWriterB() { this( new StringBuilder() ); } /** Constructs a StringWriterB using the specified string builder. * * @see #builder() */ public StringWriterB( StringBuilder _builder ) { b = _builder; } // ------------------------------------------------------------------------------------ /** The underlying string builder. */ public StringBuilder builder() { return b; } private final StringBuilder b; // - A p p e n d a b l e -------------------------------------------------------------- public @Override StringWriterB append( final char c ) throws IOException { write( c ); return StringWriterB.this; } public @Override StringWriterB append( final CharSequence csq ) throws IOException { openCheck(); b.append( csq ); return StringWriterB.this; } public @Override StringWriterB append( final CharSequence csq, final int start, final int end ) throws IOException { openCheck(); b.append( csq, start, end ); return StringWriterB.this; } // - C l o s e a b l e ---------------------------------------------------------------- public @Override void close() { isClosed = true; } private boolean isClosed; // - F l u s h a b l e ---------------------------------------------------------------- public @Override void flush() throws IOException { openCheck(); } // - O b j e c t ---------------------------------------------------------------------- /** Returns {@linkplain #builder() builder}().toString(). */ public @Override String toString() { return b.toString(); } // - W r i t e r ---------------------------------------------------------------------- public @Override void write( final char[] cbuf ) throws IOException { openCheck(); b.append( cbuf ); } public @Override void write( final char[] cbuf, final int off, final int len ) throws IOException { openCheck(); b.append( cbuf, off, len ); } public @Override void write( final int c ) throws IOException { openCheck(); b.append( (char)c ); } public @Override void write( final String str ) throws IOException { append( str ); } public @Override void write( final String str, final int off, final int len ) throws IOException { append( str, off, off + len ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private void openCheck() throws IOException // uphold the contract of Writer { if( isClosed ) throw new IOException( "attempt to operate on a closed writer" ); } }