Initial revision
[gcc.git] / libjava / java / io / OutputStreamWriter.java
1 /* Copyright (C) 1998, 1999 Cygnus Solutions
2
3 This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
8
9 package java.io;
10 import gnu.gcj.convert.UnicodeToBytes;
11
12 /**
13 * @author Per Bothner <bothner@cygnus.com>
14 * @date April 17, 1998.
15 */
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.
19 */
20
21 public class OutputStreamWriter extends Writer
22 {
23 BufferedOutputStream out;
24
25 UnicodeToBytes converter;
26
27 /* Temporary buffer. */
28 private char[] work;
29 private int wcount;
30
31 public String getEncoding() { return converter.getName(); }
32
33 private OutputStreamWriter(OutputStream out, UnicodeToBytes encoder)
34 {
35 super(out);
36 this.out = out instanceof BufferedOutputStream ? (BufferedOutputStream) out
37 : new BufferedOutputStream(out, 2048);
38 this.converter = encoder;
39 }
40
41 public OutputStreamWriter(OutputStream out, String enc)
42 throws UnsupportedEncodingException
43 {
44 this(out, UnicodeToBytes.getEncoder(enc));
45 }
46
47 public OutputStreamWriter(OutputStream out)
48 {
49 this(out, UnicodeToBytes.getDefaultEncoder());
50 }
51
52 public void close() throws IOException
53 {
54 synchronized (lock)
55 {
56 flush();
57 if (out != null)
58 {
59 out.close();
60 out = null;
61 }
62 work = null;
63 }
64 }
65
66 public void flush() throws IOException
67 {
68 synchronized (lock)
69 {
70 if (wcount > 0)
71 {
72 writeChars(work, 0, wcount);
73 wcount = 0;
74 }
75 out.flush();
76 }
77 }
78
79 public void write(char[] buf, int offset, int count)
80 throws IOException
81 {
82 synchronized (lock)
83 {
84 if (wcount > 0)
85 {
86 writeChars(work, 0, wcount);
87 wcount = 0;
88 }
89 writeChars(buf, offset, count);
90 }
91 }
92
93 private void writeChars(char[] buf, int offset, int count)
94 throws IOException
95 {
96 while (count > 0)
97 {
98 if (out.count != 0)
99 {
100 out.flush();
101 if (out.count != 0)
102 throw new IOException("unable to flush output byte buffer");
103 }
104 converter.setOutput(out.buf, out.count);
105 int converted = converter.write(buf, offset, count);
106 offset += converted;
107 count -= converted;
108 out.count = converter.count;
109 }
110 }
111
112 public void write(String str, int offset, int count)
113 throws IOException
114 {
115 synchronized (lock)
116 {
117 if (work == null)
118 work = new char[100];
119 int wlength = work.length;
120 while (count > 0)
121 {
122 int size = count;
123 if (wcount + size > wlength)
124 {
125 if (2*wcount > wlength)
126 {
127 writeChars(work, 0, wcount);
128 wcount = 0;
129 }
130 if (wcount + size > wlength)
131 size = wlength - wcount;
132 }
133 str.getChars(offset, offset+size, work, wcount);
134 offset += size;
135 count -= size;
136 wcount += size;
137 }
138 }
139 }
140
141 public void write(int ch) throws IOException
142 {
143 synchronized (lock)
144 {
145 if (work == null)
146 work = new char[100];
147 if (wcount >= work.length)
148 {
149 writeChars(work, 0, wcount);
150 wcount = 0;
151 }
152 work[wcount++] = (char) ch;
153 }
154 }
155 }