/Users/lyon/j4p/src/javagroup/util/StandardIO.java

1    /* 
2     * Copyright (C) 1997, 1998 Luke Gorrie 
3     * 
4     * This library is free software; you can redistribute it and/or 
5     * modify it under the terms of the GNU Library General Public 
6     * License as published by the Free Software Foundation; either 
7     * version 2 of the License, or (at your option) any later version. 
8     * 
9     * This library is distributed in the hope that it will be useful, 
10    * but WITHOUT ANY WARRANTY; without even the implied warranty of 
11    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12    * Library General Public License for more details. 
13    * 
14    * You should have received a copy of the GNU Library General Public 
15    * License along with this library; if not, write to the 
16    * Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
17    * MA 02139, USA. 
18    */ 
19    
20   package javagroup.util; 
21    
22   import java.io.*; 
23    
24   /** 
25    * Standard IO class that holds input/output/error streams. 
26    * 
27    * @author Luke Gorrie 
28    */ 
29   public class StandardIO { 
30    
31       /** 
32        * true if System.{in, out, err} wrappers are installed 
33        */ 
34       protected static boolean _systemWrappersInstalled = false; 
35    
36       // instance streams 
37       protected PrintStream _out, _err; 
38       protected InputStream _in; 
39    
40       // the "system" streams 
41       protected static PrintStream __out, __err; 
42       protected static InputStream __in; 
43    
44       static { 
45           try { 
46               ensureSystemWrappersInstalled(); 
47           } catch (RuntimeException e) { 
48               // don't let any runtime exceptions propagate from the static initializer 
49           } 
50       } 
51    
52       public StandardIO(PrintStream out, InputStream in, PrintStream err) { 
53           _out = out; 
54           _err = err; 
55           _in = in; 
56    
57           __out = new PrintStream(new StandardOutputStream()); 
58           __in = new StandardInputStream(); 
59           __err = new PrintStream(new StandardErrorStream()); 
60       } 
61    
62       public static StandardIO getInstance() { 
63           return (StandardIO) Namespace.getNamespace() 
64                   .getInstanceForClass(StandardIO.class); 
65       } 
66    
67       public static synchronized void ensureSystemWrappersInstalled() { 
68           if (!_systemWrappersInstalled) { 
69               installSystemWrappers(); 
70           } 
71           // make sure the System standard-IO streams are the redirectors 
72           try { 
73               System.setOut(__out); 
74               System.setIn(__in); 
75               System.setErr(__err); 
76           } catch (SecurityException e) { 
77               // ignore any security exception 
78           } 
79       } 
80    
81       public static synchronized void installSystemWrappers() { 
82    
83           if (!_systemWrappersInstalled) { 
84               _systemWrappersInstalled = true; 
85               PrintStream out = new PrintStream( 
86                       new FileOutputStream(FileDescriptor.out), true); 
87               PrintStream err = new PrintStream( 
88                       new FileOutputStream(FileDescriptor.out), true); 
89               InputStream in = new BufferedInputStream( 
90                       new FileInputStream(FileDescriptor.in)); 
91               StandardIO stdio = new StandardIO(out, in, err); 
92               Namespace.getNamespace().registerDefaultInstanceForClass( 
93                       StandardIO.class, 
94                       stdio); 
95    
96           } 
97       } 
98    
99       public PrintStream getOut() { 
100          return _out; 
101      } 
102   
103      public InputStream getIn() { 
104          return _in; 
105      } 
106   
107      public PrintStream getErr() { 
108          return _err; 
109      } 
110   
111  } 
112   
113