KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > serializer > WriterToUTF8


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xalan" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, Lotus
53  * Development Corporation., http://www.lotus.com. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57 package com.sun.org.apache.xml.internal.serializer;
58
59 import java.io.IOException JavaDoc;
60 import java.io.OutputStream JavaDoc;
61 import java.io.UnsupportedEncodingException JavaDoc;
62 import java.io.Writer JavaDoc;
63
64 /**
65  * This class writes ASCII to a byte stream as quickly as possible. For the
66  * moment it does not do buffering, though I reserve the right to do some
67  * buffering down the line if I can prove that it will be faster even if the
68  * output stream is buffered.
69  */

70 public class WriterToUTF8 extends Writer JavaDoc
71 {
72
73     /** A flag to force flush output to System.out when stepping with a debugger (debug only!) */
74     private static final boolean DEBUG_OUT = false;
75
76     /** The byte stream to write to. */
77     private final OutputStream JavaDoc m_os;
78
79     /**
80      * Create an unbuffered UTF-8 writer.
81      *
82      *
83      * @param os The byte stream to write to.
84      *
85      * @throws UnsupportedEncodingException
86      */

87     public WriterToUTF8(OutputStream JavaDoc os) throws UnsupportedEncodingException JavaDoc
88     {
89         m_os = os;
90     }
91
92     /**
93      * Write a single character. The character to be written is contained in
94      * the 16 low-order bits of the given integer value; the 16 high-order bits
95      * are ignored.
96      *
97      * <p> Subclasses that intend to support efficient single-character output
98      * should override this method.
99      *
100      * @param c int specifying a character to be written.
101      * @exception IOException If an I/O error occurs
102      */

103     public void write(final int c) throws IOException JavaDoc
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     /**
144      * Write a portion of an array of characters.
145      *
146      * @param chars Array of characters
147      * @param start Offset from which to start writing characters
148      * @param length Number of characters to write
149      *
150      * @exception IOException If an I/O error occurs
151      *
152      * @throws java.io.IOException
153      */

154     public void write(final char chars[], final int start, final int length)
155         throws java.io.IOException JavaDoc
156     {
157
158         final OutputStream JavaDoc 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     /**
206      * Write a string.
207      *
208      * @param s String to be written
209      *
210      * @exception IOException If an I/O error occurs
211      */

212     public void write(final String JavaDoc s) throws IOException JavaDoc
213     {
214
215         final int n = s.length();
216         final OutputStream JavaDoc 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     /**
264      * Flush the stream. If the stream has saved any characters from the
265      * various write() methods in a buffer, write them immediately to their
266      * intended destination. Then, if that destination is another character or
267      * byte stream, flush it. Thus one flush() invocation will flush all the
268      * buffers in a chain of Writers and OutputStreams.
269      *
270      * @exception IOException If an I/O error occurs
271      *
272      * @throws java.io.IOException
273      */

274     public void flush() throws java.io.IOException JavaDoc
275     {
276         m_os.flush();
277     }
278
279     /**
280      * Close the stream, flushing it first. Once a stream has been closed,
281      * further write() or flush() invocations will cause an IOException to be
282      * thrown. Closing a previously-closed stream, however, has no effect.
283      *
284      * @exception IOException If an I/O error occurs
285      *
286      * @throws java.io.IOException
287      */

288     public void close() throws java.io.IOException JavaDoc
289     {
290         m_os.close();
291     }
292
293     /**
294      * Get the output stream where the events will be serialized to.
295      *
296      * @return reference to the result stream, or null of only a writer was
297      * set.
298      */

299     public OutputStream JavaDoc getOutputStream()
300     {
301         return m_os;
302     }
303 }
304
Popular Tags