KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > WriterPrintStream


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.execution;
21
22 import java.io.IOException JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 final class WriterPrintStream extends PrintStream JavaDoc {
27
28     private boolean stdOut;
29
30     /**
31      * Create a new print stream. This stream will not flush automatically.
32      *
33      * @param out The output stream to which values and objects will be
34      * printed
35      *
36      */

37     public WriterPrintStream(OutputStream JavaDoc out, boolean stdOut) {
38     super(out, true);
39
40         this.stdOut = stdOut;
41     }
42
43     /** Empty */
44     public void close() {
45     }
46
47     public void flush() {
48         try {
49             if (stdOut) {
50                 ExecutionEngine.getTaskIOs().getOut().flush();
51             } else {
52                 ExecutionEngine.getTaskIOs().getErr().flush();
53             }
54         } catch (IOException JavaDoc e) {
55             setError();
56         }
57     }
58     
59     private void write(String JavaDoc s) {
60         try {
61             if (stdOut) {
62                 ExecutionEngine.getTaskIOs().getOut().write(s);
63             } else {
64                 ExecutionEngine.getTaskIOs().getErr().write(s);
65             }
66         } catch (IOException JavaDoc e) {
67             setError();
68         }
69     }
70
71     /**
72      * Print a boolean value. The string produced by <code>{@link
73      * java.lang.String#valueOf(boolean)}</code> is translated into bytes
74      * according to the platform's default character encoding, and these bytes
75      * are written in exactly the manner of the
76      * <code>{@link #write(int)}</code> method.
77      *
78      * @param b The <code>boolean</code> to be printed
79      */

80     public void print(boolean b) {
81     write(b ? "true" : "false"); // NOI18N
82
}
83
84     /**
85      * Print a character. The character is translated into one or more bytes
86      * according to the platform's default character encoding, and these bytes
87      * are written in exactly the manner of the
88      * <code>{@link #write(int)}</code> method.
89      *
90      * @param c The <code>char</code> to be printed
91      */

92     public void print(char c) {
93         try {
94             if (stdOut) {
95                 ExecutionEngine.getTaskIOs().getOut().write(c);
96             } else {
97                 ExecutionEngine.getTaskIOs().getErr().write(c);
98             }
99         } catch (IOException JavaDoc e) {
100             setError();
101         }
102     }
103
104     /**
105      * Print an integer. The string produced by <code>{@link
106      * java.lang.String#valueOf(int)}</code> is translated into bytes
107      * according to the platform's default character encoding, and these bytes
108      * are written in exactly the manner of the
109      * <code>{@link #write(int)}</code> method.
110      *
111      * @param i The <code>int</code> to be printed
112      * @see java.lang.Integer#toString(int)
113      */

114     public void print(int i) {
115     write(String.valueOf(i));
116     }
117
118     /**
119      * Print a long integer. The string produced by <code>{@link
120      * java.lang.String#valueOf(long)}</code> is translated into bytes
121      * according to the platform's default character encoding, and these bytes
122      * are written in exactly the manner of the
123      * <code>{@link #write(int)}</code> method.
124      *
125      * @param l The <code>long</code> to be printed
126      * @see java.lang.Long#toString(long)
127      */

128     public void print(long l) {
129     write(String.valueOf(l));
130     }
131
132     /**
133      * Print a floating-point number. The string produced by <code>{@link
134      * java.lang.String#valueOf(float)}</code> is translated into bytes
135      * according to the platform's default character encoding, and these bytes
136      * are written in exactly the manner of the
137      * <code>{@link #write(int)}</code> method.
138      *
139      * @param f The <code>float</code> to be printed
140      * @see java.lang.Float#toString(float)
141      */

142     public void print(float f) {
143     write(String.valueOf(f));
144     }
145
146     /**
147      * Print a double-precision floating-point number. The string produced by
148      * <code>{@link java.lang.String#valueOf(double)}</code> is translated into
149      * bytes according to the platform's default character encoding, and these
150      * bytes are written in exactly the manner of the <code>{@link
151      * #write(int)}</code> method.
152      *
153      * @param d The <code>double</code> to be printed
154      * @see java.lang.Double#toString(double)
155      */

156     public void print(double d) {
157     write(String.valueOf(d));
158     }
159
160     /**
161      * Print an array of characters. The characters are converted into bytes
162      * according to the platform's default character encoding, and these bytes
163      * are written in exactly the manner of the
164      * <code>{@link #write(int)}</code> method.
165      *
166      * @param s The array of chars to be printed
167      *
168      * @throws NullPointerException If <code>s</code> is <code>null</code>
169      */

170     public void print(char s[]) {
171         try {
172             if (stdOut) {
173                 ExecutionEngine.getTaskIOs().getOut().write(s);
174             } else {
175                 ExecutionEngine.getTaskIOs().getErr().write(s);
176             }
177         } catch (IOException JavaDoc e) {
178             setError();
179         }
180     }
181
182     /**
183      * Print a string. If the argument is <code>null</code> then the string
184      * <code>"null"</code> is printed. Otherwise, the string's characters are
185      * converted into bytes according to the platform's default character
186      * encoding, and these bytes are written in exactly the manner of the
187      * <code>{@link #write(int)}</code> method.
188      *
189      * @param s The <code>String</code> to be printed
190      */

191     public void print(String JavaDoc s) {
192     if (s == null) {
193         s = "null"; // NOI18N
194
}
195     write(s);
196     }
197
198     /**
199      * Print an object. The string produced by the <code>{@link
200      * java.lang.String#valueOf(Object)}</code> method is translated into bytes
201      * according to the platform's default character encoding, and these bytes
202      * are written in exactly the manner of the
203      * <code>{@link #write(int)}</code> method.
204      *
205      * @param obj The <code>Object</code> to be printed
206      * @see java.lang.Object#toString()
207      */

208     public void print(Object JavaDoc obj) {
209     write(String.valueOf(obj));
210     }
211
212
213     /* Methods that do terminate lines */
214
215     /**
216      * Terminate the current line by writing the line separator string. The
217      * line separator string is defined by the system property
218      * <code>line.separator</code>, and is not necessarily a single newline
219      * character (<code>'\n'</code>).
220      */

221     public void println() {
222         print(getNewLine());
223     }
224
225     /**
226      * Print a boolean and then terminate the line. This method behaves as
227      * though it invokes <code>{@link #print(boolean)}</code> and then
228      * <code>{@link #println()}</code>.
229      *
230      * @param x The <code>boolean</code> to be printed
231      */

232     public void println(boolean x) {
233         String JavaDoc out = (x ? "true" : "false"); // NOI18N
234
write(out.concat(getNewLine()));
235     }
236
237     /**
238      * Print a character and then terminate the line. This method behaves as
239      * though it invokes <code>{@link #print(char)}</code> and then
240      * <code>{@link #println()}</code>.
241      *
242      * @param x The <code>char</code> to be printed.
243      */

244     public void println(char x) {
245         String JavaDoc nline = getNewLine();
246         int nlinelen = nline.length();
247         char[] tmp = new char[nlinelen + 1];
248         tmp[0] = x;
249         for (int i = 0; i < nlinelen; i++) {
250             tmp[i + 1] = nline.charAt(i);
251         }
252         try {
253             if (stdOut) {
254                 ExecutionEngine.getTaskIOs().getOut().write(tmp);
255             } else {
256                 ExecutionEngine.getTaskIOs().getErr().write(tmp);
257             }
258         } catch (IOException JavaDoc e) {
259             setError();
260         }
261     }
262
263     /**
264      * Print an integer and then terminate the line. This method behaves as
265      * though it invokes <code>{@link #print(int)}</code> and then
266      * <code>{@link #println()}</code>.
267      *
268      * @param x The <code>int</code> to be printed.
269      */

270     public void println(int x) {
271     write(String.valueOf(x).concat(getNewLine()));
272     }
273
274     /**
275      * Print a long and then terminate the line. This method behaves as
276      * though it invokes <code>{@link #print(long)}</code> and then
277      * <code>{@link #println()}</code>.
278      *
279      * @param x a The <code>long</code> to be printed.
280      */

281     public void println(long x) {
282     write(String.valueOf(x).concat(getNewLine()));
283     }
284
285     /**
286      * Print a float and then terminate the line. This method behaves as
287      * though it invokes <code>{@link #print(float)}</code> and then
288      * <code>{@link #println()}</code>.
289      *
290      * @param x The <code>float</code> to be printed.
291      */

292     public void println(float x) {
293     write(String.valueOf(x).concat(getNewLine()));
294     }
295
296     /**
297      * Print a double and then terminate the line. This method behaves as
298      * though it invokes <code>{@link #print(double)}</code> and then
299      * <code>{@link #println()}</code>.
300      *
301      * @param x The <code>double</code> to be printed.
302      */

303     public void println(double x) {
304     write(String.valueOf(x).concat(getNewLine()));
305     }
306
307     /**
308      * Print an array of characters and then terminate the line. This method
309      * behaves as though it invokes <code>{@link #print(char[])}</code> and
310      * then <code>{@link #println()}</code>.
311      *
312      * @param x an array of chars to print.
313      */

314     public void println(char x[]) {
315         String JavaDoc nline = getNewLine();
316         int nlinelen = nline.length();
317         char[] tmp = new char[x.length + nlinelen];
318         System.arraycopy(x, 0, tmp, 0, x.length);
319         for (int i = 0; i < nlinelen; i++) {
320             tmp[x.length + i] = nline.charAt(i);
321         }
322         x = null;
323         try {
324             if (stdOut) {
325                 ExecutionEngine.getTaskIOs().getOut().write(tmp);
326             } else {
327                 ExecutionEngine.getTaskIOs().getErr().write(tmp);
328             }
329         } catch (IOException JavaDoc e) {
330             setError();
331         }
332     }
333
334     /**
335      * Print a String and then terminate the line. This method behaves as
336      * though it invokes <code>{@link #print(String)}</code> and then
337      * <code>{@link #println()}</code>.
338      *
339      * @param x The <code>String</code> to be printed.
340      */

341     public void println(String JavaDoc x) {
342         if (x == null) {
343             x = "null"; // NOI18N
344
}
345         print(x.concat(getNewLine()));
346     }
347
348     /**
349      * Print an Object and then terminate the line. This method behaves as
350      * though it invokes <code>{@link #print(Object)}</code> and then
351      * <code>{@link #println()}</code>.
352      *
353      * @param x The <code>Object</code> to be printed.
354      */

355     public void println(Object JavaDoc x) {
356         if (x == null) {
357         print("null".concat(getNewLine())); // NOI18N
358
} else {
359         String JavaDoc s = x.toString();
360         if(s == null) {
361         print("<null>".concat(getNewLine())); // NOI18N
362
} else {
363             print(s.concat(getNewLine()));
364         }
365     }
366     }
367     
368     private static String JavaDoc newLine;
369     private static String JavaDoc getNewLine() {
370         if (newLine == null) {
371             newLine = System.getProperty("line.separator");
372         }
373         return newLine;
374     }
375 }
376
Popular Tags