FileDocCategorySizeDatePackage
B2CConverter.javaAPI DocApache Tomcat 6.0.147315Fri Jul 20 04:20:36 BST 2007org.apache.tomcat.util.buf

B2CConverter

public class B2CConverter extends Object
Efficient conversion of bytes to character . This uses the standard JDK mechansim - a reader - but provides mechanisms to recycle all the objects that are used. It is compatible with JDK1.1 and up, ( nio is better, but it's not available even in 1.2 or 1.3 ) Not used in the current code, the performance gain is not very big in the current case ( since String is created anyway ), but it will be used in a later version or after the remaining optimizations.

Fields Summary
private static org.apache.juli.logging.Log
log
private IntermediateInputStream
iis
private ReadConvertor
conv
private String
encoding
static final int
BUFFER_SIZE
char[]
result
private final int
debug
Constructors Summary
protected B2CConverter()


      
    
public B2CConverter(String encoding)
Create a converter, with bytes going to a byte buffer

	this.encoding=encoding;
	reset();
    
Methods Summary
public voidconvert(ByteChunk bb, CharChunk cb)
Convert a buffer of bytes into a chars


                 
            
	 
    
	// Set the ByteChunk as input to the Intermediate reader
	iis.setByteChunk( bb );
	convert(cb);
    
private voidconvert(CharChunk cb)

	try {
	    // read from the reader
	    while( true ) { // conv.ready() ) {
		int cnt=conv.read( result, 0, BUFFER_SIZE );
		if( cnt <= 0 ) {
		    // End of stream ! - we may be in a bad state
		    if( debug>0)
			log( "EOF" );
		    //		    reset();
		    return;
		}
		if( debug > 1 )
		    log("Converted: " + new String( result, 0, cnt ));

		// XXX go directly
		cb.append( result, 0, cnt );
	    }
	} catch( IOException ex) {
	    if( debug>0)
		log( "Reseting the converter " + ex.toString() );
	    reset();
	    throw ex;
	}
    
voidlog(java.lang.String s)

         
        if (log.isDebugEnabled())
            log.debug("B2CConverter: " + s );
    
public voidrecycle()
Reset the internal state, empty the buffers. The encoding remain in effect, the internal buffers remain allocated.

	conv.recycle();
    
public voidreset()

	// destroy the reader/iis
	iis=new IntermediateInputStream();
	conv=new ReadConvertor( iis, encoding );