1 16 19 package com.sun.org.apache.xml.internal.serializer; 20 21 import java.io.IOException ; 22 import java.io.OutputStream ; 23 import java.io.UnsupportedEncodingException ; 24 import java.io.Writer ; 25 26 27 33 public final class WriterToUTF8Buffered extends Writer 34 { 35 36 39 private static final int BYTES_MAX=16*1024; 40 41 45 private static final int CHARS_MAX=(BYTES_MAX/3); 46 47 49 50 private final OutputStream m_os; 51 52 56 private final byte m_outputBytes[]; 57 58 private final char m_inputChars[]; 59 60 66 private int count; 67 68 76 public WriterToUTF8Buffered(OutputStream out) 77 throws UnsupportedEncodingException 78 { 79 m_os = out; 80 m_outputBytes = new byte[BYTES_MAX + 3]; 83 84 m_inputChars = new char[CHARS_MAX + 1]; 87 count = 0; 88 89 } 92 93 102 117 128 public void write(final int c) throws IOException 129 { 130 131 134 if (count >= BYTES_MAX) 135 flushBuffer(); 136 137 if (c < 0x80) 138 { 139 m_outputBytes[count++] = (byte) (c); 140 } 141 else if (c < 0x800) 142 { 143 m_outputBytes[count++] = (byte) (0xc0 + (c >> 6)); 144 m_outputBytes[count++] = (byte) (0x80 + (c & 0x3f)); 145 } 146 else 147 { 148 m_outputBytes[count++] = (byte) (0xe0 + (c >> 12)); 149 m_outputBytes[count++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 150 m_outputBytes[count++] = (byte) (0x80 + (c & 0x3f)); 151 } 152 } 153 154 155 166 public void write(final char chars[], final int start, final int length) 167 throws java.io.IOException 168 { 169 170 174 int lengthx3 = 3*length; 175 176 if (lengthx3 >= BYTES_MAX - count) 177 { 178 flushBuffer(); 180 181 if (lengthx3 >= BYTES_MAX) 182 { 183 190 final int chunks = 1 + length/CHARS_MAX; 191 int end_chunk = start; 192 for (int chunk = 1; chunk <= chunks; chunk++) 193 { 194 int start_chunk = end_chunk; 195 end_chunk = start + (int) ((((long) length) * chunk) / chunks); 196 int len_chunk = (end_chunk - start_chunk); 197 this.write(chars,start_chunk, len_chunk); 198 } 199 return; 200 } 201 } 202 203 204 205 final int n = length+start; 206 final byte[] buf_loc = m_outputBytes; int count_loc = count; int i = start; 209 { 210 215 char c; 216 for(; i < n && (c = chars[i])< 0x80 ; i++ ) 217 buf_loc[count_loc++] = (byte)c; 218 } 219 for (; i < n; i++) 220 { 221 222 final char c = chars[i]; 223 224 if (c < 0x80) 225 buf_loc[count_loc++] = (byte) (c); 226 else if (c < 0x800) 227 { 228 buf_loc[count_loc++] = (byte) (0xc0 + (c >> 6)); 229 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 230 } 231 else 232 { 233 buf_loc[count_loc++] = (byte) (0xe0 + (c >> 12)); 234 buf_loc[count_loc++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 235 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 236 } 237 } 238 count = count_loc; 240 241 } 242 243 250 private void directWrite(final char chars[], final int start, final int length) 251 throws java.io.IOException 252 { 253 254 255 256 if (length >= BYTES_MAX - count) 257 { 258 flushBuffer(); 260 261 if (length >= BYTES_MAX) 262 { 263 269 int chunks = 1 + length/CHARS_MAX; 270 for (int chunk =0 ; chunk < chunks; chunk++) 271 { 272 int start_chunk = start + ((length*chunk)/chunks); 273 int end_chunk = start + ((length*(chunk+1))/chunks); 274 int len_chunk = (end_chunk - start_chunk); 275 this.directWrite(chars,start_chunk, len_chunk); 276 } 277 return; 278 } 279 } 280 281 final int n = length+start; 282 final byte[] buf_loc = m_outputBytes; int count_loc = count; for(int i=start; i < n ; i++ ) 285 buf_loc[count_loc++] = (byte) buf_loc[i]; 286 count = count_loc; 288 } 289 290 297 public void write(final String s) throws IOException 298 { 299 300 final int length = s.length(); 304 int lengthx3 = 3*length; 305 306 if (lengthx3 >= BYTES_MAX - count) 307 { 308 flushBuffer(); 310 311 if (lengthx3 >= BYTES_MAX) 312 { 313 317 final int start = 0; 318 int chunks = 1 + length/CHARS_MAX; 319 for (int chunk =0 ; chunk < chunks; chunk++) 320 { 321 int start_chunk = start + ((length*chunk)/chunks); 322 int end_chunk = start + ((length*(chunk+1))/chunks); 323 int len_chunk = (end_chunk - start_chunk); 324 s.getChars(start_chunk,end_chunk, m_inputChars,0); 325 this.write(m_inputChars,0, len_chunk); 326 } 327 return; 328 } 329 } 330 331 332 s.getChars(0, length , m_inputChars, 0); 333 final char[] chars = m_inputChars; 334 final int n = length; 335 final byte[] buf_loc = m_outputBytes; int count_loc = count; int i = 0; 338 { 339 344 char c; 345 for(; i < n && (c = chars[i])< 0x80 ; i++ ) 346 buf_loc[count_loc++] = (byte)c; 347 } 348 for (; i < n; i++) 349 { 350 351 final char c = chars[i]; 352 353 if (c < 0x80) 354 buf_loc[count_loc++] = (byte) (c); 355 else if (c < 0x800) 356 { 357 buf_loc[count_loc++] = (byte) (0xc0 + (c >> 6)); 358 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 359 } 360 else 361 { 362 buf_loc[count_loc++] = (byte) (0xe0 + (c >> 12)); 363 buf_loc[count_loc++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 364 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 365 } 366 } 367 count = count_loc; 369 370 } 371 372 377 public void flushBuffer() throws IOException 378 { 379 380 if (count > 0) 381 { 382 m_os.write(m_outputBytes, 0, count); 383 384 count = 0; 385 } 386 } 387 388 399 public void flush() throws java.io.IOException 400 { 401 flushBuffer(); 402 m_os.flush(); 403 } 404 405 414 public void close() throws java.io.IOException 415 { 416 flushBuffer(); 417 m_os.close(); 418 } 419 420 426 public OutputStream getOutputStream() 427 { 428 return m_os; 429 } 430 431 436 public void directWrite(final String s) throws IOException 437 { 438 439 final int length = s.length(); 440 441 if (length >= BYTES_MAX - count) 442 { 443 flushBuffer(); 445 446 if (length >= BYTES_MAX) 447 { 448 454 final int start = 0; 455 int chunks = 1 + length/CHARS_MAX; 456 for (int chunk =0 ; chunk < chunks; chunk++) 457 { 458 int start_chunk = start + ((length*chunk)/chunks); 459 int end_chunk = start + ((length*(chunk+1))/chunks); 460 int len_chunk = (end_chunk - start_chunk); 461 s.getChars(start_chunk,end_chunk, m_inputChars,0); 462 this.directWrite(m_inputChars,0, len_chunk); 463 } 464 return; 465 } 466 } 467 468 469 s.getChars(0, length , m_inputChars, 0); 470 final char[] chars = m_inputChars; 471 final byte[] buf_loc = m_outputBytes; int count_loc = count; int i = 0; 474 while( i < length) 475 buf_loc[count_loc++] = (byte)chars[i++]; 476 477 478 count = count_loc; 480 481 } 482 } 483 | Popular Tags |