FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / io / OutputStreamWriter.java
blobd49e104773a1a7738402bc5eda6c296defa5f306
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
9 package java.io;
10 import gnu.gcj.convert.UnicodeToBytes;
12 /**
13 * @author Per Bothner <bothner@cygnus.com>
14 * @date April 17, 1998.
16 /* Written using "Java Class Libraries", 2nd edition, plus online
17 * API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Believed complete and correct, but only supports 8859_1.
21 public class OutputStreamWriter extends Writer
23 BufferedOutputStream out;
25 UnicodeToBytes converter;
27 /* Temporary buffer. */
28 private char[] work;
29 private int wcount;
31 public String getEncoding()
33 return out != null ? converter.getName() : null;
36 private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder)
38 this.out = out instanceof BufferedOutputStream
39 ? (BufferedOutputStream) out
40 : new BufferedOutputStream(out, 250);
41 /* Don't need to call super(out) here as long as the lock gets set. */
42 this.lock = out;
43 this.converter = encoder;
46 public OutputStreamWriter(OutputStream out, String enc)
47 throws UnsupportedEncodingException
49 this(out, UnicodeToBytes.getEncoder(enc));
52 public OutputStreamWriter(OutputStream out)
54 this(out, UnicodeToBytes.getDefaultEncoder());
57 public void close() throws IOException
59 synchronized (lock)
61 if (out != null)
63 flush();
64 out.close();
65 out = null;
67 work = null;
71 public void flush() throws IOException
73 synchronized (lock)
75 if (out == null)
76 throw new IOException("Stream closed");
78 if (wcount > 0)
80 writeChars(work, 0, wcount);
81 wcount = 0;
83 out.flush();
87 public void write(char[] buf, int offset, int count)
88 throws IOException
90 synchronized (lock)
92 if (out == null)
93 throw new IOException("Stream closed");
95 if (wcount > 0)
97 writeChars(work, 0, wcount);
98 wcount = 0;
100 writeChars(buf, offset, count);
104 /** Writes characters through to the inferior BufferedOutputStream.
105 * Ignores wcount and the work buffer. */
106 private void writeChars(char[] buf, int offset, int count)
107 throws IOException
109 while (count > 0)
111 // We must flush if out.count == out.buf.length.
112 // It is probably a good idea to flush if out.buf is almost full.
113 // This test is an approximation for "almost full".
114 if (out.count + count >= out.buf.length)
116 out.flush();
117 if (out.count != 0)
118 throw new IOException("unable to flush output byte buffer");
120 converter.setOutput(out.buf, out.count);
121 int converted = converter.write(buf, offset, count);
122 offset += converted;
123 count -= converted;
124 out.count = converter.count;
128 public void write(String str, int offset, int count)
129 throws IOException
131 synchronized (lock)
133 if (out == null)
134 throw new IOException("Stream closed");
136 if (work == null)
137 work = new char[100];
138 int wlength = work.length;
139 while (count > 0)
141 int size = count;
142 if (wcount + size > wlength)
144 if (2*wcount > wlength)
146 writeChars(work, 0, wcount);
147 wcount = 0;
149 if (wcount + size > wlength)
150 size = wlength - wcount;
152 str.getChars(offset, offset+size, work, wcount);
153 offset += size;
154 count -= size;
155 wcount += size;
160 public void write(int ch) throws IOException
162 synchronized (lock)
164 if (out == null)
165 throw new IOException("Stream closed");
167 if (work == null)
168 work = new char[100];
169 if (wcount >= work.length)
171 writeChars(work, 0, wcount);
172 wcount = 0;
174 work[wcount++] = (char) ch;