1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.IOException ; 22 import java.io.Writer ; 23 24 41 public class SerializerTraceWriter extends Writer 42 { 43 44 48 private final java.io.Writer m_writer; 49 50 51 private final SerializerTrace m_tracer; 52 53 56 private int buf_length; 57 58 62 private byte buf[]; 63 64 68 private int count; 69 70 75 private void setBufferSize(int size) 76 { 77 buf = new byte[size + 3]; 78 buf_length = size; 79 count = 0; 80 } 81 82 93 public SerializerTraceWriter(Writer out, SerializerTrace tracer) 94 { 95 m_writer = out; 96 m_tracer = tracer; 97 setBufferSize(1024); 98 } 99 100 108 private void flushBuffer() throws IOException 109 { 110 111 if (count > 0) 113 { 114 char[] chars = new char[count]; 115 for(int i=0; i<count; i++) 116 chars[i] = (char) buf[i]; 117 118 if (m_tracer != null) 119 m_tracer.fireGenerateEvent( 120 SerializerTrace.EVENTTYPE_OUTPUT_CHARACTERS, 121 chars, 122 0, 123 chars.length); 124 125 count = 0; 126 } 127 } 128 129 133 public void flush() throws java.io.IOException 134 { 135 if (m_writer != null) 137 m_writer.flush(); 138 139 flushBuffer(); 141 } 142 143 147 public void close() throws java.io.IOException 148 { 149 if (m_writer != null) 151 m_writer.close(); 152 153 flushBuffer(); 155 } 156 157 158 169 public void write(final int c) throws IOException 170 { 171 if (m_writer != null) 173 m_writer.write(c); 174 175 177 180 if (count >= buf_length) 181 flushBuffer(); 182 183 if (c < 0x80) 184 { 185 buf[count++] = (byte) (c); 186 } 187 else if (c < 0x800) 188 { 189 buf[count++] = (byte) (0xc0 + (c >> 6)); 190 buf[count++] = (byte) (0x80 + (c & 0x3f)); 191 } 192 else 193 { 194 buf[count++] = (byte) (0xe0 + (c >> 12)); 195 buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 196 buf[count++] = (byte) (0x80 + (c & 0x3f)); 197 } 198 } 199 200 211 public void write(final char chars[], final int start, final int length) 212 throws java.io.IOException 213 { 214 if (m_writer != null) 216 m_writer.write(chars, start, length); 217 218 int lengthx3 = (length << 1) + length; 220 221 if (lengthx3 >= buf_length) 222 { 223 224 227 228 flushBuffer(); 229 setBufferSize(2 * lengthx3); 230 231 } 232 233 if (lengthx3 > buf_length - count) 234 { 235 flushBuffer(); 236 } 237 238 final int n = length + start; 239 for (int i = start; i < n; i++) 240 { 241 final char c = chars[i]; 242 243 if (c < 0x80) 244 buf[count++] = (byte) (c); 245 else if (c < 0x800) 246 { 247 buf[count++] = (byte) (0xc0 + (c >> 6)); 248 buf[count++] = (byte) (0x80 + (c & 0x3f)); 249 } 250 else 251 { 252 buf[count++] = (byte) (0xe0 + (c >> 12)); 253 buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 254 buf[count++] = (byte) (0x80 + (c & 0x3f)); 255 } 256 } 257 258 } 259 260 267 public void write(final String s) throws IOException 268 { 269 if (m_writer != null) 271 m_writer.write(s); 272 273 final int length = s.length(); 275 276 280 int lengthx3 = (length << 1) + length; 281 282 if (lengthx3 >= buf_length) 283 { 284 285 288 289 flushBuffer(); 290 setBufferSize(2 * lengthx3); 291 } 292 293 if (lengthx3 > buf_length - count) 294 { 295 flushBuffer(); 296 } 297 298 for (int i = 0; i < length; i++) 299 { 300 final char c = s.charAt(i); 301 302 if (c < 0x80) 303 buf[count++] = (byte) (c); 304 else if (c < 0x800) 305 { 306 buf[count++] = (byte) (0xc0 + (c >> 6)); 307 buf[count++] = (byte) (0x80 + (c & 0x3f)); 308 } 309 else 310 { 311 buf[count++] = (byte) (0xe0 + (c >> 12)); 312 buf[count++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 313 buf[count++] = (byte) (0x80 + (c & 0x3f)); 314 } 315 } 316 } 317 318 } 319 | Popular Tags |