]>
Commit | Line | Data |
---|---|---|
ee9dd372 TT |
1 | // PrintStream.java - Print string representations |
2 | ||
3 | /* Copyright (C) 1998, 1999 Cygnus Solutions | |
4 | ||
5 | This file is part of libgcj. | |
6 | ||
7 | This software is copyrighted work licensed under the terms of the | |
8 | Libgcj License. Please consult the file "LIBGCJ_LICENSE" for | |
9 | details. */ | |
10 | ||
11 | package java.io; | |
839df961 | 12 | import gnu.gcj.convert.UnicodeToBytes; |
ee9dd372 TT |
13 | |
14 | /** | |
15 | * @author Tom Tromey <tromey@cygnus.com> | |
16 | * @date September 24, 1998 | |
17 | */ | |
18 | ||
19 | /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 | |
20 | * "The Java Language Specification", ISBN 0-201-63451-1 | |
21 | * Status: Not finished. | |
22 | */ | |
23 | ||
24 | public class PrintStream extends FilterOutputStream | |
25 | { | |
839df961 PB |
26 | /* Notice the implementation is quite similar to OutputStreamWriter. |
27 | * This leads to some minor duplication, because neither inherits | |
28 | * from the other, and we want to maximize performance. */ | |
29 | ||
ee9dd372 TT |
30 | public boolean checkError () |
31 | { | |
32 | return error; | |
33 | } | |
34 | ||
35 | public void close () | |
36 | { | |
37 | try | |
38 | { | |
39 | out.close(); | |
40 | } | |
41 | catch (IOException e) | |
42 | { | |
43 | setError (); | |
44 | } | |
45 | } | |
46 | ||
47 | public void flush () | |
48 | { | |
49 | try | |
50 | { | |
51 | out.flush(); | |
52 | } | |
53 | catch (IOException e) | |
54 | { | |
55 | setError (); | |
56 | } | |
57 | } | |
58 | ||
839df961 PB |
59 | private synchronized void print (String str, boolean println) |
60 | { | |
61 | try | |
62 | { | |
63 | writeChars(str, 0, str.length()); | |
64 | if (println) | |
65 | writeChars(line_separator, 0, line_separator.length); | |
66 | if (auto_flush) | |
67 | flush(); | |
68 | } | |
69 | catch (IOException e) | |
70 | { | |
71 | setError (); | |
72 | } | |
73 | } | |
74 | ||
75 | private synchronized void print (char[] chars, int pos, int len, | |
76 | boolean println) | |
ee9dd372 TT |
77 | { |
78 | try | |
79 | { | |
839df961 PB |
80 | writeChars(chars, pos, len); |
81 | if (println) | |
82 | writeChars(line_separator, 0, line_separator.length); | |
83 | if (auto_flush) | |
84 | flush(); | |
ee9dd372 TT |
85 | } |
86 | catch (IOException e) | |
87 | { | |
88 | setError (); | |
89 | } | |
90 | } | |
91 | ||
839df961 PB |
92 | /** Writes characters through to the inferior BufferedOutputStream. */ |
93 | private void writeChars(char[] buf, int offset, int count) | |
94 | throws IOException | |
95 | { | |
96 | while (count > 0) | |
97 | { | |
98 | // We must flush if out.count == out.buf.length. | |
99 | // It is probably a good idea to flush if out.buf is almost full. | |
100 | // This test is an approximation for "almost full". | |
101 | if (out.count + count >= out.buf.length) | |
102 | { | |
103 | out.flush(); | |
104 | if (out.count != 0) | |
105 | throw new IOException("unable to flush output byte buffer"); | |
106 | } | |
107 | converter.setOutput(out.buf, out.count); | |
108 | int converted = converter.write(buf, offset, count); | |
109 | offset += converted; | |
110 | count -= converted; | |
111 | out.count = converter.count; | |
112 | } | |
113 | } | |
114 | ||
115 | private void writeChars(String str, int offset, int count) | |
116 | throws IOException | |
117 | { | |
118 | while (count > 0) | |
119 | { | |
120 | // We must flush if out.count == out.buf.length. | |
121 | // It is probably a good idea to flush if out.buf is almost full. | |
122 | // This test is an approximation for "almost full". | |
123 | if (out.count + count >= out.buf.length) | |
124 | { | |
125 | out.flush(); | |
126 | if (out.count != 0) | |
127 | throw new IOException("unable to flush output byte buffer"); | |
128 | } | |
129 | converter.setOutput(out.buf, out.count); | |
130 | int converted = converter.write(str, offset, count, work); | |
131 | offset += converted; | |
132 | count -= converted; | |
133 | out.count = converter.count; | |
134 | } | |
135 | } | |
136 | ||
ee9dd372 TT |
137 | public void print (boolean bool) |
138 | { | |
839df961 | 139 | print(String.valueOf(bool), false); |
ee9dd372 TT |
140 | } |
141 | ||
142 | public void print (int inum) | |
143 | { | |
839df961 | 144 | print(String.valueOf(inum), false); |
ee9dd372 TT |
145 | } |
146 | ||
147 | public void print (long lnum) | |
148 | { | |
839df961 | 149 | print(String.valueOf(lnum), false); |
ee9dd372 TT |
150 | } |
151 | ||
152 | public void print (float fnum) | |
153 | { | |
839df961 | 154 | print(String.valueOf(fnum), false); |
ee9dd372 TT |
155 | } |
156 | ||
157 | public void print (double dnum) | |
158 | { | |
839df961 | 159 | print(String.valueOf(dnum), false); |
ee9dd372 TT |
160 | } |
161 | ||
162 | public void print (Object obj) | |
163 | { | |
839df961 | 164 | print(obj == null ? "null" : obj.toString(), false); |
ee9dd372 TT |
165 | } |
166 | ||
167 | public void print (String str) | |
168 | { | |
839df961 | 169 | print(str == null ? "null" : str, false); |
ee9dd372 TT |
170 | } |
171 | ||
839df961 | 172 | public synchronized void print (char ch) |
ee9dd372 | 173 | { |
839df961 PB |
174 | work[0] = ch; |
175 | print(work, 0, 1, false); | |
ee9dd372 TT |
176 | } |
177 | ||
178 | public void print (char[] charArray) | |
179 | { | |
839df961 | 180 | print(charArray, 0, charArray.length, false); |
ee9dd372 TT |
181 | } |
182 | ||
183 | public void println () | |
184 | { | |
839df961 | 185 | print(line_separator, 0, line_separator.length, false); |
ee9dd372 TT |
186 | } |
187 | ||
188 | public void println (boolean bool) | |
189 | { | |
839df961 | 190 | print(String.valueOf(bool), true); |
ee9dd372 TT |
191 | } |
192 | ||
193 | public void println (int inum) | |
194 | { | |
839df961 | 195 | print(String.valueOf(inum), true); |
ee9dd372 TT |
196 | } |
197 | ||
198 | public void println (long lnum) | |
199 | { | |
839df961 | 200 | print(String.valueOf(lnum), true); |
ee9dd372 TT |
201 | } |
202 | ||
203 | public void println (float fnum) | |
204 | { | |
839df961 | 205 | print(String.valueOf(fnum), true); |
ee9dd372 TT |
206 | } |
207 | ||
208 | public void println (double dnum) | |
209 | { | |
839df961 | 210 | print(String.valueOf(dnum), true); |
ee9dd372 TT |
211 | } |
212 | ||
213 | public void println (Object obj) | |
214 | { | |
839df961 | 215 | print(obj == null ? "null" : obj.toString(), true); |
ee9dd372 TT |
216 | } |
217 | ||
218 | public void println (String str) | |
219 | { | |
839df961 | 220 | print (str == null ? "null" : str, true); |
ee9dd372 TT |
221 | } |
222 | ||
839df961 | 223 | public synchronized void println (char ch) |
ee9dd372 | 224 | { |
839df961 PB |
225 | work[0] = ch; |
226 | print(work, 0, 1, true); | |
ee9dd372 TT |
227 | } |
228 | ||
229 | public void println (char[] charArray) | |
230 | { | |
839df961 | 231 | print(charArray, 0, charArray.length, true); |
ee9dd372 TT |
232 | } |
233 | ||
234 | public PrintStream (OutputStream out) | |
235 | { | |
839df961 | 236 | this(out, false); |
ee9dd372 TT |
237 | } |
238 | ||
239 | public PrintStream (OutputStream out, boolean af) | |
240 | { | |
6b5ba2ce BM |
241 | super(out); |
242 | if (out instanceof BufferedOutputStream) | |
243 | this.out = (BufferedOutputStream) out; | |
244 | else | |
245 | { | |
246 | this.out = new BufferedOutputStream(out, 250); | |
247 | /* PrintStream redefines "out". Explicitly reset FilterOutputStream's | |
248 | * "out" so that they're referring to the same thing. */ | |
249 | super.out = this.out; | |
250 | } | |
839df961 | 251 | converter = UnicodeToBytes.getDefaultEncoder(); |
ee9dd372 TT |
252 | error = false; |
253 | auto_flush = af; | |
254 | } | |
255 | ||
256 | protected void setError () | |
257 | { | |
258 | error = true; | |
259 | } | |
260 | ||
261 | public void write (int oneByte) | |
262 | { | |
263 | try | |
264 | { | |
265 | out.write(oneByte); | |
ee9dd372 TT |
266 | if (auto_flush && oneByte == '\n') |
267 | out.flush(); | |
268 | } | |
269 | catch (IOException e) | |
270 | { | |
271 | setError (); | |
272 | } | |
273 | } | |
274 | ||
275 | public void write (byte[] buffer, int offset, int count) | |
276 | { | |
277 | try | |
278 | { | |
279 | out.write(buffer, offset, count); | |
ee9dd372 TT |
280 | if (auto_flush) |
281 | out.flush(); | |
282 | } | |
283 | catch (IOException e) | |
284 | { | |
285 | setError (); | |
286 | } | |
287 | } | |
288 | ||
839df961 PB |
289 | BufferedOutputStream out; |
290 | UnicodeToBytes converter; | |
291 | ||
292 | char[] work = new char[100]; | |
293 | ||
ee9dd372 TT |
294 | // True if error occurred. |
295 | private boolean error; | |
296 | // True if auto-flush. | |
297 | private boolean auto_flush; | |
298 | ||
299 | // Line separator string. | |
839df961 PB |
300 | private static final char[] line_separator |
301 | = System.getProperty("line.separator").toCharArray(); | |
ee9dd372 | 302 | } |