1 17 18 19 package org.apache.tomcat.util.buf; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.InputStreamReader ; 24 import java.io.UnsupportedEncodingException ; 25 26 37 public class B2CConverter { 38 39 40 private static org.apache.commons.logging.Log log= 41 org.apache.commons.logging.LogFactory.getLog( B2CConverter.class ); 42 43 private IntermediateInputStream iis; 44 private ReadConvertor conv; 45 private String encoding; 46 47 protected B2CConverter() { 48 } 49 50 52 public B2CConverter(String encoding) 53 throws IOException 54 { 55 this.encoding=encoding; 56 reset(); 57 } 58 59 60 63 public void recycle() { 64 conv.recycle(); 65 } 66 67 static final int BUFFER_SIZE=8192; 68 char result[]=new char[BUFFER_SIZE]; 69 70 72 public void convert( ByteChunk bb, CharChunk cb ) 73 throws IOException 74 { 75 iis.setByteChunk( bb ); 77 convert(cb); 78 } 79 80 private void convert(CharChunk cb) 81 throws IOException 82 { 83 try { 84 while( true ) { int cnt=conv.read( result, 0, BUFFER_SIZE ); 87 if( cnt <= 0 ) { 88 if( debug>0) 90 log( "EOF" ); 91 return; 93 } 94 if( debug > 1 ) 95 log("Converted: " + new String ( result, 0, cnt )); 96 97 cb.append( result, 0, cnt ); 99 } 100 } catch( IOException ex) { 101 if( debug>0) 102 log( "Reseting the converter " + ex.toString() ); 103 reset(); 104 throw ex; 105 } 106 } 107 108 public void reset() 109 throws IOException 110 { 111 iis=new IntermediateInputStream(); 113 conv=new ReadConvertor( iis, encoding ); 114 } 115 116 private final int debug=0; 117 void log( String s ) { 118 if (log.isDebugEnabled()) 119 log.debug("B2CConverter: " + s ); 120 } 121 122 124 176 } 177 178 180 181 182 185 final class ReadConvertor extends InputStreamReader { 186 private IntermediateInputStream iis; 188 189 191 193 public ReadConvertor( IntermediateInputStream in, String enc ) 194 throws UnsupportedEncodingException 195 { 196 super( in, enc ); 197 iis=in; 198 } 199 200 202 public final void close() throws IOException { 203 } 206 207 public final int read(char cbuf[], int off, int len) 208 throws IOException 209 { 210 return super.read( cbuf, off, len ); 212 } 213 214 216 public final void recycle() { 217 } 218 } 219 220 221 227 final class IntermediateInputStream extends InputStream { 228 byte buf[]; 229 int pos; 230 int len; 231 int end; 232 233 public IntermediateInputStream() { 234 } 235 236 public final void close() throws IOException { 237 throw new IOException ("close() called - shouldn't happen "); 239 } 240 241 public final int read(byte cbuf[], int off, int len) throws IOException { 242 if( pos >= end ) return -1; 243 if (pos + len > end) { 244 len = end - pos; 245 } 246 if (len <= 0) { 247 return 0; 248 } 249 System.arraycopy(buf, pos, cbuf, off, len); 250 pos += len; 251 return len; 252 } 253 254 public final int read() throws IOException { 255 return (pos < end ) ? (buf[pos++] & 0xff) : -1; 256 } 257 258 260 void setBuffer( byte b[], int p, int l ) { 261 buf=b; 262 pos=p; 263 len=l; 264 end=pos+len; 265 } 266 267 void setByteChunk( ByteChunk mb ) { 268 buf=mb.getBytes(); 269 pos=mb.getStart(); 270 len=mb.getLength(); 271 end=pos+len; 272 } 273 274 } 275 | Popular Tags |