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. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.io.Writer;
  23. /**
  24. * This class writes ASCII to a byte stream as quickly as possible. For the
  25. * moment it does not do buffering, though I reserve the right to do some
  26. * buffering down the line if I can prove that it will be faster even if the
  27. * output stream is buffered.
  28. */
  29. public class WriterToASCI extends Writer
  30. {
  31. /** The byte stream to write to. */
  32. private final OutputStream m_os;
  33. /**
  34. * Create an unbuffered ASCII writer.
  35. *
  36. *
  37. * @param os The byte stream to write to.
  38. */
  39. public WriterToASCI(OutputStream os)
  40. {
  41. m_os = os;
  42. }
  43. /**
  44. * Write a portion of an array of characters.
  45. *
  46. * @param chars Array of characters
  47. * @param start Offset from which to start writing characters
  48. * @param length Number of characters to write
  49. *
  50. * @exception IOException If an I/O error occurs
  51. *
  52. * @throws java.io.IOException
  53. */
  54. public void write(char chars[], int start, int length)
  55. throws java.io.IOException
  56. {
  57. int n = length+start;
  58. for (int i = start; i < n; i++)
  59. {
  60. m_os.write(chars[i]);
  61. }
  62. }
  63. /**
  64. * Write a single character. The character to be written is contained in
  65. * the 16 low-order bits of the given integer value; the 16 high-order bits
  66. * are ignored.
  67. *
  68. * <p> Subclasses that intend to support efficient single-character output
  69. * should override this method.
  70. *
  71. * @param c int specifying a character to be written.
  72. * @exception IOException If an I/O error occurs
  73. */
  74. public void write(int c) throws IOException
  75. {
  76. m_os.write(c);
  77. }
  78. /**
  79. * Write a string.
  80. *
  81. * @param str String to be written
  82. *
  83. * @exception IOException If an I/O error occurs
  84. */
  85. public void write(String s) throws IOException
  86. {
  87. int n = s.length();
  88. for (int i = 0; i < n; i++)
  89. {
  90. m_os.write(s.charAt(i));
  91. }
  92. }
  93. /**
  94. * Flush the stream. If the stream has saved any characters from the
  95. * various write() methods in a buffer, write them immediately to their
  96. * intended destination. Then, if that destination is another character or
  97. * byte stream, flush it. Thus one flush() invocation will flush all the
  98. * buffers in a chain of Writers and OutputStreams.
  99. *
  100. * @exception IOException If an I/O error occurs
  101. */
  102. public void flush() throws java.io.IOException
  103. {
  104. m_os.flush();
  105. }
  106. /**
  107. * Close the stream, flushing it first. Once a stream has been closed,
  108. * further write() or flush() invocations will cause an IOException to be
  109. * thrown. Closing a previously-closed stream, however, has no effect.
  110. *
  111. * @exception IOException If an I/O error occurs
  112. */
  113. public void close() throws java.io.IOException
  114. {
  115. m_os.close();
  116. }
  117. /**
  118. * Get the output stream where the events will be serialized to.
  119. *
  120. * @return reference to the result stream, or null of only a writer was
  121. * set.
  122. */
  123. public OutputStream getOutputStream()
  124. {
  125. return m_os;
  126. }
  127. }