package votorola.g.io; // Copyright 2007, 2012, 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 java.nio.*; import java.nio.charset.*; import votorola.g.lang.*; /** An output stream that writes to a string builder. */ public @ThreadRestricted @Warning("dead code") final class StringSink extends OutputStream { /** Constructs a StringSink. * * @see #builder() * @see #charset() */ public StringSink( StringBuilder _builder, Charset _charset ) { b = _builder; charset = _charset; decoder = charset.newDecoder(); } // ------------------------------------------------------------------------------------ /** The underlying string builder. */ public StringBuilder builder() { return b; } private final StringBuilder b; /** The character set in which this stream's bytes are encoded. */ public Charset charset() { return charset; } private final Charset charset; /** Resets this stream discarding any output in the string builder. */ public void reset() { decoder.reset(); byteBuffer.clear(); charBuffer.clear(); StringBuilderX.clear( b ); } // - O u t p u t - S t r e a m -------------------------------------------------------- public @Override void flush() throws CharacterCodingException { flushByteBuffer( /*endOfInput*/true ); } public @Override void write( final int c ) throws CharacterCodingException { if( byteBuffer.position() >= byteBuffer.limit() ) flushByteBuffer( /*endOfInput*/false ); byteBuffer.put( (byte)c ); } //// 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 ) { b.append( charBuffer.charAt( p )); } charBuffer.clear(); } }