1 /* Copyright (C) 1998, 1999 Cygnus Solutions
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() { return converter
.getName(); }
33 private OutputStreamWriter(OutputStream out
, UnicodeToBytes encoder
)
36 this.out
= out
instanceof BufferedOutputStream ?
(BufferedOutputStream
) out
37 : new BufferedOutputStream(out
, 2048);
38 this.converter
= encoder
;
41 public OutputStreamWriter(OutputStream out
, String enc
)
42 throws UnsupportedEncodingException
44 this(out
, UnicodeToBytes
.getEncoder(enc
));
47 public OutputStreamWriter(OutputStream out
)
49 this(out
, UnicodeToBytes
.getDefaultEncoder());
52 public void close() throws IOException
66 public void flush() throws IOException
72 writeChars(work
, 0, wcount
);
79 public void write(char[] buf
, int offset
, int count
)
86 writeChars(work
, 0, wcount
);
89 writeChars(buf
, offset
, count
);
93 private void writeChars(char[] buf
, int offset
, int count
)
102 throw new IOException("unable to flush output byte buffer");
104 converter
.setOutput(out
.buf
, out
.count
);
105 int converted
= converter
.write(buf
, offset
, count
);
108 out
.count
= converter
.count
;
112 public void write(String str
, int offset
, int count
)
118 work
= new char[100];
119 int wlength
= work
.length
;
123 if (wcount
+ size
> wlength
)
125 if (2*wcount
> wlength
)
127 writeChars(work
, 0, wcount
);
130 if (wcount
+ size
> wlength
)
131 size
= wlength
- wcount
;
133 str
.getChars(offset
, offset
+size
, work
, wcount
);
141 public void write(int ch
) throws IOException
146 work
= new char[100];
147 if (wcount
>= work
.length
)
149 writeChars(work
, 0, wcount
);
152 work
[wcount
++] = (char) ch
;