KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: WriterToASCI.java,v 1.2 2004/02/17 04:18:18 minchau Exp $
18  */

19 package com.sun.org.apache.xml.internal.serializer;
20
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import java.io.Writer JavaDoc;
24
25
26
27 /**
28  * This class writes ASCII to a byte stream as quickly as possible. For the
29  * moment it does not do buffering, though I reserve the right to do some
30  * buffering down the line if I can prove that it will be faster even if the
31  * output stream is buffered.
32  */

33 public class WriterToASCI extends Writer JavaDoc
34 {
35
36   /** The byte stream to write to. */
37   private final OutputStream JavaDoc m_os;
38
39   /**
40    * Create an unbuffered ASCII writer.
41    *
42    *
43    * @param os The byte stream to write to.
44    */

45   public WriterToASCI(OutputStream JavaDoc os)
46   {
47     m_os = os;
48   }
49
50   /**
51    * Write a portion of an array of characters.
52    *
53    * @param chars Array of characters
54    * @param start Offset from which to start writing characters
55    * @param length Number of characters to write
56    *
57    * @exception IOException If an I/O error occurs
58    *
59    * @throws java.io.IOException
60    */

61   public void write(char chars[], int start, int length)
62           throws java.io.IOException JavaDoc
63   {
64
65     int n = length+start;
66
67     for (int i = start; i < n; i++)
68     {
69       m_os.write(chars[i]);
70     }
71   }
72
73   /**
74    * Write a single character. The character to be written is contained in
75    * the 16 low-order bits of the given integer value; the 16 high-order bits
76    * are ignored.
77    *
78    * <p> Subclasses that intend to support efficient single-character output
79    * should override this method.
80    *
81    * @param c int specifying a character to be written.
82    * @exception IOException If an I/O error occurs
83    */

84   public void write(int c) throws IOException JavaDoc
85   {
86     m_os.write(c);
87   }
88
89   /**
90    * Write a string.
91    *
92    * @param str String to be written
93    *
94    * @exception IOException If an I/O error occurs
95    */

96   public void write(String JavaDoc s) throws IOException JavaDoc
97   {
98     int n = s.length();
99     for (int i = 0; i < n; i++)
100     {
101       m_os.write(s.charAt(i));
102     }
103   }
104
105   /**
106    * Flush the stream. If the stream has saved any characters from the
107    * various write() methods in a buffer, write them immediately to their
108    * intended destination. Then, if that destination is another character or
109    * byte stream, flush it. Thus one flush() invocation will flush all the
110    * buffers in a chain of Writers and OutputStreams.
111    *
112    * @exception IOException If an I/O error occurs
113    */

114   public void flush() throws java.io.IOException JavaDoc
115   {
116     m_os.flush();
117   }
118
119   /**
120    * Close the stream, flushing it first. Once a stream has been closed,
121    * further write() or flush() invocations will cause an IOException to be
122    * thrown. Closing a previously-closed stream, however, has no effect.
123    *
124    * @exception IOException If an I/O error occurs
125    */

126   public void close() throws java.io.IOException JavaDoc
127   {
128     m_os.close();
129   }
130
131   /**
132    * Get the output stream where the events will be serialized to.
133    *
134    * @return reference to the result stream, or null of only a writer was
135    * set.
136    */

137   public OutputStream JavaDoc getOutputStream()
138   {
139     return m_os;
140   }
141
142 }
143
Popular Tags