1 57 package com.sun.org.apache.xml.internal.serializer; 58 59 import java.io.IOException ; 60 import java.io.OutputStream ; 61 import java.io.UnsupportedEncodingException ; 62 import java.io.Writer ; 63 64 70 public class WriterToUTF8 extends Writer 71 { 72 73 74 private static final boolean DEBUG_OUT = false; 75 76 77 private final OutputStream m_os; 78 79 87 public WriterToUTF8(OutputStream os) throws UnsupportedEncodingException 88 { 89 m_os = os; 90 } 91 92 103 public void write(final int c) throws IOException 104 { 105 106 if (c < 0x80) 107 m_os.write(c); 108 else if (c < 0x800) 109 { 110 m_os.write(0xc0 + (c >> 6)); 111 m_os.write(0x80 + (c & 0x3f)); 112 } 113 else 114 { 115 m_os.write(0xe0 + (c >> 12)); 116 m_os.write(0x80 + ((c >> 6) & 0x3f)); 117 m_os.write(0x80 + (c & 0x3f)); 118 } 119 120 if (DEBUG_OUT) 121 { 122 if (c < 0x80) 123 { 124 char ch = (char) c; 125 System.out.print(ch); 126 } 127 else if (c < 0x800) 128 { 129 System.out.print(0xc0 + (c >> 6)); 130 System.out.print(0x80 + (c & 0x3f)); 131 } 132 else 133 { 134 System.out.print(0xe0 + (c >> 12)); 135 System.out.print(0x80 + ((c >> 6) & 0x3f)); 136 System.out.print(0x80 + (c & 0x3f)); 137 } 138 System.out.flush(); 139 } 140 return; 141 } 142 143 154 public void write(final char chars[], final int start, final int length) 155 throws java.io.IOException 156 { 157 158 final OutputStream os = m_os; 159 160 int n = length + start; 161 for (int i = start; i < n; i++) 162 { 163 final char c = chars[i]; 164 165 if (c < 0x80) 166 os.write(c); 167 else if (c < 0x800) 168 { 169 os.write(0xc0 + (c >> 6)); 170 os.write(0x80 + (c & 0x3f)); 171 } 172 else 173 { 174 os.write(0xe0 + (c >> 12)); 175 os.write(0x80 + ((c >> 6) & 0x3f)); 176 os.write(0x80 + (c & 0x3f)); 177 } 178 } 179 180 if (DEBUG_OUT) 181 { 182 for (int i = start; i < n; i++) 183 { 184 final char c = chars[i]; 185 186 if (c < 0x80) 187 System.out.print(c); 188 else if (c < 0x800) 189 { 190 System.out.print(0xc0 + (c >> 6)); 191 System.out.print(0x80 + (c & 0x3f)); 192 } 193 else 194 { 195 System.out.print(0xe0 + (c >> 12)); 196 System.out.print(0x80 + ((c >> 6) & 0x3f)); 197 System.out.print(0x80 + (c & 0x3f)); 198 } 199 } 200 System.out.flush(); 201 } 202 return; 203 } 204 205 212 public void write(final String s) throws IOException 213 { 214 215 final int n = s.length(); 216 final OutputStream os = m_os; 217 218 for (int i = 0; i < n; i++) 219 { 220 final char c = s.charAt(i); 221 222 if (c < 0x80) 223 os.write(c); 224 else if (c < 0x800) 225 { 226 os.write(0xc0 + (c >> 6)); 227 os.write(0x80 + (c & 0x3f)); 228 } 229 else 230 { 231 os.write(0xe0 + (c >> 12)); 232 os.write(0x80 + ((c >> 6) & 0x3f)); 233 os.write(0x80 + (c & 0x3f)); 234 } 235 } 236 237 if (DEBUG_OUT) 238 { 239 240 for (int i = 0; i < n; i++) 241 { 242 final char c = s.charAt(i); 243 244 if (c < 0x80) 245 System.out.print(c); 246 else if (c < 0x800) 247 { 248 System.out.print(0xc0 + (c >> 6)); 249 System.out.print(0x80 + (c & 0x3f)); 250 } 251 else 252 { 253 System.out.print(0xe0 + (c >> 12)); 254 System.out.print(0x80 + ((c >> 6) & 0x3f)); 255 System.out.print(0x80 + (c & 0x3f)); 256 } 257 } 258 System.out.flush(); 259 } 260 return; 261 } 262 263 274 public void flush() throws java.io.IOException 275 { 276 m_os.flush(); 277 } 278 279 288 public void close() throws java.io.IOException 289 { 290 m_os.close(); 291 } 292 293 299 public OutputStream getOutputStream() 300 { 301 return m_os; 302 } 303 } 304 | Popular Tags |