1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 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 org.apache.xalan.serialize;
  58. import java.io.*;
  59. /**
  60. * This class writes ASCII to a byte stream as quickly as possible. For the
  61. * moment it does not do buffering, though I reserve the right to do some
  62. * buffering down the line if I can prove that it will be faster even if the
  63. * output stream is buffered.
  64. */
  65. public class WriterToASCI extends Writer
  66. {
  67. /** The byte stream to write to. */
  68. private final OutputStream m_os;
  69. /**
  70. * Create an unbuffered ASCII writer.
  71. *
  72. *
  73. * @param os The byte stream to write to.
  74. */
  75. public WriterToASCI(OutputStream os)
  76. {
  77. m_os = os;
  78. }
  79. /**
  80. * Write a portion of an array of characters.
  81. *
  82. * @param chars Array of characters
  83. * @param start Offset from which to start writing characters
  84. * @param length Number of characters to write
  85. *
  86. * @exception IOException If an I/O error occurs
  87. *
  88. * @throws java.io.IOException
  89. */
  90. public void write(char chars[], int start, int length)
  91. throws java.io.IOException
  92. {
  93. int n = length+start;
  94. for (int i = start; i < n; i++)
  95. {
  96. m_os.write(chars[i]);
  97. }
  98. }
  99. /**
  100. * Write a single character. The character to be written is contained in
  101. * the 16 low-order bits of the given integer value; the 16 high-order bits
  102. * are ignored.
  103. *
  104. * <p> Subclasses that intend to support efficient single-character output
  105. * should override this method.
  106. *
  107. * @param c int specifying a character to be written.
  108. * @exception IOException If an I/O error occurs
  109. */
  110. public void write(int c) throws IOException
  111. {
  112. m_os.write(c);
  113. }
  114. /**
  115. * Write a string.
  116. *
  117. * @param str String to be written
  118. *
  119. * @exception IOException If an I/O error occurs
  120. */
  121. public void write(String s) throws IOException
  122. {
  123. int n = s.length();
  124. for (int i = 0; i < n; i++)
  125. {
  126. m_os.write(s.charAt(i));
  127. }
  128. }
  129. /**
  130. * Flush the stream. If the stream has saved any characters from the
  131. * various write() methods in a buffer, write them immediately to their
  132. * intended destination. Then, if that destination is another character or
  133. * byte stream, flush it. Thus one flush() invocation will flush all the
  134. * buffers in a chain of Writers and OutputStreams.
  135. *
  136. * @exception IOException If an I/O error occurs
  137. */
  138. public void flush() throws java.io.IOException
  139. {
  140. m_os.flush();
  141. }
  142. /**
  143. * Close the stream, flushing it first. Once a stream has been closed,
  144. * further write() or flush() invocations will cause an IOException to be
  145. * thrown. Closing a previously-closed stream, however, has no effect.
  146. *
  147. * @exception IOException If an I/O error occurs
  148. */
  149. public void close() throws java.io.IOException
  150. {
  151. m_os.close();
  152. }
  153. /**
  154. * Get the output stream where the events will be serialized to.
  155. *
  156. * @return reference to the result stream, or null of only a writer was
  157. * set.
  158. */
  159. public OutputStream getOutputStream()
  160. {
  161. return m_os;
  162. }
  163. }