1 16 19 package org.apache.xml.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 189 final int chunks = 1 + length/CHARS_MAX; 190 for (int chunk =0 ; chunk < chunks; chunk++) 191 { 192 int start_chunk = start + ((length*chunk)/chunks); 193 int end_chunk = start + ((length*(chunk+1))/chunks); 194 int len_chunk = (end_chunk - start_chunk); 195 this.write(chars,start_chunk, len_chunk); 196 } 197 return; 198 } 199 } 200 201 202 203 final int n = length+start; 204 final byte[] buf_loc = m_outputBytes; int count_loc = count; int i = start; 207 { 208 213 char c; 214 for(; i < n && (c = chars[i])< 0x80 ; i++ ) 215 buf_loc[count_loc++] = (byte)c; 216 } 217 for (; i < n; i++) 218 { 219 220 final char c = chars[i]; 221 222 if (c < 0x80) 223 buf_loc[count_loc++] = (byte) (c); 224 else if (c < 0x800) 225 { 226 buf_loc[count_loc++] = (byte) (0xc0 + (c >> 6)); 227 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 228 } 229 else 230 { 231 buf_loc[count_loc++] = (byte) (0xe0 + (c >> 12)); 232 buf_loc[count_loc++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 233 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 234 } 235 } 236 count = count_loc; 238 239 } 240 241 248 private void directWrite(final char chars[], final int start, final int length) 249 throws java.io.IOException 250 { 251 252 253 254 if (length >= BYTES_MAX - count) 255 { 256 flushBuffer(); 258 259 if (length >= BYTES_MAX) 260 { 261 267 int chunks = 1 + length/CHARS_MAX; 268 for (int chunk =0 ; chunk < chunks; chunk++) 269 { 270 int start_chunk = start + ((length*chunk)/chunks); 271 int end_chunk = start + ((length*(chunk+1))/chunks); 272 int len_chunk = (end_chunk - start_chunk); 273 this.directWrite(chars,start_chunk, len_chunk); 274 } 275 return; 276 } 277 } 278 279 final int n = length+start; 280 final byte[] buf_loc = m_outputBytes; int count_loc = count; for(int i=start; i < n ; i++ ) 283 buf_loc[count_loc++] = (byte) buf_loc[i]; 284 count = count_loc; 286 } 287 288 295 public void write(final String s) throws IOException 296 { 297 298 final int length = s.length(); 302 int lengthx3 = 3*length; 303 304 if (lengthx3 >= BYTES_MAX - count) 305 { 306 flushBuffer(); 308 309 if (lengthx3 >= BYTES_MAX) 310 { 311 315 final int start = 0; 316 int chunks = 1 + length/CHARS_MAX; 317 for (int chunk =0 ; chunk < chunks; chunk++) 318 { 319 int start_chunk = start + ((length*chunk)/chunks); 320 int end_chunk = start + ((length*(chunk+1))/chunks); 321 int len_chunk = (end_chunk - start_chunk); 322 s.getChars(start_chunk,end_chunk, m_inputChars,0); 323 this.write(m_inputChars,0, len_chunk); 324 } 325 return; 326 } 327 } 328 329 330 s.getChars(0, length , m_inputChars, 0); 331 final char[] chars = m_inputChars; 332 final int n = length; 333 final byte[] buf_loc = m_outputBytes; int count_loc = count; int i = 0; 336 { 337 342 char c; 343 for(; i < n && (c = chars[i])< 0x80 ; i++ ) 344 buf_loc[count_loc++] = (byte)c; 345 } 346 for (; i < n; i++) 347 { 348 349 final char c = chars[i]; 350 351 if (c < 0x80) 352 buf_loc[count_loc++] = (byte) (c); 353 else if (c < 0x800) 354 { 355 buf_loc[count_loc++] = (byte) (0xc0 + (c >> 6)); 356 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 357 } 358 else 359 { 360 buf_loc[count_loc++] = (byte) (0xe0 + (c >> 12)); 361 buf_loc[count_loc++] = (byte) (0x80 + ((c >> 6) & 0x3f)); 362 buf_loc[count_loc++] = (byte) (0x80 + (c & 0x3f)); 363 } 364 } 365 count = count_loc; 367 368 } 369 370 375 public void flushBuffer() throws IOException 376 { 377 378 if (count > 0) 379 { 380 m_os.write(m_outputBytes, 0, count); 381 382 count = 0; 383 } 384 } 385 386 397 public void flush() throws java.io.IOException 398 { 399 flushBuffer(); 400 m_os.flush(); 401 } 402 403 412 public void close() throws java.io.IOException 413 { 414 flushBuffer(); 415 m_os.close(); 416 } 417 418 424 public OutputStream getOutputStream() 425 { 426 return m_os; 427 } 428 429 434 public void directWrite(final String s) throws IOException 435 { 436 437 final int length = s.length(); 438 439 if (length >= BYTES_MAX - count) 440 { 441 flushBuffer(); 443 444 if (length >= BYTES_MAX) 445 { 446 452 final int start = 0; 453 int chunks = 1 + length/CHARS_MAX; 454 for (int chunk =0 ; chunk < chunks; chunk++) 455 { 456 int start_chunk = start + ((length*chunk)/chunks); 457 int end_chunk = start + ((length*(chunk+1))/chunks); 458 int len_chunk = (end_chunk - start_chunk); 459 s.getChars(start_chunk,end_chunk, m_inputChars,0); 460 this.directWrite(m_inputChars,0, len_chunk); 461 } 462 return; 463 } 464 } 465 466 467 s.getChars(0, length , m_inputChars, 0); 468 final char[] chars = m_inputChars; 469 final byte[] buf_loc = m_outputBytes; int count_loc = count; int i = 0; 472 while( i < length) 473 buf_loc[count_loc++] = (byte)chars[i++]; 474 475 476 count = count_loc; 478 479 } 480 } 481 | Popular Tags |