FileDocCategorySizeDatePackage
B2CConverter.javaAPI DocGlassfish v2 API8824Fri May 04 22:33:14 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 com.sun.org.apache.commons.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;
	}
    
public static voidconvertASCII(MessageBytes mb)
Character conversion of a US-ASCII MessageBytes.

 
        // This is of course only meaningful for bytes
        if (mb.getType() != MessageBytes.T_BYTES)
            return;
        
        ByteChunk bc = mb.getByteChunk();
        CharChunk cc = mb.getCharChunk();
        int length = bc.getLength();
        cc.allocate(length, -1);

        // Default encoding: fast conversion
        byte[] bbuf = bc.getBuffer();
        char[] cbuf = cc.getBuffer();
        int start = bc.getStart();
        for (int i = 0; i < length; i++) {
            cbuf[i] = (char) (bbuf[i + start] & 0xff);
        }
        mb.setChars(cbuf, 0, length);
   
     
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 );