2013-11-04 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libjava / java / io / OutputStreamWriter.java
blobaac1684c697c09c81d214ad9913035c4bea4f876
1 /* OutputStreamWriter.java -- Writer that converts chars to bytes
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.io;
41 import gnu.gcj.convert.UnicodeToBytes;
42 import gnu.gcj.convert.CharsetToBytesAdaptor;
43 import java.nio.charset.Charset;
44 import java.nio.charset.CharsetEncoder;
46 /**
47 * This class writes characters to an output stream that is byte oriented
48 * It converts the chars that are written to bytes using an encoding layer,
49 * which is specific to a particular encoding standard. The desired
50 * encoding can either be specified by name, or if no encoding is specified,
51 * the system default encoding will be used. The system default encoding
52 * name is determined from the system property <code>file.encoding</code>.
53 * The only encodings that are guaranteed to be available are "8859_1"
54 * (the Latin-1 character set) and "UTF8". Unfortunately, Java does not
55 * provide a mechanism for listing the encodings that are supported in
56 * a given implementation.
57 * <p>
58 * Here is a list of standard encoding names that may be available:
59 * <p>
60 * <ul>
61 * <li>8859_1 (ISO-8859-1/Latin-1)
62 * <li>8859_2 (ISO-8859-2/Latin-2)
63 * <li>8859_3 (ISO-8859-3/Latin-3)
64 * <li>8859_4 (ISO-8859-4/Latin-4)
65 * <li>8859_5 (ISO-8859-5/Latin-5)
66 * <li>8859_6 (ISO-8859-6/Latin-6)
67 * <li>8859_7 (ISO-8859-7/Latin-7)
68 * <li>8859_8 (ISO-8859-8/Latin-8)
69 * <li>8859_9 (ISO-8859-9/Latin-9)
70 * <li>ASCII (7-bit ASCII)
71 * <li>UTF8 (UCS Transformation Format-8)
72 * <li>More Later
73 * </ul>
75 * @author Aaron M. Renn (arenn@urbanophile.com)
76 * @author Per Bothner (bothner@cygnus.com)
77 * @date April 17, 1998.
79 public class OutputStreamWriter extends Writer
81 BufferedOutputStream out;
83 /**
84 * This is the byte-character encoder class that does the writing and
85 * translation of characters to bytes before writing to the underlying
86 * class.
88 UnicodeToBytes converter;
90 /* Temporary buffer. */
91 private char[] work;
92 private int wcount;
94 private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder)
96 this.out = out instanceof BufferedOutputStream
97 ? (BufferedOutputStream) out
98 : new BufferedOutputStream(out, 250);
99 /* Don't need to call super(out) here as long as the lock gets set. */
100 this.lock = out;
101 this.converter = encoder;
105 * This method initializes a new instance of <code>OutputStreamWriter</code>
106 * to write to the specified stream using a caller supplied character
107 * encoding scheme. Note that due to a deficiency in the Java language
108 * design, there is no way to determine which encodings are supported.
110 * @param out The <code>OutputStream</code> to write to
111 * @param encoding_scheme The name of the encoding scheme to use for
112 * character to byte translation
114 * @exception UnsupportedEncodingException If the named encoding is
115 * not available.
117 public OutputStreamWriter (OutputStream out, String encoding_scheme)
118 throws UnsupportedEncodingException
120 this(out, UnicodeToBytes.getEncoder(encoding_scheme));
124 * This method initializes a new instance of <code>OutputStreamWriter</code>
125 * to write to the specified stream using the default encoding.
127 * @param out The <code>OutputStream</code> to write to
129 public OutputStreamWriter (OutputStream out)
131 this(out, UnicodeToBytes.getDefaultEncoder());
135 * This method initializes a new instance of <code>OutputStreamWriter</code>
136 * to write to the specified stream using a given <code>Charset</code>.
138 * @param out The <code>OutputStream</code> to write to
139 * @param cs The <code>Charset</code> of the encoding to use
141 public OutputStreamWriter(OutputStream out, Charset cs)
143 this(out, new CharsetToBytesAdaptor(cs));
147 * This method initializes a new instance of <code>OutputStreamWriter</code>
148 * to write to the specified stream using a given
149 * <code>CharsetEncoder</code>.
151 * @param out The <code>OutputStream</code> to write to
152 * @param enc The <code>CharsetEncoder</code> to encode the output with
154 public OutputStreamWriter(OutputStream out, CharsetEncoder enc)
156 this(out, new CharsetToBytesAdaptor(enc));
160 * This method closes this stream, and the underlying
161 * <code>OutputStream</code>
163 * @exception IOException If an error occurs
165 public void close () throws IOException
167 synchronized (lock)
169 if (out != null)
171 converter.setFinished();
172 flush();
173 out.close();
174 out = null;
176 work = null;
181 * This method returns the name of the character encoding scheme currently
182 * in use by this stream. If the stream has been closed, then this method
183 * may return <code>null</code>.
185 * @return The encoding scheme name
187 public String getEncoding ()
189 return out != null ? converter.getName() : null;
193 * This method flushes any buffered bytes to the underlying output sink.
195 * @exception IOException If an error occurs
197 public void flush () throws IOException
199 synchronized (lock)
201 if (out == null)
202 throw new IOException("Stream closed");
204 // Always write -- if we are close()ing then we want to make
205 // sure the converter is flushed.
206 if (work == null)
207 work = new char[100];
208 writeChars(work, 0, wcount);
209 wcount = 0;
211 out.flush();
216 * This method writes <code>count</code> characters from the specified
217 * array to the output stream starting at position <code>offset</code>
218 * into the array.
220 * @param buf The array of character to write from
221 * @param offset The offset into the array to start writing chars from
222 * @param count The number of chars to write.
224 * @exception IOException If an error occurs
226 public void write (char[] buf, int offset, int count) throws IOException
228 synchronized (lock)
230 if (out == null)
231 throw new IOException("Stream closed");
233 if (wcount > 0)
235 writeChars(work, 0, wcount);
236 wcount = 0;
238 writeChars(buf, offset, count);
243 * Writes characters through to the inferior BufferedOutputStream.
244 * Ignores wcount and the work buffer.
246 private void writeChars(char[] buf, int offset, int count)
247 throws IOException
251 // We must flush if out.count == out.buf.length.
252 // It is probably a good idea to flush if out.buf is almost full.
253 // This test is an approximation for "almost full".
254 if (out.count + count >= out.buf.length)
256 out.flush();
257 if (out.count != 0)
258 throw new IOException("unable to flush output byte buffer");
260 converter.setOutput(out.buf, out.count);
261 int converted = converter.write(buf, offset, count);
262 // Must set this before we flush the output stream, because
263 // flushing will reset 'out.count'.
264 out.count = converter.count;
265 // Flush if we cannot make progress.
266 if (converted == 0 && out.count == converter.count)
268 out.flush();
269 if (out.count != 0)
270 throw new IOException("unable to flush output byte buffer");
272 offset += converted;
273 count -= converted;
275 while (count > 0 || converter.havePendingBytes());
279 * This method writes <code>count</code> bytes from the specified
280 * <code>String</code> starting at position <code>offset</code> into the
281 * <code>String</code>.
283 * @param str The <code>String</code> to write chars from
284 * @param offset The position in the <code>String</code> to start
285 * writing chars from
286 * @param count The number of chars to write
288 * @exception IOException If an error occurs
290 public void write (String str, int offset, int count) throws IOException
292 synchronized (lock)
294 if (out == null)
295 throw new IOException("Stream closed");
297 if (work == null)
298 work = new char[100];
299 int wlength = work.length;
300 while (count > 0)
302 int size = count;
303 if (wcount + size > wlength)
305 if (2*wcount > wlength)
307 writeChars(work, 0, wcount);
308 wcount = 0;
310 if (wcount + size > wlength)
311 size = wlength - wcount;
313 str.getChars(offset, offset+size, work, wcount);
314 offset += size;
315 count -= size;
316 wcount += size;
322 * This method writes a single character to the output stream.
324 * @param ch The char to write, passed as an int.
326 * @exception IOException If an error occurs
328 public void write (int ch) throws IOException
330 synchronized (lock)
332 if (out == null)
333 throw new IOException("Stream closed");
335 if (work == null)
336 work = new char[100];
337 if (wcount >= work.length)
339 writeChars(work, 0, wcount);
340 wcount = 0;
342 work[wcount++] = (char) ch;
346 } // class OutputStreamWriter