001package 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. 002 003import java.io.*; 004import votorola.g.lang.*; 005 006 007/** A string writer based on a string builder as opposed to a string buffer. 008 * 009 * @see StringWriter 010 */ 011public @ThreadRestricted final class StringWriterB extends Writer 012{ 013 014 015 /** Constructs a StringWriterB. 016 */ 017 public StringWriterB() { this( new StringBuilder() ); } 018 019 020 021 /** Constructs a StringWriterB using the specified string builder. 022 * 023 * @see #builder() 024 */ 025 public StringWriterB( StringBuilder _builder ) { b = _builder; } 026 027 028 029 // ------------------------------------------------------------------------------------ 030 031 032 /** The underlying string builder. 033 */ 034 public StringBuilder builder() { return b; } 035 036 037 private final StringBuilder b; 038 039 040 041 // - A p p e n d a b l e -------------------------------------------------------------- 042 043 044 public @Override StringWriterB append( final char c ) throws IOException 045 { 046 write( c ); 047 return StringWriterB.this; 048 } 049 050 051 052 public @Override StringWriterB append( final CharSequence csq ) throws IOException 053 { 054 openCheck(); 055 b.append( csq ); 056 return StringWriterB.this; 057 } 058 059 060 061 public @Override StringWriterB append( final CharSequence csq, final int start, final int end ) 062 throws IOException 063 { 064 openCheck(); 065 b.append( csq, start, end ); 066 return StringWriterB.this; 067 } 068 069 070 071 // - C l o s e a b l e ---------------------------------------------------------------- 072 073 074 public @Override void close() { isClosed = true; } 075 076 077 private boolean isClosed; 078 079 080 081 // - F l u s h a b l e ---------------------------------------------------------------- 082 083 084 public @Override void flush() throws IOException { openCheck(); } 085 086 087 088 // - O b j e c t ---------------------------------------------------------------------- 089 090 091 /** Returns {@linkplain #builder() builder}().toString(). 092 */ 093 public @Override String toString() { return b.toString(); } 094 095 096 097 // - W r i t e r ---------------------------------------------------------------------- 098 099 100 public @Override void write( final char[] cbuf ) throws IOException 101 { 102 openCheck(); 103 b.append( cbuf ); 104 } 105 106 107 108 public @Override void write( final char[] cbuf, final int off, final int len ) throws IOException 109 { 110 openCheck(); 111 b.append( cbuf, off, len ); 112 } 113 114 115 116 public @Override void write( final int c ) throws IOException 117 { 118 openCheck(); 119 b.append( (char)c ); 120 } 121 122 123 124 public @Override void write( final String str ) throws IOException { append( str ); } 125 126 127 128 public @Override void write( final String str, final int off, final int len ) throws IOException 129 { 130 append( str, off, off + len ); 131 } 132 133 134 135//// P r i v a t e /////////////////////////////////////////////////////////////////////// 136 137 138 private void openCheck() throws IOException // uphold the contract of Writer 139 { 140 if( isClosed ) throw new IOException( "attempt to operate on a closed writer" ); 141 } 142 143 144 145}