FSF GCC merge 02/23/03
[official-gcc.git] / libjava / java / io / PrintStream.java
blob621778da9d56df100756dfcb8f778f61b7f48aa3
1 // PrintStream.java - Print string representations
3 /* Copyright (C) 1998, 1999, 2001 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
11 package java.io;
12 import gnu.gcj.convert.UnicodeToBytes;
14 /**
15 * @author Tom Tromey <tromey@cygnus.com>
16 * @date September 24, 1998
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
24 public class PrintStream extends FilterOutputStream
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. */
30 public boolean checkError ()
32 flush();
33 return error;
36 public void close ()
38 try
40 flush();
41 out.close();
43 catch (InterruptedIOException iioe)
45 Thread.currentThread().interrupt();
47 catch (IOException e)
49 setError ();
53 public void flush ()
55 try
57 out.flush();
59 catch (InterruptedIOException iioe)
61 Thread.currentThread().interrupt();
63 catch (IOException e)
65 setError ();
69 private synchronized void print (String str, boolean println)
71 try
73 writeChars(str, 0, str.length());
74 if (println)
75 writeChars(line_separator, 0, line_separator.length);
76 if (auto_flush)
77 flush();
79 catch (InterruptedIOException iioe)
81 Thread.currentThread().interrupt();
83 catch (IOException e)
85 setError ();
89 private synchronized void print (char[] chars, int pos, int len,
90 boolean println)
92 try
94 writeChars(chars, pos, len);
95 if (println)
96 writeChars(line_separator, 0, line_separator.length);
97 if (auto_flush)
98 flush();
100 catch (InterruptedIOException iioe)
102 Thread.currentThread().interrupt();
104 catch (IOException e)
106 setError ();
110 private void writeChars(char[] buf, int offset, int count)
111 throws IOException
113 while (count > 0)
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);
123 private void writeChars(String str, int offset, int count)
124 throws IOException
126 while (count > 0)
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);
136 public void print (boolean bool)
138 print(String.valueOf(bool), false);
141 public void print (int inum)
143 print(String.valueOf(inum), false);
146 public void print (long lnum)
148 print(String.valueOf(lnum), false);
151 public void print (float fnum)
153 print(String.valueOf(fnum), false);
156 public void print (double dnum)
158 print(String.valueOf(dnum), false);
161 public void print (Object obj)
163 print(obj == null ? "null" : obj.toString(), false);
166 public void print (String str)
168 print(str == null ? "null" : str, false);
171 public synchronized void print (char ch)
173 work[0] = ch;
174 print(work, 0, 1, false);
177 public void print (char[] charArray)
179 print(charArray, 0, charArray.length, false);
182 public void println ()
184 print(line_separator, 0, line_separator.length, false);
187 public void println (boolean bool)
189 print(String.valueOf(bool), true);
192 public void println (int inum)
194 print(String.valueOf(inum), true);
197 public void println (long lnum)
199 print(String.valueOf(lnum), true);
202 public void println (float fnum)
204 print(String.valueOf(fnum), true);
207 public void println (double dnum)
209 print(String.valueOf(dnum), true);
212 public void println (Object obj)
214 print(obj == null ? "null" : obj.toString(), true);
217 public void println (String str)
219 print (str == null ? "null" : str, true);
222 public synchronized void println (char ch)
224 work[0] = ch;
225 print(work, 0, 1, true);
228 public void println (char[] charArray)
230 print(charArray, 0, charArray.length, true);
233 public PrintStream (OutputStream out)
235 this(out, false);
238 public PrintStream (OutputStream out, boolean af)
240 super(out);
241 converter = UnicodeToBytes.getDefaultEncoder();
242 error = false;
243 auto_flush = af;
246 protected void setError ()
248 error = true;
251 public void write (int oneByte)
255 out.write(oneByte);
256 if (auto_flush && oneByte == '\n')
257 flush();
259 catch (InterruptedIOException iioe)
261 Thread.currentThread().interrupt();
263 catch (IOException e)
265 setError ();
269 public void write (byte[] buffer, int offset, int count)
273 out.write(buffer, offset, count);
274 if (auto_flush)
275 flush();
277 catch (InterruptedIOException iioe)
279 Thread.currentThread().interrupt();
281 catch (IOException e)
283 setError ();
287 UnicodeToBytes converter;
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];
294 // True if error occurred.
295 private boolean error;
296 // True if auto-flush.
297 private boolean auto_flush;
299 // Line separator string.
300 private static final char[] line_separator
301 = System.getProperty("line.separator").toCharArray();