001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: WriterToASCI.java 468654 2006-10-28 07:09:23Z minchau $
020     */
021    package org.apache.xml.serializer;
022    
023    import java.io.IOException;
024    import java.io.OutputStream;
025    import java.io.Writer;
026    
027    
028    
029    /**
030     * This class writes ASCII to a byte stream as quickly as possible.  For the
031     * moment it does not do buffering, though I reserve the right to do some
032     * buffering down the line if I can prove that it will be faster even if the
033     * output stream is buffered.
034     * 
035     * This class is only used internally within Xalan.
036     * 
037     * @xsl.usage internal
038     */
039    class WriterToASCI extends Writer implements WriterChain
040    {
041    
042      /** The byte stream to write to.  */
043      private final OutputStream m_os;
044    
045      /**
046       * Create an unbuffered ASCII writer.
047       *
048       *
049       * @param os The byte stream to write to.
050       */
051      public WriterToASCI(OutputStream os)
052      {
053        m_os = os;
054      }
055    
056      /**
057       * Write a portion of an array of characters.
058       *
059       * @param  chars  Array of characters
060       * @param  start   Offset from which to start writing characters
061       * @param  length   Number of characters to write
062       *
063       * @exception  IOException  If an I/O error occurs
064       *
065       * @throws java.io.IOException
066       */
067      public void write(char chars[], int start, int length)
068              throws java.io.IOException
069      {
070    
071        int n = length+start;
072    
073        for (int i = start; i < n; i++)
074        {
075          m_os.write(chars[i]);
076        }
077      }
078    
079      /**
080       * Write a single character.  The character to be written is contained in
081       * the 16 low-order bits of the given integer value; the 16 high-order bits
082       * are ignored.
083       *
084       * <p> Subclasses that intend to support efficient single-character output
085       * should override this method.
086       *
087       * @param c  int specifying a character to be written.
088       * @exception  IOException  If an I/O error occurs
089       */
090      public void write(int c) throws IOException
091      {
092        m_os.write(c);
093      }
094    
095      /**
096       * Write a string.
097       *
098       * @param  s String to be written
099       *
100       * @exception  IOException  If an I/O error occurs
101       */
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       */
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       */
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       */
143      public OutputStream getOutputStream()
144      {
145        return m_os;
146      }
147    
148      /**
149       * Get the writer that this writer directly chains to.
150       */
151      public Writer getWriter()
152      {
153          return null;
154      }
155    }