FileDocCategorySizeDatePackage
C2BConverter.javaAPI DocGlassfish v2 API6338Mon Jun 04 11:49:54 BST 2007org.apache.tomcat.util.buf

C2BConverter

public class C2BConverter extends Object
Efficient conversion of character to bytes. Now uses NIO directly

Fields Summary
private static com.sun.org.apache.commons.logging.Log
log
protected ByteChunk
bb
protected String
enc
protected CharsetEncoder
encoder
Constructors Summary
public C2BConverter(ByteChunk output, String encoding)
Create a converter, with bytes going to a byte buffer

    
                   
           
        this.bb=output;
        this.enc=encoding;
        encoder = Charset.forName(enc).newEncoder().
		onMalformedInput(CodingErrorAction.REPLACE).
		onUnmappableCharacter(CodingErrorAction.REPLACE);
    
public C2BConverter(String encoding)
Create a converter

        this( new ByteChunk(1024), encoding );
    
Methods Summary
public voidconvert(java.lang.String s, int off, int len)
Generate the bytes using the specified encoding

        convert(s.toCharArray(), off, len);
    
public voidconvert(char c)
Generate the bytes using the specified encoding

        char[] tmp = new char[1];
        tmp[0] = c;
        convert(tmp, 0, 1);
    
public 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 );
        }
        //System.out.println("C2B: XXX " + bb.getBuffer() + bb.getLength()); 
        setByteChunk(orig);
    
public voidconvert(char[] c, int off, int len)
Generate the bytes using the specified encoding

        CharBuffer cb = CharBuffer.wrap(c, off, len);
        byte[] barr = bb.getBuffer();
        int boff = bb.getEnd();
        ByteBuffer tmp = ByteBuffer.wrap(barr, boff, barr.length - boff);
        CoderResult cr = encoder.encode(cb, tmp, true);
        bb.setEnd(tmp.position());
        while (cr == CoderResult.OVERFLOW) {
	    if (!bb.canGrow())
                bb.flushBuffer();
	    boff = bb.getEnd();
	    barr = bb.getBuffer();
            tmp = ByteBuffer.wrap(barr, boff, barr.length - boff);
            cr = encoder.encode(cb, tmp, true);
            bb.setEnd(tmp.position());
        }
        if (cr != CoderResult.UNDERFLOW) {
            throw new IOException("Encoding error");
	}
    
public voidconvert(java.lang.String s)
Generate the bytes using the specified encoding

        convert(s, 0, s.length());
    
public voidflushBuffer()
Flush any internal buffers into the ByteOutput or the internal byte[]

        bb.flushBuffer();
    
public ByteChunkgetByteChunk()

        return bb;
    
public java.lang.StringgetEncoding()

        return enc;
    
public static org.apache.tomcat.util.buf.C2BConvertergetInstance(ByteChunk output, java.lang.String encoding)

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

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

        this.bb=bb;