• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id: WriterToASCI.java 468654 2006-10-28 07:09:23Z minchau $
20  */
21 package org.apache.xml.serializer;
22 
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.Writer;
26 
27 
28 
29 /**
30  * This class writes ASCII to a byte stream as quickly as possible.  For the
31  * moment it does not do buffering, though I reserve the right to do some
32  * buffering down the line if I can prove that it will be faster even if the
33  * output stream is buffered.
34  *
35  * This class is only used internally within Xalan.
36  *
37  * @xsl.usage internal
38  */
39 class WriterToASCI extends Writer implements WriterChain
40 {
41 
42   /** The byte stream to write to.  */
43   private final OutputStream m_os;
44 
45   /**
46    * Create an unbuffered ASCII writer.
47    *
48    *
49    * @param os The byte stream to write to.
50    */
WriterToASCI(OutputStream os)51   public WriterToASCI(OutputStream os)
52   {
53     m_os = os;
54   }
55 
56   /**
57    * Write a portion of an array of characters.
58    *
59    * @param  chars  Array of characters
60    * @param  start   Offset from which to start writing characters
61    * @param  length   Number of characters to write
62    *
63    * @exception  IOException  If an I/O error occurs
64    *
65    * @throws java.io.IOException
66    */
write(char chars[], int start, int length)67   public void write(char chars[], int start, int length)
68           throws java.io.IOException
69   {
70 
71     int n = length+start;
72 
73     for (int i = start; i < n; i++)
74     {
75       m_os.write(chars[i]);
76     }
77   }
78 
79   /**
80    * Write a single character.  The character to be written is contained in
81    * the 16 low-order bits of the given integer value; the 16 high-order bits
82    * are ignored.
83    *
84    * <p> Subclasses that intend to support efficient single-character output
85    * should override this method.
86    *
87    * @param c  int specifying a character to be written.
88    * @exception  IOException  If an I/O error occurs
89    */
write(int c)90   public void write(int c) throws IOException
91   {
92     m_os.write(c);
93   }
94 
95   /**
96    * Write a string.
97    *
98    * @param  s String to be written
99    *
100    * @exception  IOException  If an I/O error occurs
101    */
write(String s)102   public void write(String s) throws IOException
103   {
104     int n = s.length();
105     for (int i = 0; i < n; i++)
106     {
107       m_os.write(s.charAt(i));
108     }
109   }
110 
111   /**
112    * Flush the stream.  If the stream has saved any characters from the
113    * various write() methods in a buffer, write them immediately to their
114    * intended destination.  Then, if that destination is another character or
115    * byte stream, flush it.  Thus one flush() invocation will flush all the
116    * buffers in a chain of Writers and OutputStreams.
117    *
118    * @exception  IOException  If an I/O error occurs
119    */
flush()120   public void flush() throws java.io.IOException
121   {
122     m_os.flush();
123   }
124 
125   /**
126    * Close the stream, flushing it first.  Once a stream has been closed,
127    * further write() or flush() invocations will cause an IOException to be
128    * thrown.  Closing a previously-closed stream, however, has no effect.
129    *
130    * @exception  IOException  If an I/O error occurs
131    */
close()132   public void close() throws java.io.IOException
133   {
134     m_os.close();
135   }
136 
137   /**
138    * Get the output stream where the events will be serialized to.
139    *
140    * @return reference to the result stream, or null of only a writer was
141    * set.
142    */
getOutputStream()143   public OutputStream getOutputStream()
144   {
145     return m_os;
146   }
147 
148   /**
149    * Get the writer that this writer directly chains to.
150    */
getWriter()151   public Writer getWriter()
152   {
153       return null;
154   }
155 }
156