1 // PrintStream.java - Print string representations
3 /* Copyright (C) 1998, 1999 Red Hat, Inc.
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
12 import gnu.gcj.convert.UnicodeToBytes;
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: Not finished.
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 ()
59 private synchronized void print (String str, boolean println)
63 writeChars(str, 0, str.length());
65 writeChars(line_separator, 0, line_separator.length);
75 private synchronized void print (char[] chars, int pos, int len,
80 writeChars(chars, pos, len);
82 writeChars(line_separator, 0, line_separator.length);
92 /** Writes characters through to the inferior BufferedOutputStream. */
93 private void writeChars(char[] buf, int offset, int count)
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)
105 throw new IOException("unable to flush output byte buffer");
107 converter.setOutput(out.buf, out.count);
108 int converted = converter.write(buf, offset, count);
111 out.count = converter.count;
115 private void writeChars(String str, int offset, int count)
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)
127 throw new IOException("unable to flush output byte buffer");
129 converter.setOutput(out.buf, out.count);
130 int converted = converter.write(str, offset, count, work);
133 out.count = converter.count;
137 public void print (boolean bool)
139 print(String.valueOf(bool), false);
142 public void print (int inum)
144 print(String.valueOf(inum), false);
147 public void print (long lnum)
149 print(String.valueOf(lnum), false);
152 public void print (float fnum)
154 print(String.valueOf(fnum), false);
157 public void print (double dnum)
159 print(String.valueOf(dnum), false);
162 public void print (Object obj)
164 print(obj == null ? "null" : obj.toString(), false);
167 public void print (String str)
169 print(str == null ? "null" : str, false);
172 public synchronized void print (char ch)
175 print(work, 0, 1, false);
178 public void print (char[] charArray)
180 print(charArray, 0, charArray.length, false);
183 public void println ()
185 print(line_separator, 0, line_separator.length, false);
188 public void println (boolean bool)
190 print(String.valueOf(bool), true);
193 public void println (int inum)
195 print(String.valueOf(inum), true);
198 public void println (long lnum)
200 print(String.valueOf(lnum), true);
203 public void println (float fnum)
205 print(String.valueOf(fnum), true);
208 public void println (double dnum)
210 print(String.valueOf(dnum), true);
213 public void println (Object obj)
215 print(obj == null ? "null" : obj.toString(), true);
218 public void println (String str)
220 print (str == null ? "null" : str, true);
223 public synchronized void println (char ch)
226 print(work, 0, 1, true);
229 public void println (char[] charArray)
231 print(charArray, 0, charArray.length, true);
234 public PrintStream (OutputStream out)
239 public PrintStream (OutputStream out, boolean af)
242 if (out instanceof BufferedOutputStream)
243 this.out = (BufferedOutputStream) out;
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;
251 converter = UnicodeToBytes.getDefaultEncoder();
256 protected void setError ()
261 public void write (int oneByte)
266 if (auto_flush && oneByte == '\n')
269 catch (IOException e)
275 public void write (byte[] buffer, int offset, int count)
279 out.write(buffer, offset, count);
283 catch (IOException e)
289 BufferedOutputStream out;
290 UnicodeToBytes converter;
292 char[] work = new char[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();