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