1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2003 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 com.sun.org.apache.xml.internal.serializer;
  58. import java.io.IOException;
  59. import java.io.OutputStream;
  60. import java.io.UnsupportedEncodingException;
  61. import java.io.Writer;
  62. /**
  63. * This class writes ASCII to a byte stream as quickly as possible. For the
  64. * moment it does not do buffering, though I reserve the right to do some
  65. * buffering down the line if I can prove that it will be faster even if the
  66. * output stream is buffered.
  67. */
  68. public class WriterToUTF8 extends Writer
  69. {
  70. /** A flag to force flush output to System.out when stepping with a debugger (debug only!) */
  71. private static final boolean DEBUG_OUT = false;
  72. /** The byte stream to write to. */
  73. private final OutputStream m_os;
  74. /**
  75. * Create an unbuffered UTF-8 writer.
  76. *
  77. *
  78. * @param os The byte stream to write to.
  79. *
  80. * @throws UnsupportedEncodingException
  81. */
  82. public WriterToUTF8(OutputStream os) throws UnsupportedEncodingException
  83. {
  84. m_os = os;
  85. }
  86. /**
  87. * Write a single character. The character to be written is contained in
  88. * the 16 low-order bits of the given integer value; the 16 high-order bits
  89. * are ignored.
  90. *
  91. * <p> Subclasses that intend to support efficient single-character output
  92. * should override this method.
  93. *
  94. * @param c int specifying a character to be written.
  95. * @exception IOException If an I/O error occurs
  96. */
  97. public void write(final int c) throws IOException
  98. {
  99. if (c < 0x80)
  100. m_os.write(c);
  101. else if (c < 0x800)
  102. {
  103. m_os.write(0xc0 + (c >> 6));
  104. m_os.write(0x80 + (c & 0x3f));
  105. }
  106. else
  107. {
  108. m_os.write(0xe0 + (c >> 12));
  109. m_os.write(0x80 + ((c >> 6) & 0x3f));
  110. m_os.write(0x80 + (c & 0x3f));
  111. }
  112. if (DEBUG_OUT)
  113. {
  114. if (c < 0x80)
  115. {
  116. char ch = (char) c;
  117. System.out.print(ch);
  118. }
  119. else if (c < 0x800)
  120. {
  121. System.out.print(0xc0 + (c >> 6));
  122. System.out.print(0x80 + (c & 0x3f));
  123. }
  124. else
  125. {
  126. System.out.print(0xe0 + (c >> 12));
  127. System.out.print(0x80 + ((c >> 6) & 0x3f));
  128. System.out.print(0x80 + (c & 0x3f));
  129. }
  130. System.out.flush();
  131. }
  132. return;
  133. }
  134. /**
  135. * Write a portion of an array of characters.
  136. *
  137. * @param chars Array of characters
  138. * @param start Offset from which to start writing characters
  139. * @param length Number of characters to write
  140. *
  141. * @exception IOException If an I/O error occurs
  142. *
  143. * @throws java.io.IOException
  144. */
  145. public void write(final char chars[], final int start, final int length)
  146. throws java.io.IOException
  147. {
  148. final OutputStream os = m_os;
  149. int n = length + start;
  150. for (int i = start; i < n; i++)
  151. {
  152. final char c = chars[i];
  153. if (c < 0x80)
  154. os.write(c);
  155. else if (c < 0x800)
  156. {
  157. os.write(0xc0 + (c >> 6));
  158. os.write(0x80 + (c & 0x3f));
  159. }
  160. else
  161. {
  162. os.write(0xe0 + (c >> 12));
  163. os.write(0x80 + ((c >> 6) & 0x3f));
  164. os.write(0x80 + (c & 0x3f));
  165. }
  166. }
  167. if (DEBUG_OUT)
  168. {
  169. for (int i = start; i < n; i++)
  170. {
  171. final char c = chars[i];
  172. if (c < 0x80)
  173. System.out.print(c);
  174. else if (c < 0x800)
  175. {
  176. System.out.print(0xc0 + (c >> 6));
  177. System.out.print(0x80 + (c & 0x3f));
  178. }
  179. else
  180. {
  181. System.out.print(0xe0 + (c >> 12));
  182. System.out.print(0x80 + ((c >> 6) & 0x3f));
  183. System.out.print(0x80 + (c & 0x3f));
  184. }
  185. }
  186. System.out.flush();
  187. }
  188. return;
  189. }
  190. /**
  191. * Write a string.
  192. *
  193. * @param s String to be written
  194. *
  195. * @exception IOException If an I/O error occurs
  196. */
  197. public void write(final String s) throws IOException
  198. {
  199. final int n = s.length();
  200. final OutputStream os = m_os;
  201. for (int i = 0; i < n; i++)
  202. {
  203. final char c = s.charAt(i);
  204. if (c < 0x80)
  205. os.write(c);
  206. else if (c < 0x800)
  207. {
  208. os.write(0xc0 + (c >> 6));
  209. os.write(0x80 + (c & 0x3f));
  210. }
  211. else
  212. {
  213. os.write(0xe0 + (c >> 12));
  214. os.write(0x80 + ((c >> 6) & 0x3f));
  215. os.write(0x80 + (c & 0x3f));
  216. }
  217. }
  218. if (DEBUG_OUT)
  219. {
  220. for (int i = 0; i < n; i++)
  221. {
  222. final char c = s.charAt(i);
  223. if (c < 0x80)
  224. System.out.print(c);
  225. else if (c < 0x800)
  226. {
  227. System.out.print(0xc0 + (c >> 6));
  228. System.out.print(0x80 + (c & 0x3f));
  229. }
  230. else
  231. {
  232. System.out.print(0xe0 + (c >> 12));
  233. System.out.print(0x80 + ((c >> 6) & 0x3f));
  234. System.out.print(0x80 + (c & 0x3f));
  235. }
  236. }
  237. System.out.flush();
  238. }
  239. return;
  240. }
  241. /**
  242. * Flush the stream. If the stream has saved any characters from the
  243. * various write() methods in a buffer, write them immediately to their
  244. * intended destination. Then, if that destination is another character or
  245. * byte stream, flush it. Thus one flush() invocation will flush all the
  246. * buffers in a chain of Writers and OutputStreams.
  247. *
  248. * @exception IOException If an I/O error occurs
  249. *
  250. * @throws java.io.IOException
  251. */
  252. public void flush() throws java.io.IOException
  253. {
  254. m_os.flush();
  255. }
  256. /**
  257. * Close the stream, flushing it first. Once a stream has been closed,
  258. * further write() or flush() invocations will cause an IOException to be
  259. * thrown. Closing a previously-closed stream, however, has no effect.
  260. *
  261. * @exception IOException If an I/O error occurs
  262. *
  263. * @throws java.io.IOException
  264. */
  265. public void close() throws java.io.IOException
  266. {
  267. m_os.close();
  268. }
  269. /**
  270. * Get the output stream where the events will be serialized to.
  271. *
  272. * @return reference to the result stream, or null of only a writer was
  273. * set.
  274. */
  275. public OutputStream getOutputStream()
  276. {
  277. return m_os;
  278. }
  279. }