C2BConverterpublic 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 void | convert(java.lang.String s, int off, int len)Generate the bytes using the specified encoding
convert(s.toCharArray(), off, len);
| public void | convert(char c)Generate the bytes using the specified encoding
char[] tmp = new char[1];
tmp[0] = c;
convert(tmp, 0, 1);
| public void | convert(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 void | convert(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 void | convert(java.lang.String s)Generate the bytes using the specified encoding
convert(s, 0, s.length());
| public void | flushBuffer()Flush any internal buffers into the ByteOutput or the internal
byte[]
bb.flushBuffer();
| public ByteChunk | getByteChunk()
return bb;
| public java.lang.String | getEncoding()
return enc;
| public static org.apache.tomcat.util.buf.C2BConverter | getInstance(ByteChunk output, java.lang.String encoding)
return new C2BConverter(output, encoding);
| public void | recycle()Reset the internal state, empty the buffers.
The encoding remain in effect, the internal buffers remain allocated.
bb.recycle();
| public void | setByteChunk(ByteChunk bb)
this.bb=bb;
|
|