KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > output > DemuxOutputStream


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io.output;
17
18 import java.io.IOException JavaDoc;
19 import java.io.OutputStream JavaDoc;
20
21 /**
22  * Data written to this stream is forwarded to a stream that has been associated
23  * with this thread.
24  *
25  * @author <a HREF="mailto:peter@apache.org">Peter Donald</a>
26  * @version $Revision: 1.5 $ $Date: 2004/02/23 04:40:29 $
27  */

28 public class DemuxOutputStream
29     extends OutputStream JavaDoc
30 {
31     private InheritableThreadLocal JavaDoc m_streams = new InheritableThreadLocal JavaDoc();
32
33     /**
34      * Bind the specified stream to the current thread.
35      *
36      * @param output the stream to bind
37      * @return the OutputStream that was previously active
38      */

39     public OutputStream JavaDoc bindStream( OutputStream JavaDoc output )
40     {
41         OutputStream JavaDoc stream = getStream();
42         m_streams.set( output );
43         return stream;
44     }
45
46     /**
47      * Closes stream associated with current thread.
48      *
49      * @throws IOException if an error occurs
50      */

51     public void close()
52         throws IOException JavaDoc
53     {
54         OutputStream JavaDoc output = getStream();
55         if( null != output )
56         {
57             output.close();
58         }
59     }
60
61     /**
62      * Flushes stream associated with current thread.
63      *
64      * @throws IOException if an error occurs
65      */

66     public void flush()
67         throws IOException JavaDoc
68     {
69         OutputStream JavaDoc output = getStream();
70         if( null != output )
71         {
72             output.flush();
73         }
74     }
75
76     /**
77      * Writes byte to stream associated with current thread.
78      *
79      * @param ch the byte to write to stream
80      * @throws IOException if an error occurs
81      */

82     public void write( int ch )
83         throws IOException JavaDoc
84     {
85         OutputStream JavaDoc output = getStream();
86         if( null != output )
87         {
88             output.write( ch );
89         }
90     }
91
92     /**
93      * Utility method to retrieve stream bound to current thread (if any).
94      */

95     private OutputStream JavaDoc getStream()
96     {
97         return (OutputStream JavaDoc)m_streams.get();
98     }
99 }
100
Popular Tags