source: trunk/gcc/libjava/java/io/PrintStream.java

Last change on this file was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

  • Property cvs2svn:cvs-rev set to 1.1.1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 5.8 KB
Line 
1// PrintStream.java - Print string representations
2
3/* Copyright (C) 1998, 1999, 2001, 2003 Free Software Foundation
4
5 This file is part of libgcj.
6
7This software is copyrighted work licensed under the terms of the
8Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9details. */
10
11package java.io;
12import gnu.gcj.convert.UnicodeToBytes;
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: Believed complete and correct to 1.3
22 */
23
24public class PrintStream extends FilterOutputStream
25{
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
30 public boolean checkError ()
31 {
32 flush();
33 return error;
34 }
35
36 public void close ()
37 {
38 try
39 {
40 flush();
41 out.close();
42 }
43 catch (InterruptedIOException iioe)
44 {
45 Thread.currentThread().interrupt();
46 }
47 catch (IOException e)
48 {
49 setError ();
50 }
51 }
52
53 public void flush ()
54 {
55 try
56 {
57 out.flush();
58 }
59 catch (InterruptedIOException iioe)
60 {
61 Thread.currentThread().interrupt();
62 }
63 catch (IOException e)
64 {
65 setError ();
66 }
67 }
68
69 private synchronized void print (String str, boolean println)
70 {
71 try
72 {
73 writeChars(str, 0, str.length());
74 if (println)
75 writeChars(line_separator, 0, line_separator.length);
76 if (auto_flush)
77 flush();
78 }
79 catch (InterruptedIOException iioe)
80 {
81 Thread.currentThread().interrupt();
82 }
83 catch (IOException e)
84 {
85 setError ();
86 }
87 }
88
89 private synchronized void print (char[] chars, int pos, int len,
90 boolean println)
91 {
92 try
93 {
94 writeChars(chars, pos, len);
95 if (println)
96 writeChars(line_separator, 0, line_separator.length);
97 if (auto_flush)
98 flush();
99 }
100 catch (InterruptedIOException iioe)
101 {
102 Thread.currentThread().interrupt();
103 }
104 catch (IOException e)
105 {
106 setError ();
107 }
108 }
109
110 private void writeChars(char[] buf, int offset, int count)
111 throws IOException
112 {
113 while (count > 0 || converter.havePendingBytes())
114 {
115 converter.setOutput(work_bytes, 0);
116 int converted = converter.write(buf, offset, count);
117 offset += converted;
118 count -= converted;
119 out.write(work_bytes, 0, converter.count);
120 }
121 }
122
123 private void writeChars(String str, int offset, int count)
124 throws IOException
125 {
126 while (count > 0 || converter.havePendingBytes())
127 {
128 converter.setOutput(work_bytes, 0);
129 int converted = converter.write(str, offset, count, work);
130 offset += converted;
131 count -= converted;
132 out.write(work_bytes, 0, converter.count);
133 }
134 }
135
136 public void print (boolean bool)
137 {
138 print(String.valueOf(bool), false);
139 }
140
141 public void print (int inum)
142 {
143 print(String.valueOf(inum), false);
144 }
145
146 public void print (long lnum)
147 {
148 print(String.valueOf(lnum), false);
149 }
150
151 public void print (float fnum)
152 {
153 print(String.valueOf(fnum), false);
154 }
155
156 public void print (double dnum)
157 {
158 print(String.valueOf(dnum), false);
159 }
160
161 public void print (Object obj)
162 {
163 print(obj == null ? "null" : obj.toString(), false);
164 }
165
166 public void print (String str)
167 {
168 print(str == null ? "null" : str, false);
169 }
170
171 public synchronized void print (char ch)
172 {
173 work[0] = ch;
174 print(work, 0, 1, false);
175 }
176
177 public void print (char[] charArray)
178 {
179 print(charArray, 0, charArray.length, false);
180 }
181
182 public void println ()
183 {
184 print(line_separator, 0, line_separator.length, false);
185 }
186
187 public void println (boolean bool)
188 {
189 print(String.valueOf(bool), true);
190 }
191
192 public void println (int inum)
193 {
194 print(String.valueOf(inum), true);
195 }
196
197 public void println (long lnum)
198 {
199 print(String.valueOf(lnum), true);
200 }
201
202 public void println (float fnum)
203 {
204 print(String.valueOf(fnum), true);
205 }
206
207 public void println (double dnum)
208 {
209 print(String.valueOf(dnum), true);
210 }
211
212 public void println (Object obj)
213 {
214 print(obj == null ? "null" : obj.toString(), true);
215 }
216
217 public void println (String str)
218 {
219 print (str == null ? "null" : str, true);
220 }
221
222 public synchronized void println (char ch)
223 {
224 work[0] = ch;
225 print(work, 0, 1, true);
226 }
227
228 public void println (char[] charArray)
229 {
230 print(charArray, 0, charArray.length, true);
231 }
232
233 public PrintStream (OutputStream out)
234 {
235 this(out, false);
236 }
237
238 public PrintStream (OutputStream out, boolean af)
239 {
240 super(out);
241 converter = UnicodeToBytes.getDefaultEncoder();
242 error = false;
243 auto_flush = af;
244 }
245
246 protected void setError ()
247 {
248 error = true;
249 }
250
251 public void write (int oneByte)
252 {
253 try
254 {
255 out.write(oneByte);
256 if (auto_flush && oneByte == '\n')
257 flush();
258 }
259 catch (InterruptedIOException iioe)
260 {
261 Thread.currentThread().interrupt();
262 }
263 catch (IOException e)
264 {
265 setError ();
266 }
267 }
268
269 public void write (byte[] buffer, int offset, int count)
270 {
271 try
272 {
273 out.write(buffer, offset, count);
274 if (auto_flush)
275 flush();
276 }
277 catch (InterruptedIOException iioe)
278 {
279 Thread.currentThread().interrupt();
280 }
281 catch (IOException e)
282 {
283 setError ();
284 }
285 }
286
287 UnicodeToBytes converter;
288
289 // Work buffer of characters for converter.
290 char[] work = new char[100];
291 // Work buffer of bytes where we temporarily keep converter output.
292 byte[] work_bytes = new byte[100];
293
294 // True if error occurred.
295 private boolean error;
296 // True if auto-flush.
297 private boolean auto_flush;
298
299 // Line separator string.
300 private static final char[] line_separator
301 = System.getProperty("line.separator").toCharArray();
302}
Note: See TracBrowser for help on using the repository browser.