1. /*
  2. * @(#)OutputStreamWriter.java 1.47 04/01/12
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.io;
  8. import java.nio.charset.Charset;
  9. import java.nio.charset.CharsetEncoder;
  10. import sun.nio.cs.StreamEncoder;
  11. /**
  12. * An OutputStreamWriter is a bridge from character streams to byte streams:
  13. * Characters written to it are encoded into bytes using a specified {@link
  14. * java.nio.charset.Charset <code>charset</code>}. The charset that it uses
  15. * may be specified by name or may be given explicitly, or the platform's
  16. * default charset may be accepted.
  17. *
  18. * <p> Each invocation of a write() method causes the encoding converter to be
  19. * invoked on the given character(s). The resulting bytes are accumulated in a
  20. * buffer before being written to the underlying output stream. The size of
  21. * this buffer may be specified, but by default it is large enough for most
  22. * purposes. Note that the characters passed to the write() methods are not
  23. * buffered.
  24. *
  25. * <p> For top efficiency, consider wrapping an OutputStreamWriter within a
  26. * BufferedWriter so as to avoid frequent converter invocations. For example:
  27. *
  28. * <pre>
  29. * Writer out
  30. * = new BufferedWriter(new OutputStreamWriter(System.out));
  31. * </pre>
  32. *
  33. * <p> A <i>surrogate pair</i> is a character represented by a sequence of two
  34. * <tt>char</tt> values: A <i>high</i> surrogate in the range '\uD800' to
  35. * '\uDBFF' followed by a <i>low</i> surrogate in the range '\uDC00' to
  36. * '\uDFFF'. If the character represented by a surrogate pair cannot be
  37. * encoded by a given charset then a charset-dependent <i>substitution
  38. * sequence</i> is written to the output stream.
  39. *
  40. * <p> A <i>malformed surrogate element</i> is a high surrogate that is not
  41. * followed by a low surrogate or a low surrogate that is not preceded by a
  42. * high surrogate. It is illegal to attempt to write a character stream
  43. * containing malformed surrogate elements. The behavior of an instance of
  44. * this class when a malformed surrogate element is written is not specified.
  45. *
  46. * @see BufferedWriter
  47. * @see OutputStream
  48. * @see java.nio.charset.Charset
  49. *
  50. * @version 1.47, 04/01/12
  51. * @author Mark Reinhold
  52. * @since JDK1.1
  53. */
  54. public class OutputStreamWriter extends Writer {
  55. private final StreamEncoder se;
  56. /**
  57. * Create an OutputStreamWriter that uses the named charset.
  58. *
  59. * @param out
  60. * An OutputStream
  61. *
  62. * @param charsetName
  63. * The name of a supported
  64. * {@link java.nio.charset.Charset </code>charset<code>}
  65. *
  66. * @exception UnsupportedEncodingException
  67. * If the named encoding is not supported
  68. */
  69. public OutputStreamWriter(OutputStream out, String charsetName)
  70. throws UnsupportedEncodingException
  71. {
  72. super(out);
  73. if (charsetName == null)
  74. throw new NullPointerException("charsetName");
  75. se = StreamEncoder.forOutputStreamWriter(out, this, charsetName);
  76. }
  77. /**
  78. * Create an OutputStreamWriter that uses the default character encoding.
  79. *
  80. * @param out An OutputStream
  81. */
  82. public OutputStreamWriter(OutputStream out) {
  83. super(out);
  84. try {
  85. se = StreamEncoder.forOutputStreamWriter(out, this, (String)null);
  86. } catch (UnsupportedEncodingException e) {
  87. throw new Error(e);
  88. }
  89. }
  90. /**
  91. * Create an OutputStreamWriter that uses the given charset. </p>
  92. *
  93. * @param out
  94. * An OutputStream
  95. *
  96. * @param cs
  97. * A charset
  98. *
  99. * @since 1.4
  100. * @spec JSR-51
  101. */
  102. public OutputStreamWriter(OutputStream out, Charset cs) {
  103. super(out);
  104. if (cs == null)
  105. throw new NullPointerException("charset");
  106. se = StreamEncoder.forOutputStreamWriter(out, this, cs);
  107. }
  108. /**
  109. * Create an OutputStreamWriter that uses the given charset encoder. </p>
  110. *
  111. * @param out
  112. * An OutputStream
  113. *
  114. * @param enc
  115. * A charset encoder
  116. *
  117. * @since 1.4
  118. * @spec JSR-51
  119. */
  120. public OutputStreamWriter(OutputStream out, CharsetEncoder enc) {
  121. super(out);
  122. if (enc == null)
  123. throw new NullPointerException("charset encoder");
  124. se = StreamEncoder.forOutputStreamWriter(out, this, enc);
  125. }
  126. /**
  127. * Return the name of the character encoding being used by this stream.
  128. *
  129. * <p> If the encoding has an historical name then that name is returned;
  130. * otherwise the encoding's canonical name is returned.
  131. *
  132. * <p> If this instance was created with the {@link
  133. * #OutputStreamWriter(OutputStream, String)} constructor then the returned
  134. * name, being unique for the encoding, may differ from the name passed to
  135. * the constructor. This method may return <tt>null</tt> if the stream has
  136. * been closed. </p>
  137. *
  138. * @return The historical name of this encoding, or possibly
  139. * <code>null</code> if the stream has been closed
  140. *
  141. * @see java.nio.charset.Charset
  142. *
  143. * @revised 1.4
  144. * @spec JSR-51
  145. */
  146. public String getEncoding() {
  147. return se.getEncoding();
  148. }
  149. /**
  150. * Flush the output buffer to the underlying byte stream, without flushing
  151. * the byte stream itself. This method is non-private only so that it may
  152. * be invoked by PrintStream.
  153. */
  154. void flushBuffer() throws IOException {
  155. se.flushBuffer();
  156. }
  157. /**
  158. * Write a single character.
  159. *
  160. * @exception IOException If an I/O error occurs
  161. */
  162. public void write(int c) throws IOException {
  163. se.write(c);
  164. }
  165. /**
  166. * Write a portion of an array of characters.
  167. *
  168. * @param cbuf Buffer of characters
  169. * @param off Offset from which to start writing characters
  170. * @param len Number of characters to write
  171. *
  172. * @exception IOException If an I/O error occurs
  173. */
  174. public void write(char cbuf[], int off, int len) throws IOException {
  175. se.write(cbuf, off, len);
  176. }
  177. /**
  178. * Write a portion of a string.
  179. *
  180. * @param str A String
  181. * @param off Offset from which to start writing characters
  182. * @param len Number of characters to write
  183. *
  184. * @exception IOException If an I/O error occurs
  185. */
  186. public void write(String str, int off, int len) throws IOException {
  187. se.write(str, off, len);
  188. }
  189. /**
  190. * Flush the stream.
  191. *
  192. * @exception IOException If an I/O error occurs
  193. */
  194. public void flush() throws IOException {
  195. se.flush();
  196. }
  197. /**
  198. * Close the stream.
  199. *
  200. * @exception IOException If an I/O error occurs
  201. */
  202. public void close() throws IOException {
  203. se.close();
  204. }
  205. }