package textbender.g.io; // Copyright 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender 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 Textbender Software. THE TEXTBENDER 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 TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.io.*; import java.nio.*; import java.nio.charset.*; /** An output stream that writes to a string builder. */ public final class StringOutputStream extends OutputStream { /** Constructs a StringOutputStream. * * @param stringBuilder per {@linkplain #stringBuilder() stringBuilder} * @param encodingName per {@linkplain #encodingName() encodingName} */ public StringOutputStream( StringBuilder stringBuilder, String encodingName ) { this.stringBuilder = stringBuilder; this.encodingName = encodingName; Charset charset = Charset.forName( encodingName ); decoder = charset.newDecoder(); } // ------------------------------------------------------------------------------------ /** Character encoding of this stream's bytes. */ public String encodingName() { return encodingName; } private final String encodingName; /** Resets this stream, discarding any output in the string builder. */ public void reset() { decoder.reset(); byteBuffer.clear(); charBuffer.clear(); stringBuilder.delete( 0, Integer.MAX_VALUE ); } /** Underlying string builder, into which output strings are written. */ public StringBuilder stringBuilder() { return stringBuilder; } private final StringBuilder stringBuilder; // - O u t p u t - S t r e a m -------------------------------------------------------- /** Does nothing. */ public void close() {} public void flush() throws CharacterCodingException { flushByteBuffer( /*endOfInput*/true ); } public void write( int b ) throws CharacterCodingException { if( byteBuffer.position() >= byteBuffer.limit() ) flushByteBuffer( /*endOfInput*/false ); byteBuffer.put( (byte)b ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final ByteBuffer byteBuffer = ByteBuffer.allocate( /*capacity*/16 ); private final CharBuffer charBuffer = CharBuffer.allocate( /*capacity*/16 ); private final CharsetDecoder decoder; private void flushByteBuffer( final boolean endOfInput ) throws CharacterCodingException { CoderResult result; // byte to char // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - byteBuffer.flip(); result = decoder.decode( byteBuffer, charBuffer, endOfInput ); byteBuffer.compact(); if( result.isError() ) result.throwException(); // char to string // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - flushCharBuffer(); if( endOfInput ) { result = decoder.flush( charBuffer ); if( result.isError() ) result.throwException(); flushCharBuffer(); } } private void flushCharBuffer() { charBuffer.flip(); for( int p = charBuffer.position(), l = charBuffer.limit(); p < l; ++p ) { stringBuilder.append( charBuffer.charAt( p )); } charBuffer.clear(); } }