OSDN Git Service

b37bea70e29df8fdf387377927053d6ac66e41f8
[pf3gnuchains/gcc-fork.git] / libjava / java / io / PrintStream.java
1 // PrintStream.java - Print string representations
2
3 /* Copyright (C) 1998, 1999  Red Hat, Inc.
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;
12 import 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:  Not finished.
22  */
23
24 public 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     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
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)
77   {
78     try
79       {
80         writeChars(chars, pos, len);
81         if (println)
82           writeChars(line_separator, 0, line_separator.length);
83         if (auto_flush)
84           flush();
85       }
86     catch (IOException e)
87       {
88         setError ();
89       }
90   }
91
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
137   public void print (boolean bool)
138   {
139     print(String.valueOf(bool), false);
140   }
141
142   public void print (int inum)
143   {
144     print(String.valueOf(inum), false);
145   }
146
147   public void print (long lnum)
148   {
149     print(String.valueOf(lnum), false);
150   }
151
152   public void print (float fnum)
153   {
154     print(String.valueOf(fnum), false);
155   }
156
157   public void print (double dnum)
158   {
159     print(String.valueOf(dnum), false);
160   }
161
162   public void print (Object obj)
163   {
164     print(obj == null ? "null" : obj.toString(), false);
165   }
166
167   public void print (String str)
168   {
169     print(str == null ? "null" : str, false);
170   }
171
172   public synchronized void print (char ch)
173   {
174     work[0] = ch;
175     print(work, 0, 1, false);
176   }
177
178   public void print (char[] charArray)
179   {
180     print(charArray, 0, charArray.length, false);
181   }
182
183   public void println ()
184   {
185     print(line_separator, 0, line_separator.length, false);
186   }
187
188   public void println (boolean bool)
189   {
190     print(String.valueOf(bool), true);
191   }
192
193   public void println (int inum)
194   {
195     print(String.valueOf(inum), true);
196   }
197
198   public void println (long lnum)
199   {
200     print(String.valueOf(lnum), true);
201   }
202
203   public void println (float fnum)
204   {
205     print(String.valueOf(fnum), true);
206   }
207
208   public void println (double dnum)
209   {
210     print(String.valueOf(dnum), true);
211   }
212
213   public void println (Object obj)
214   {
215     print(obj == null ? "null" : obj.toString(), true);
216   }
217
218   public void println (String str)
219   {
220     print (str == null ? "null" : str, true);
221   }
222
223   public synchronized void println (char ch)
224   {
225     work[0] = ch;
226     print(work, 0, 1, true);
227   }
228
229   public void println (char[] charArray)
230   {
231     print(charArray, 0, charArray.length, true);
232   }
233
234   public PrintStream (OutputStream out)
235   {
236     this(out, false);
237   }
238
239   public PrintStream (OutputStream out, boolean af)
240   {
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       }
251     converter = UnicodeToBytes.getDefaultEncoder();
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);
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);
280         if (auto_flush)
281           out.flush();
282       }
283     catch (IOException e)
284       {
285         setError ();
286       }
287   }
288
289   BufferedOutputStream out;
290   UnicodeToBytes converter;
291
292   char[] work = new char[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 }