1 17 18 package org.apache.tomcat.util.buf; 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.OutputStreamWriter ; 23 import java.io.UnsupportedEncodingException ; 24 25 32 public final class C2BConverter { 33 34 private static org.apache.commons.logging.Log log= 35 org.apache.commons.logging.LogFactory.getLog(C2BConverter.class ); 36 37 private IntermediateOutputStream ios; 38 private WriteConvertor conv; 39 private ByteChunk bb; 40 private String enc; 41 42 44 public C2BConverter(ByteChunk output, String encoding) throws IOException { 45 this.bb=output; 46 ios=new IntermediateOutputStream( output ); 47 conv=new WriteConvertor( ios, encoding ); 48 this.enc=encoding; 49 } 50 51 53 public C2BConverter(String encoding) throws IOException { 54 this( new ByteChunk(1024), encoding ); 55 } 56 57 public ByteChunk getByteChunk() { 58 return bb; 59 } 60 61 public String getEncoding() { 62 return enc; 63 } 64 65 public void setByteChunk(ByteChunk bb) { 66 this.bb=bb; 67 ios.setByteChunk( bb ); 68 } 69 70 73 public final void recycle() { 74 conv.recycle(); 75 bb.recycle(); 76 } 77 78 80 public final void convert(char c[], int off, int len ) throws IOException { 81 conv.write( c, off, len ); 82 } 83 84 86 public final void convert(String s, int off, int len ) throws IOException { 87 conv.write( s, off, len ); 88 } 89 90 92 public final void convert(String s ) throws IOException { 93 conv.write( s ); 94 } 95 96 98 public final void convert(char c ) throws IOException { 99 conv.write( c ); 100 } 101 102 104 public final void convert(MessageBytes mb ) throws IOException { 105 int type=mb.getType(); 106 if( type==MessageBytes.T_BYTES ) 107 return; 108 ByteChunk orig=bb; 109 setByteChunk( mb.getByteChunk()); 110 bb.recycle(); 111 bb.allocate( 32, -1 ); 112 113 if( type==MessageBytes.T_STR ) { 114 convert( mb.getString() ); 115 } else if( type==MessageBytes.T_CHARS ) { 117 CharChunk charC=mb.getCharChunk(); 118 convert( charC.getBuffer(), 119 charC.getOffset(), charC.getLength()); 120 } else { 122 if (log.isDebugEnabled()) 123 log.debug("XXX unknowon type " + type ); 124 } 125 flushBuffer(); 126 setByteChunk(orig); 128 } 129 130 133 public final void flushBuffer() throws IOException { 134 conv.flush(); 135 } 136 137 } 138 139 141 142 143 163 final class WriteConvertor extends OutputStreamWriter { 164 private IntermediateOutputStream ios; 166 167 169 171 public WriteConvertor( IntermediateOutputStream out, String enc ) 172 throws UnsupportedEncodingException 173 { 174 super( out, enc ); 175 ios=out; 176 } 177 178 180 public final void close() throws IOException { 181 } 184 185 188 public final void flush() throws IOException { 189 super.flush(); 192 } 193 194 public final void write(char cbuf[], int off, int len) throws IOException { 195 super.write( cbuf, off, len ); 197 } 198 199 201 public final void recycle() { 202 ios.disable(); 203 try { 204 flush(); 206 } catch( Exception ex ) { 207 ex.printStackTrace(); 208 } 209 ios.enable(); 210 } 211 212 } 213 214 215 221 final class IntermediateOutputStream extends OutputStream { 222 private ByteChunk tbuff; 223 private boolean enabled=true; 224 225 public IntermediateOutputStream(ByteChunk tbuff) { 226 this.tbuff=tbuff; 227 } 228 229 public final void close() throws IOException { 230 throw new IOException ("close() called - shouldn't happen "); 232 } 233 234 public final void flush() throws IOException { 235 } 238 239 public final void write(byte cbuf[], int off, int len) throws IOException { 240 if( enabled ) { 242 tbuff.append( cbuf, off, len ); 243 } 244 } 245 246 public final void write( int i ) throws IOException { 247 throw new IOException ("write( int ) called - shouldn't happen "); 248 } 249 250 252 void setByteChunk( ByteChunk bb ) { 253 tbuff=bb; 254 } 255 256 259 final void disable() { 260 enabled=false; 261 } 262 263 265 final void enable() { 266 enabled=true; 267 } 268 } 269 | Popular Tags |