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

C2BConverter

public final class C2BConverter extends Object
Efficient conversion of character to bytes. This uses the standard JDK mechansim - a writer - 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 )

Fields Summary
private static org.apache.juli.logging.Log
log
private IntermediateOutputStream
ios
private WriteConvertor
conv
private ByteChunk
bb
private String
enc
Constructors Summary
public C2BConverter(ByteChunk output, String encoding)
Create a converter, with bytes going to a byte buffer

    
                   
           
	this.bb=output;
	ios=new IntermediateOutputStream( output );
	conv=new WriteConvertor( ios, encoding );
        this.enc=encoding;
    
public C2BConverter(String encoding)
Create a converter

	this( new ByteChunk(1024), encoding );
    
Methods Summary
public final voidconvert(char c)
Generate the bytes using the specified encoding

	conv.write( c );
    
public final voidconvert(MessageBytes mb)
Convert a message bytes chars to bytes

        int type=mb.getType();
        if( type==MessageBytes.T_BYTES )
            return;
        ByteChunk orig=bb;
        setByteChunk( mb.getByteChunk());
        bb.recycle();
        bb.allocate( 32, -1 );
        
        if( type==MessageBytes.T_STR ) {
            convert( mb.getString() );
            // System.out.println("XXX Converting " + mb.getString() );
        } else if( type==MessageBytes.T_CHARS ) {
            CharChunk charC=mb.getCharChunk();
            convert( charC.getBuffer(),
                                charC.getOffset(), charC.getLength());
            //System.out.println("XXX Converting " + mb.getCharChunk() );
        } else {
            if (log.isDebugEnabled())
                log.debug("XXX unknowon type " + type );
        }
        flushBuffer();
        //System.out.println("C2B: XXX " + bb.getBuffer() + bb.getLength()); 
        setByteChunk(orig);
    
public final voidconvert(char[] c, int off, int len)
Generate the bytes using the specified encoding

	conv.write( c, off, len );
    
public final voidconvert(java.lang.String s, int off, int len)
Generate the bytes using the specified encoding

	conv.write( s, off, len );
    
public final voidconvert(java.lang.String s)
Generate the bytes using the specified encoding

	conv.write( s );
    
public final voidflushBuffer()
Flush any internal buffers into the ByteOutput or the internal byte[]

	conv.flush();
    
public ByteChunkgetByteChunk()

	return bb;
    
public java.lang.StringgetEncoding()

        return enc;
    
public final voidrecycle()
Reset the internal state, empty the buffers. The encoding remain in effect, the internal buffers remain allocated.

	conv.recycle();
	bb.recycle();
    
public voidsetByteChunk(ByteChunk bb)

	this.bb=bb;
	ios.setByteChunk( bb );