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
10 import gnu
.gcj
.convert
.UnicodeToBytes
;
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. */
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. */
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
71 public void flush() throws IOException
76 throw new IOException("Stream closed");
80 writeChars(work
, 0, wcount
);
87 public void write(char[] buf
, int offset
, int count
)
93 throw new IOException("Stream closed");
97 writeChars(work
, 0, wcount
);
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
)
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
)
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
);
124 out
.count
= converter
.count
;
128 public void write(String str
, int offset
, int count
)
134 throw new IOException("Stream closed");
137 work
= new char[100];
138 int wlength
= work
.length
;
142 if (wcount
+ size
> wlength
)
144 if (2*wcount
> wlength
)
146 writeChars(work
, 0, wcount
);
149 if (wcount
+ size
> wlength
)
150 size
= wlength
- wcount
;
152 str
.getChars(offset
, offset
+size
, work
, wcount
);
160 public void write(int ch
) throws IOException
165 throw new IOException("Stream closed");
168 work
= new char[100];
169 if (wcount
>= work
.length
)
171 writeChars(work
, 0, wcount
);
174 work
[wcount
++] = (char) ch
;