KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > hessian > mux > MuxOutputStream


1 /*
2  * Copyright (c) 2001-2004 Caucho Technology, Inc. All rights reserved.
3  *
4  * The Apache Software License, Version 1.1
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Caucho Technology (http://www.caucho.com/)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The names "Hessian", "Resin", and "Caucho" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * info@caucho.com.
29  *
30  * 5. Products derived from this software may not be called "Resin"
31  * nor may "Resin" appear in their names without prior written
32  * permission of Caucho Technology.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL CAUCHO TECHNOLOGY OR ITS CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
39  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
42  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
43  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
44  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * @author Scott Ferguson
47  */

48
49 package com.caucho.hessian.mux;
50
51 import java.io.IOException JavaDoc;
52 import java.io.OutputStream JavaDoc;
53
54 /**
55  * Output stream to a specific channel.
56  */

57 public class MuxOutputStream extends OutputStream JavaDoc {
58   private MuxServer server;
59   private int channel;
60   private OutputStream JavaDoc os;
61
62   /**
63    * Null argument constructor.
64    */

65   public MuxOutputStream()
66   {
67   }
68
69   /**
70    * Initialize the multiplexor with input and output streams.
71    */

72   protected void init(MuxServer server, int channel)
73     throws IOException JavaDoc
74   {
75     this.server = server;
76     this.channel = channel;
77     this.os = null;
78   }
79
80   /**
81    * Gets the raw output stream. Clients will normally not call
82    * this.
83    */

84   protected OutputStream JavaDoc getOutputStream()
85     throws IOException JavaDoc
86   {
87     if (os == null && server != null)
88       os = server.writeChannel(channel);
89     
90     return os;
91   }
92
93   /**
94    * Gets the channel of the connection.
95    */

96   public int getChannel()
97   {
98     return channel;
99   }
100
101   /**
102    * Writes a URL to the stream.
103    */

104   public void writeURL(String JavaDoc url)
105     throws IOException JavaDoc
106   {
107     writeUTF('U', url);
108   }
109   
110   /**
111    * Writes a data byte to the output stream.
112    */

113   public void write(int ch)
114     throws IOException JavaDoc
115   {
116     OutputStream JavaDoc os = getOutputStream();
117     
118     os.write('D');
119     os.write(0);
120     os.write(1);
121     os.write(ch);
122   }
123
124   /**
125    * Writes data to the output stream.
126    */

127   public void write(byte []buffer, int offset, int length)
128     throws IOException JavaDoc
129   {
130     OutputStream JavaDoc os = getOutputStream();
131     
132     for (; length > 0x8000; length -= 0x8000) {
133       os.write('D');
134       os.write(0x80);
135       os.write(0x00);
136       os.write(buffer, offset, 0x8000);
137       
138       offset += 0x8000;
139     }
140
141     os.write('D');
142     os.write(length >> 8);
143     os.write(length);
144     os.write(buffer, offset, length);
145   }
146
147   /**
148    * Flush data to the output stream.
149    */

150   public void yield()
151     throws IOException JavaDoc
152   {
153     OutputStream JavaDoc os = this.os;
154     this.os = null;
155
156     if (os != null)
157       server.yield(channel);
158   }
159
160   /**
161    * Flush data to the output stream.
162    */

163   public void flush()
164     throws IOException JavaDoc
165   {
166     OutputStream JavaDoc os = this.os;
167     this.os = null;
168
169     if (os != null)
170       server.flush(channel);
171   }
172
173   /**
174    * Complete writing to the stream, closing the channel.
175    */

176   public void close()
177     throws IOException JavaDoc
178   {
179     if (server != null) {
180       OutputStream JavaDoc os = getOutputStream();
181       this.os = null;
182       
183       MuxServer server = this.server;
184       this.server = null;
185
186       server.close(channel);
187     }
188   }
189
190   /**
191    * Writes a UTF-8 string.
192    *
193    * @param code the HMUX code identifying the string
194    * @param string the string to write
195    */

196   protected void writeUTF(int code, String JavaDoc string)
197     throws IOException JavaDoc
198   {
199     OutputStream JavaDoc os = getOutputStream();
200     
201     os.write(code);
202     
203     int charLength = string.length();
204
205     int length = 0;
206     for (int i = 0; i < charLength; i++) {
207       char ch = string.charAt(i);
208
209       if (ch < 0x80)
210         length++;
211       else if (ch < 0x800)
212         length += 2;
213       else
214         length += 3;
215     }
216     
217     os.write(length >> 8);
218     os.write(length);
219     
220     for (int i = 0; i < length; i++) {
221       char ch = string.charAt(i);
222
223       if (ch < 0x80)
224         os.write(ch);
225       else if (ch < 0x800) {
226         os.write(0xc0 + (ch >> 6) & 0x1f);
227         os.write(0x80 + (ch & 0x3f));
228       }
229       else {
230         os.write(0xe0 + (ch >> 12) & 0xf);
231         os.write(0x80 + ((ch >> 6) & 0x3f));
232         os.write(0x80 + (ch & 0x3f));
233       }
234     }
235   }
236 }
237
Popular Tags