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