KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > util > UnicodeWriter


1 package polyglot.util;
2
3 import java.io.*;
4
5 /**
6  * Output stream for writing unicode. Non-ASCII Unicode characters
7  * are escaped.
8  */

9 public class UnicodeWriter extends FilterWriter
10 {
11   public UnicodeWriter(Writer out)
12   {
13     super(out);
14   }
15
16   public void write(int c) throws IOException
17   {
18     if( c <= 0xFF) {
19       super.write(c);
20     }
21     else {
22       String JavaDoc s = String.valueOf(Integer.toHexString(c));
23       super.write('\\');
24       super.write('u');
25       for(int i = s.length(); i < 4; i++) {
26         super.write('0');
27       }
28       write(s);
29     }
30   }
31   
32   public void write(char[] cbuf, int off, int len) throws IOException
33   {
34     for( int i = 0; i < len; i++)
35     {
36       write((int)cbuf[i+off]);
37     }
38   }
39
40   public void write(String JavaDoc str, int off, int len) throws IOException
41   {
42     write(str.toCharArray(), off, len);
43   }
44 }
45
Popular Tags