KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > GenericOutput


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 import java.io.PrintStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29
30 /**
31     non-public helper classes
32  */

33 class NullOutput implements IOutput
34 {
35     NullOutput( )
36     {
37     }
38
39     public void print( String JavaDoc msg )
40     {
41         // do nothing
42
}
43
44     public void print( Object JavaDoc msg )
45     {
46         // do nothing
47
}
48
49     public void println( String JavaDoc msg )
50     {
51         // do nothing
52
}
53
54     public void println( Object JavaDoc msg )
55     {
56         // do nothing
57
}
58
59     public void close()
60     {
61         // do nothing
62
}
63
64     public void flush()
65     {
66         // do nothing
67
}
68 }
69
70
71 class Output implements IOutput
72 {
73     OutputStream JavaDoc mOutputStream = null;
74     PrintStream JavaDoc mPrint = null;
75     boolean mCloseWhenDone = false;
76
77     public Output( OutputStream JavaDoc outputStream, boolean closeWhenDone )
78            throws Exception JavaDoc
79     {
80     mPrint = new PrintStream JavaDoc( outputStream );
81
82     // don't assign these unless we were successful creating mPrintWriter
83
mOutputStream = outputStream;
84     mCloseWhenDone = closeWhenDone;
85     }
86
87     /**
88     Call to release/close the output stream when done.
89     This prevents files from remaining open for a long time.
90     */

91     public void close()
92     {
93     if ( mCloseWhenDone )
94     {
95         try
96         {
97         mOutputStream.close();
98         }
99         catch( Exception JavaDoc e )
100         {
101         }
102     }
103     mOutputStream = null;
104     mPrint = null;
105     mCloseWhenDone = false;
106     }
107
108
109     public void print( String JavaDoc msg )
110     {
111     mPrint.print( msg );
112     }
113
114
115     public void print( Object JavaDoc msg )
116     {
117     mPrint.println( msg.toString() );
118     }
119
120
121     public void println( String JavaDoc msg )
122     {
123     mPrint.println( msg );
124     }
125
126
127     public void println( Object JavaDoc msg )
128     {
129     mPrint.println( msg.toString() );
130     }
131
132
133     public void flush()
134     {
135         try
136     {
137         mOutputStream.flush();
138     }
139     catch( Exception JavaDoc e )
140     {
141     }
142     }
143 }
144
145
146 /**
147     A base class for all different types of outputs.
148  */

149 public abstract class GenericOutput implements IOutput
150 {
151     private IOutput mOutput = null;
152
153
154     /**
155         A generic constructor with an OutputStream object and a boolean.
156
157         @param out is the OutputStream object to be set.
158         @param closeWhenDone is the boolen tag.
159      */

160     protected GenericOutput( OutputStream JavaDoc out, boolean closeWhenDone )
161     {
162         try
163         {
164             if ( out != null )
165             {
166                 mOutput = new Output( out, closeWhenDone );
167             }
168             else
169             {
170                 mOutput = new NullOutput();
171             }
172
173         }
174         catch ( Exception JavaDoc e )
175         {
176             mOutput = new NullOutput();
177         }
178     }
179
180
181     /**
182         Print a string.
183
184         @param s - The String to be printed
185      */

186     public void print( String JavaDoc message )
187     {
188         mOutput.print( message );
189     }
190
191
192     /**
193         Print a string.
194
195         @param msg - object on which toString() will be called.
196      */

197     public void print( Object JavaDoc msg )
198     {
199         mOutput.print( msg.toString() );
200     }
201
202
203     /**
204         Print a String and then terminate the line.
205
206         @param s - The String to be printed
207      */

208     public void println( String JavaDoc message )
209     {
210         mOutput.println( message );
211     }
212
213
214     /**
215         Print a String and then terminate the line.
216
217         @param msg - object on which toString() will be called.
218      */

219     public void println( Object JavaDoc msg )
220     {
221         mOutput.println( msg.toString() );
222     }
223
224
225     /**
226         Closes the underlying output stream.
227      */

228     public void close()
229     {
230         mOutput.close();
231         mOutput = new NullOutput();
232     }
233
234
235     /**
236         Flushes this output stream and forces any buffered output bytes to
237         be written out.
238      */

239     public void flush()
240     {
241         mOutput.flush();
242     }
243
244
245 }
246
Popular Tags