Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / libjava / java / io / OutputStreamWriter.java
blob07050415aba43009b363717b029fb7538145755b
1 /* OutputStreamWriter.java -- Writer that converts chars to bytes
2 Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 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., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 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;
43 /**
44 * This class writes characters to an output stream that is byte oriented
45 * It converts the chars that are written to bytes using an encoding layer,
46 * which is specific to a particular encoding standard. The desired
47 * encoding can either be specified by name, or if no encoding is specified,
48 * the system default encoding will be used. The system default encoding
49 * name is determined from the system property <code>file.encoding</code>.
50 * The only encodings that are guaranteed to be available are "8859_1"
51 * (the Latin-1 character set) and "UTF8". Unfortunately, Java does not
52 * provide a mechanism for listing the encodings that are supported in
53 * a given implementation.
54 * <p>
55 * Here is a list of standard encoding names that may be available:
56 * <p>
57 * <ul>
58 * <li>8859_1 (ISO-8859-1/Latin-1)
59 * <li>8859_2 (ISO-8859-2/Latin-2)
60 * <li>8859_3 (ISO-8859-3/Latin-3)
61 * <li>8859_4 (ISO-8859-4/Latin-4)
62 * <li>8859_5 (ISO-8859-5/Latin-5)
63 * <li>8859_6 (ISO-8859-6/Latin-6)
64 * <li>8859_7 (ISO-8859-7/Latin-7)
65 * <li>8859_8 (ISO-8859-8/Latin-8)
66 * <li>8859_9 (ISO-8859-9/Latin-9)
67 * <li>ASCII (7-bit ASCII)
68 * <li>UTF8 (UCS Transformation Format-8)
69 * <li>More Later
70 * </ul>
72 * @author Aaron M. Renn (arenn@urbanophile.com)
73 * @author Per Bothner (bothner@cygnus.com)
74 * @date April 17, 1998.
76 public class OutputStreamWriter extends Writer
78 BufferedOutputStream out;
80 /**
81 * This is the byte-character encoder class that does the writing and
82 * translation of characters to bytes before writing to the underlying
83 * class.
85 UnicodeToBytes converter;
87 /* Temporary buffer. */
88 private char[] work;
89 private int wcount;
91 private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder)
93 this.out = out instanceof BufferedOutputStream
94 ? (BufferedOutputStream) out
95 : new BufferedOutputStream(out, 250);
96 /* Don't need to call super(out) here as long as the lock gets set. */
97 this.lock = out;
98 this.converter = encoder;
102 * This method initializes a new instance of <code>OutputStreamWriter</code>
103 * to write to the specified stream using a caller supplied character
104 * encoding scheme. Note that due to a deficiency in the Java language
105 * design, there is no way to determine which encodings are supported.
107 * @param out The <code>OutputStream</code> to write to
108 * @param encoding_scheme The name of the encoding scheme to use for
109 * character to byte translation
111 * @exception UnsupportedEncodingException If the named encoding is
112 * not available.
114 public OutputStreamWriter (OutputStream out, String encoding_scheme)
115 throws UnsupportedEncodingException
117 this(out, UnicodeToBytes.getEncoder(encoding_scheme));
121 * This method initializes a new instance of <code>OutputStreamWriter</code>
122 * to write to the specified stream using the default encoding.
124 * @param out The <code>OutputStream</code> to write to
126 public OutputStreamWriter (OutputStream out)
128 this(out, UnicodeToBytes.getDefaultEncoder());
132 * This method closes this stream, and the underlying
133 * <code>OutputStream</code>
135 * @exception IOException If an error occurs
137 public void close () throws IOException
139 synchronized (lock)
141 if (out != null)
143 flush();
144 out.close();
145 out = null;
147 work = null;
152 * This method returns the name of the character encoding scheme currently
153 * in use by this stream. If the stream has been closed, then this method
154 * may return <code>null</code>.
156 * @return The encoding scheme name
158 public String getEncoding ()
160 return out != null ? converter.getName() : null;
164 * This method flushes any buffered bytes to the underlying output sink.
166 * @exception IOException If an error occurs
168 public void flush () throws IOException
170 synchronized (lock)
172 if (out == null)
173 throw new IOException("Stream closed");
175 if (wcount > 0)
177 writeChars(work, 0, wcount);
178 wcount = 0;
180 out.flush();
185 * This method writes <code>count</code> characters from the specified
186 * array to the output stream starting at position <code>offset</code>
187 * into the array.
189 * @param buf The array of character to write from
190 * @param offset The offset into the array to start writing chars from
191 * @param count The number of chars to write.
193 * @exception IOException If an error occurs
195 public void write (char[] buf, int offset, int count) throws IOException
197 synchronized (lock)
199 if (out == null)
200 throw new IOException("Stream closed");
202 if (wcount > 0)
204 writeChars(work, 0, wcount);
205 wcount = 0;
207 writeChars(buf, offset, count);
212 * Writes characters through to the inferior BufferedOutputStream.
213 * Ignores wcount and the work buffer.
215 private void writeChars(char[] buf, int offset, int count)
216 throws IOException
218 while (count > 0 || converter.havePendingBytes())
220 // We must flush if out.count == out.buf.length.
221 // It is probably a good idea to flush if out.buf is almost full.
222 // This test is an approximation for "almost full".
223 if (out.count + count >= out.buf.length)
225 out.flush();
226 if (out.count != 0)
227 throw new IOException("unable to flush output byte buffer");
229 converter.setOutput(out.buf, out.count);
230 int converted = converter.write(buf, offset, count);
231 // Flush if we cannot make progress.
232 if (converted == 0 && out.count == converter.count)
234 out.flush();
235 if (out.count != 0)
236 throw new IOException("unable to flush output byte buffer");
238 offset += converted;
239 count -= converted;
240 out.count = converter.count;
245 * This method writes <code>count</code> bytes from the specified
246 * <code>String</code> starting at position <code>offset</code> into the
247 * <code>String</code>.
249 * @param str The <code>String</code> to write chars from
250 * @param offset The position in the <code>String</code> to start
251 * writing chars from
252 * @param count The number of chars to write
254 * @exception IOException If an error occurs
256 public void write (String str, int offset, int count) throws IOException
258 synchronized (lock)
260 if (out == null)
261 throw new IOException("Stream closed");
263 if (work == null)
264 work = new char[100];
265 int wlength = work.length;
266 while (count > 0)
268 int size = count;
269 if (wcount + size > wlength)
271 if (2*wcount > wlength)
273 writeChars(work, 0, wcount);
274 wcount = 0;
276 if (wcount + size > wlength)
277 size = wlength - wcount;
279 str.getChars(offset, offset+size, work, wcount);
280 offset += size;
281 count -= size;
282 wcount += size;
288 * This method writes a single character to the output stream.
290 * @param ch The char to write, passed as an int.
292 * @exception IOException If an error occurs
294 public void write (int ch) throws IOException
296 synchronized (lock)
298 if (out == null)
299 throw new IOException("Stream closed");
301 if (work == null)
302 work = new char[100];
303 if (wcount >= work.length)
305 writeChars(work, 0, wcount);
306 wcount = 0;
308 work[wcount++] = (char) ch;
312 } // class OutputStreamWriter