KickJava   Java API By Example, From Geeks To Geeks.

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


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.Writer JavaDoc;
23 import java.io.PrintStream JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 final class OutputStreamWriter extends Writer JavaDoc {
27
28     private PrintStream JavaDoc out;
29
30     /**
31      * Create an OutputStreamWriter that uses the default character encoding.
32      *
33      * @param out An OutputStream
34      */

35     public OutputStreamWriter(PrintStream JavaDoc out) {
36         super(out);
37         if (out == null)
38             throw new NullPointerException JavaDoc();
39         this.out = out;
40     }
41
42     /** Check to make sure that the stream has not been closed */
43     private void ensureOpen() throws IOException JavaDoc {
44         if (out == null)
45             throw new IOException JavaDoc();
46     }
47     
48     /**
49      * Write a single character.
50      *
51      * @exception IOException If an I/O error occurs
52      */

53     public void write(int c) throws IOException JavaDoc {
54         char cbuf[] = new char[1];
55         cbuf[0] = (char) c;
56         write(cbuf, 0, 1);
57     }
58
59     /**
60      * Write a portion of an array of characters.
61      *
62      * @param cbuf Buffer of characters
63      * @param off Offset from which to start writing characters
64      * @param len Number of characters to write
65      *
66      * @exception IOException If an I/O error occurs
67      */

68     public void write(char cbuf[], int off, int len) throws IOException JavaDoc {
69         synchronized (lock) {
70             if ((off == 0) && (len == cbuf.length)) {
71                 out.print(cbuf);
72             } else {
73                 char[] chars = new char[len];
74                 System.arraycopy(cbuf, off, chars, 0, len);
75                 out.print(chars);
76             }
77         }
78     }
79
80     /**
81      * Write a portion of a string.
82      *
83      * @param str A String
84      * @param off Offset from which to start writing characters
85      * @param len Number of characters to write
86      *
87      * @exception IOException If an I/O error occurs
88      */

89     public void write(String JavaDoc str, int off, int len) throws IOException JavaDoc {
90         char[] chars = new char[len];
91         str.getChars(off, off + len, chars, 0);
92         out.print(chars);
93     }
94
95     /**
96      * Flush the stream.
97      *
98      * @exception IOException If an I/O error occurs
99      */

100     public void flush() {
101         synchronized (lock) {
102             if (out == null)
103                 return;
104             out.flush();
105         }
106     }
107
108     /**
109      * Close the stream.
110      *
111      * @exception IOException If an I/O error occurs
112      */

113     public void close() {
114         synchronized (lock) {
115             if (out == null)
116                 return;
117             flush();
118             out.close();
119             out = null;
120         }
121     }
122
123 }
124
Popular Tags