reformat benchmark source codes to meet the requirements of the annotation generation.
[IRC.git] / Robust / src / ClassLibrary / SSJavaInfer / PrintStream.java
1 /* PrintStream.java -- OutputStream for printing output
2    Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005, 2006
3    Free Software Foundation, Inc.
4
5 This file is part of GNU Classpath.
6
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11  
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING.  If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
21
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library.  Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
26
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module.  An independent module is a module which is not derived from
34 or based on this library.  If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so.  If you do not wish to do so, delete this
37 exception statement from your version. */
38
39 //package java.io;
40
41 /*import java.util.Locale;
42  import java.util.Formatter;
43
44  import gnu.classpath.SystemProperties;
45  */
46 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
47  * "The Java Language Specification", ISBN 0-201-63451-1
48  * Status:  Believed complete and correct to 1.3
49  */
50
51 /**
52  * This class prints Java primitive values and object to a stream as text. None
53  * of the methods in this class throw an exception. However, errors can be
54  * detected by calling the <code>checkError()</code> method. Additionally, this
55  * stream can be designated as "autoflush" when created so that any writes are
56  * automatically flushed to the underlying output sink when the current line is
57  * terminated.
58  * <p>
59  * This class converts char's into byte's using the system default encoding.
60  * 
61  * @author Aaron M. Renn (arenn@urbanophile.com)
62  * @author Tom Tromey (tromey@cygnus.com)
63  * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
64  */
65 public class PrintStream extends FilterOutputStream // implements Appendable
66 {
67   /*
68    * Notice the implementation is quite similar to OutputStreamWriter. This
69    * leads to some minor duplication, because neither inherits from the other,
70    * and we want to maximize performance.
71    */
72
73   // Line separator string.
74   private static final char[] line_separator = { '\n' }/*
75                                                         * SystemProperties.
76                                                         * getProperty
77                                                         * ("line.separator",
78                                                         * "\n").toCharArray()
79                                                         */;
80
81   /**
82    * Encoding name
83    */
84   private final String encoding;
85
86   /**
87    * This boolean indicates whether or not an error has ever occurred on this
88    * stream.
89    */
90   private boolean error_occurred = false;
91
92   /**
93    * This is <code>true</code> if auto-flush is enabled, <code>false</code>
94    * otherwise
95    */
96   private final boolean auto_flush;
97
98   /**
99    * This method initializes a new <code>PrintStream</code> object to write to
100    * the specified output File. Doesn't autoflush.
101    * 
102    * @param file
103    *          The <code>File</code> to write to.
104    * @throws FileNotFoundException
105    *           if an error occurs while opening the file.
106    * 
107    * @since 1.5
108    */
109   public PrintStream(File file)
110   // throws FileNotFoundException
111   {
112     this(new FileOutputStream(file), false);
113   }
114
115   /**
116    * This method initializes a new <code>PrintStream</code> object to write to
117    * the specified output File. Doesn't autoflush.
118    * 
119    * @param file
120    *          The <code>File</code> to write to.
121    * @param encoding
122    *          The name of the character encoding to use for this object.
123    * @throws FileNotFoundException
124    *           If an error occurs while opening the file.
125    * @throws UnsupportedEncodingException
126    *           If the charset specified by <code>encoding</code> is invalid.
127    * 
128    * @since 1.5
129    */
130   public PrintStream(File file, String encoding)
131   // throws FileNotFoundException,UnsupportedEncodingException
132   {
133     this(new FileOutputStream(file), false, encoding);
134   }
135
136   /**
137    * This method initializes a new <code>PrintStream</code> object to write to
138    * the specified output File. Doesn't autoflush.
139    * 
140    * @param fileName
141    *          The name of the <code>File</code> to write to.
142    * @throws FileNotFoundException
143    *           if an error occurs while opening the file,
144    * 
145    * @since 1.5
146    */
147   public PrintStream(String fileName)
148   // throws FileNotFoundException
149   {
150     this(new FileOutputStream(new File(fileName)), false);
151   }
152
153   /**
154    * This method initializes a new <code>PrintStream</code> object to write to
155    * the specified output File. Doesn't autoflush.
156    * 
157    * @param fileName
158    *          The name of the <code>File</code> to write to.
159    * @param encoding
160    *          The name of the character encoding to use for this object.
161    * @throws FileNotFoundException
162    *           if an error occurs while opening the file.
163    * @throws UnsupportedEncodingException
164    *           If the charset specified by <code>encoding</code> is invalid.
165    * 
166    * @since 1.5
167    */
168   public PrintStream(String fileName, String encoding)
169   // throws FileNotFoundException,UnsupportedEncodingException
170   {
171     this(new FileOutputStream(new File(fileName)), false, encoding);
172   }
173
174   /**
175    * This method initializes a new <code>PrintStream</code> object to write to
176    * the specified output sink. Doesn't autoflush.
177    * 
178    * @param out
179    *          The <code>OutputStream</code> to write to.
180    */
181   public PrintStream(OutputStream out) {
182     this(out, false);
183   }
184
185   /**
186    * This method initializes a new <code>PrintStream</code> object to write to
187    * the specified output sink. This constructor also allows "auto-flush"
188    * functionality to be specified where the stream will be flushed after every
189    * <code>print</code> or <code>println</code> call, when the
190    * <code>write</code> methods with array arguments are called, or when a
191    * single new-line character is written.
192    * <p>
193    * 
194    * @param out
195    *          The <code>OutputStream</code> to write to.
196    * @param auto_flush
197    *          <code>true</code> to flush the stream after every line,
198    *          <code>false</code> otherwise
199    */
200   public PrintStream(OutputStream out, boolean auto_flush) {
201     super(out);
202     /*
203      * String encoding; try { encoding =
204      * SystemProperties.getProperty("file.encoding"); } catch (SecurityException
205      * e){ encoding = "ISO8859_1"; } catch (IllegalArgumentException e){
206      * encoding = "ISO8859_1"; } catch (NullPointerException e){ encoding =
207      * "ISO8859_1"; }
208      */
209     this.encoding = "ISO8859_1"; // encoding;
210     this.auto_flush = auto_flush;
211   }
212
213   /**
214    * This method initializes a new <code>PrintStream</code> object to write to
215    * the specified output sink. This constructor also allows "auto-flush"
216    * functionality to be specified where the stream will be flushed after every
217    * <code>print</code> or <code>println</code> call, when the
218    * <code>write</code> methods with array arguments are called, or when a
219    * single new-line character is written.
220    * <p>
221    * 
222    * @param out
223    *          The <code>OutputStream</code> to write to.
224    * @param auto_flush
225    *          <code>true</code> to flush the stream after every line,
226    *          <code>false</code> otherwise
227    * @param encoding
228    *          The name of the character encoding to use for this object.
229    */
230   public PrintStream(OutputStream out, boolean auto_flush, String encoding)
231   // throws UnsupportedEncodingException
232   {
233     super(out);
234
235     new String(new byte[] { 0 }, encoding); // check if encoding is supported
236     this.encoding = encoding;
237     this.auto_flush = auto_flush;
238   }
239
240   /**
241    * This method checks to see if an error has occurred on this stream. Note
242    * that once an error has occurred, this method will continue to report
243    * <code>true</code> forever for this stream. Before checking for an error
244    * condition, this method flushes the stream.
245    * 
246    * @return <code>true</code> if an error has occurred, <code>false</code>
247    *         otherwise
248    */
249   public boolean checkError() {
250     flush();
251     return error_occurred;
252   }
253
254   /**
255    * This method can be called by subclasses to indicate that an error has
256    * occurred and should be reported by <code>checkError</code>.
257    */
258   protected void setError() {
259     error_occurred = true;
260   }
261
262   /**
263    * This method closes this stream and all underlying streams.
264    */
265   public void close() {
266     /*
267      * try { flush(); out.close(); } catch (InterruptedIOException iioe) {
268      * Thread.currentThread().interrupt(); } catch (IOException e) { setError
269      * (); }
270      */
271   }
272
273   /**
274    * This method flushes any buffered bytes to the underlying stream and then
275    * flushes that stream as well.
276    */
277   public void flush() {
278     /*
279      * try { out.flush(); } catch (InterruptedIOException iioe) {
280      * Thread.currentThread().interrupt(); } catch (IOException e) { setError
281      * (); }
282      */
283   }
284
285   private synchronized void print(String str, boolean println) {
286     /*
287      * try { writeChars(str, 0, str.length()); if (println)
288      * writeChars(line_separator, 0, line_separator.length); if (auto_flush)
289      * flush(); } catch (InterruptedIOException iioe) {
290      * Thread.currentThread().interrupt(); } catch (IOException e) { setError
291      * (); }
292      */
293   }
294
295   private synchronized void print(char[] chars, int pos, int len, boolean println) {
296     /*
297      * try { writeChars(chars, pos, len); if (println)
298      * writeChars(line_separator, 0, line_separator.length); if (auto_flush)
299      * flush(); } catch (InterruptedIOException iioe) {
300      * Thread.currentThread().interrupt(); } catch (IOException e) { setError
301      * (); }
302      */
303   }
304
305   private void writeChars(char[] buf, int offset, int count)
306   // throws IOException
307   {
308     /*
309      * byte[] bytes = (new String(buf, offset, count)).getBytes(encoding);
310      * out.write(bytes, 0, bytes.length);
311      */
312   }
313
314   private void writeChars(String str, int offset, int count)
315   // throws IOException
316   {
317     /*
318      * byte[] bytes = str.substring(offset, offset+count).getBytes(encoding);
319      * out.write(bytes, 0, bytes.length);
320      */
321   }
322
323   /**
324    * This methods prints a boolean value to the stream. <code>true</code> values
325    * are printed as "true" and <code>false</code> values are printed as "false".
326    * 
327    * @param bool
328    *          The <code>boolean</code> value to print
329    */
330   public void print(boolean bool) {
331     print(String.valueOf(bool), false);
332   }
333
334   /**
335    * This method prints an integer to the stream. The value printed is
336    * determined using the <code>String.valueOf()</code> method.
337    * 
338    * @param inum
339    *          The <code>int</code> value to be printed
340    */
341   public void print(int inum) {
342     print(String.valueOf(inum), false);
343   }
344
345   /**
346    * This method prints a long to the stream. The value printed is determined
347    * using the <code>String.valueOf()</code> method.
348    * 
349    * @param lnum
350    *          The <code>long</code> value to be printed
351    */
352   public void print(long lnum) {
353     print(String.valueOf(lnum), false);
354   }
355
356   /**
357    * This method prints a float to the stream. The value printed is determined
358    * using the <code>String.valueOf()</code> method.
359    * 
360    * @param fnum
361    *          The <code>float</code> value to be printed
362    */
363   public void print(float fnum) {
364     print(String.valueOf(fnum), false);
365   }
366
367   /**
368    * This method prints a double to the stream. The value printed is determined
369    * using the <code>String.valueOf()</code> method.
370    * 
371    * @param dnum
372    *          The <code>double</code> value to be printed
373    */
374   public void print(double dnum) {
375     print(String.valueOf(dnum), false);
376   }
377
378   /**
379    * This method prints an <code>Object</code> to the stream. The actual value
380    * printed is determined by calling the <code>String.valueOf()</code> method.
381    * 
382    * @param obj
383    *          The <code>Object</code> to print.
384    */
385   public void print(Object obj) {
386     print(obj == null ? "null" : obj.toString(), false);
387   }
388
389   /**
390    * This method prints a <code>String</code> to the stream. The actual value
391    * printed depends on the system default encoding.
392    * 
393    * @param str
394    *          The <code>String</code> to print.
395    */
396   public void print(String str) {
397     print(str == null ? "null" : str, false);
398   }
399
400   /**
401    * This method prints a char to the stream. The actual value printed is
402    * determined by the character encoding in use.
403    * 
404    * @param ch
405    *          The <code>char</code> value to be printed
406    */
407   public synchronized void print(char ch) {
408     print(new char[] { ch }, 0, 1, false);
409   }
410
411   /**
412    * This method prints an array of characters to the stream. The actual value
413    * printed depends on the system default encoding.
414    * 
415    * @param charArray
416    *          The array of characters to print.
417    */
418   public void print(char[] charArray) {
419     print(charArray, 0, charArray.length, false);
420   }
421
422   /**
423    * This method prints a line separator sequence to the stream. The value
424    * printed is determined by the system property <xmp>line.separator</xmp> and
425    * is not necessarily the Unix '\n' newline character.
426    */
427   public void println() {
428     print(line_separator, 0, line_separator.length, false);
429   }
430
431   /**
432    * This methods prints a boolean value to the stream. <code>true</code> values
433    * are printed as "true" and <code>false</code> values are printed as "false".
434    * <p>
435    * This method prints a line termination sequence after printing the value.
436    * 
437    * @param bool
438    *          The <code>boolean</code> value to print
439    */
440   public void println(boolean bool) {
441     print(String.valueOf(bool), true);
442   }
443
444   /**
445    * This method prints an integer to the stream. The value printed is
446    * determined using the <code>String.valueOf()</code> method.
447    * <p>
448    * This method prints a line termination sequence after printing the value.
449    * 
450    * @param inum
451    *          The <code>int</code> value to be printed
452    */
453   public void println(int inum) {
454     print(String.valueOf(inum), true);
455   }
456
457   /**
458    * This method prints a long to the stream. The value printed is determined
459    * using the <code>String.valueOf()</code> method.
460    * <p>
461    * This method prints a line termination sequence after printing the value.
462    * 
463    * @param lnum
464    *          The <code>long</code> value to be printed
465    */
466   public void println(long lnum) {
467     print(String.valueOf(lnum), true);
468   }
469
470   /**
471    * This method prints a float to the stream. The value printed is determined
472    * using the <code>String.valueOf()</code> method.
473    * <p>
474    * This method prints a line termination sequence after printing the value.
475    * 
476    * @param fnum
477    *          The <code>float</code> value to be printed
478    */
479   public void println(float fnum) {
480     print(String.valueOf(fnum), true);
481   }
482
483   /**
484    * This method prints a double to the stream. The value printed is determined
485    * using the <code>String.valueOf()</code> method.
486    * <p>
487    * This method prints a line termination sequence after printing the value.
488    * 
489    * @param dnum
490    *          The <code>double</code> value to be printed
491    */
492   public void println(double dnum) {
493     print(String.valueOf(dnum), true);
494   }
495
496   /**
497    * This method prints an <code>Object</code> to the stream. The actual value
498    * printed is determined by calling the <code>String.valueOf()</code> method.
499    * <p>
500    * This method prints a line termination sequence after printing the value.
501    * 
502    * @param obj
503    *          The <code>Object</code> to print.
504    */
505   public void println(Object obj) {
506     print(obj == null ? "null" : obj.toString(), true);
507   }
508
509   /**
510    * This method prints a <code>String</code> to the stream. The actual value
511    * printed depends on the system default encoding.
512    * <p>
513    * This method prints a line termination sequence after printing the value.
514    * 
515    * @param str
516    *          The <code>String</code> to print.
517    */
518   public void println(String str) {
519     print(str == null ? "null" : str, true);
520   }
521
522   /**
523    * This method prints a char to the stream. The actual value printed is
524    * determined by the character encoding in use.
525    * <p>
526    * This method prints a line termination sequence after printing the value.
527    * 
528    * @param ch
529    *          The <code>char</code> value to be printed
530    */
531   public synchronized void println(char ch) {
532     print(new char[] { ch }, 0, 1, true);
533   }
534
535   /**
536    * This method prints an array of characters to the stream. The actual value
537    * printed depends on the system default encoding.
538    * <p>
539    * This method prints a line termination sequence after printing the value.
540    * 
541    * @param charArray
542    *          The array of characters to print.
543    */
544   public void println(char[] charArray) {
545     print(charArray, 0, charArray.length, true);
546   }
547
548   /**
549    * This method writes a byte of data to the stream. If auto-flush is enabled,
550    * printing a newline character will cause the stream to be flushed after the
551    * character is written.
552    * 
553    * @param oneByte
554    *          The byte to be written
555    */
556   public void write(int oneByte) {
557     /*
558      * try { out.write (oneByte & 0xff);
559      * 
560      * if (auto_flush && (oneByte == '\n')) flush (); } catch
561      * (InterruptedIOException iioe) { Thread.currentThread ().interrupt (); }
562      * catch (IOException e) { setError (); }
563      */
564   }
565
566   /**
567    * This method writes <code>len</code> bytes from the specified array starting
568    * at index <code>offset</code> into the array.
569    * 
570    * @param buffer
571    *          The array of bytes to write
572    * @param offset
573    *          The index into the array to start writing from
574    * @param len
575    *          The number of bytes to write
576    */
577   public void write(byte[] buffer, int offset, int len) {
578     /*
579      * try { out.write (buffer, offset, len);
580      * 
581      * if (auto_flush) flush (); } catch (InterruptedIOException iioe) {
582      * Thread.currentThread ().interrupt (); } catch (IOException e) { setError
583      * (); }
584      */
585   }
586
587   /** @since 1.5 */
588   public PrintStream append(char c) {
589     print(c);
590     return this;
591   }
592
593   /** @since 1.5 */
594   /*
595    * public PrintStream append(CharSequence cs) { print(cs == null ? "null" :
596    * cs.toString()); return this; }
597    */
598
599   /** @since 1.5 */
600   /*
601    * public PrintStream append(CharSequence cs, int start, int end) { print(cs
602    * == null ? "null" : cs.subSequence(start, end).toString()); return this; }
603    */
604
605   /** @since 1.5 */
606   /*
607    * public PrintStream printf(String format, Object... args) { return this;
608    * //format(format, args); }
609    */
610
611   /** @since 1.5 */
612   /*
613    * public PrintStream printf(Locale locale, String format, Object... args) {
614    * return format(locale, format, args); }
615    */
616
617   /** @since 1.5 */
618   /*
619    * public PrintStream format(String format, Object... args) { return this;
620    * //format(Locale.getDefault(), format, args); }
621    */
622
623   /** @since 1.5 */
624   /*
625    * public PrintStream format(Locale locale, String format, Object... args) {
626    * Formatter f = new Formatter(this, locale); f.format(format, args); return
627    * this; }
628    */
629 } // class PrintStream