KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > hmux > HmuxStreamWrapper


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.hmux;
31
32 import com.caucho.log.Log;
33 import com.caucho.vfs.StreamImpl;
34
35 import java.io.IOException JavaDoc;
36 import java.net.SocketException JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 /**
41  * Facade to HmuxStream to properly handle the close.
42  */

43 class HmuxStreamWrapper extends StreamImpl {
44   private static final Logger JavaDoc log = Log.open(HmuxStream.class);
45   
46   private HmuxStream _stream;
47
48   /**
49    * Create a new HMUX stream.
50    */

51   HmuxStreamWrapper(HmuxStream stream)
52     throws IOException JavaDoc
53   {
54     _stream = stream;
55   }
56
57   /**
58    * Set if this should be an SSL connection.
59    */

60   public void setSSL(boolean isSSL)
61   {
62     _stream.setSSL(isSSL);
63   }
64
65   /**
66    * Set if this should be an SSL connection.
67    */

68   public boolean isSSL()
69   {
70     return _stream.isSSL();
71   }
72
73   /**
74    * Sets the method
75    */

76   public void setMethod(String JavaDoc method)
77   {
78     _stream.setMethod(method);
79   }
80
81   /**
82    * Sets true if we're only interested in the head.
83    */

84   public void setHead(boolean isHead)
85   {
86     _stream.setHead(isHead);
87   }
88
89   /**
90    * Returns the stream's host.
91    */

92   public String JavaDoc getHost()
93   {
94     return _stream.getHost();
95   }
96
97   /**
98    * Returns the stream's port.
99    */

100   public int getPort()
101   {
102     return _stream.getPort();
103   }
104   
105   /**
106    * Returns a header from the response returned from the HMUX server.
107    *
108    * @param name name of the header
109    * @return the header value.
110    */

111   public Object JavaDoc getAttribute(String JavaDoc name)
112     throws IOException JavaDoc
113   {
114     if (_stream != null)
115       return _stream.getAttribute(name);
116     else
117       return null;
118   }
119
120   /**
121    * Returns an iterator of the returned header names.
122    */

123   public Iterator JavaDoc getAttributeNames()
124     throws IOException JavaDoc
125   {
126     if (_stream != null)
127       return _stream.getAttributeNames();
128     else
129       return null;
130   }
131
132   /**
133    * Sets a header for the request.
134    */

135   public void setAttribute(String JavaDoc name, Object JavaDoc value)
136   {
137     if (_stream != null)
138       _stream.setAttribute(name, value);
139   }
140
141   /**
142    * Remove a header for the request.
143    */

144   public void removeAttribute(String JavaDoc name)
145   {
146     if (_stream != null)
147       _stream.removeAttribute(name);
148   }
149
150   /**
151    * Sets the timeout.
152    */

153   public void setSocketTimeout(long timeout)
154     throws SocketException JavaDoc
155   {
156     if (_stream != null)
157       _stream.setSocketTimeout(timeout);
158   }
159
160   /**
161    * The stream is always writable (?)
162    */

163   public boolean canWrite()
164   {
165     if (_stream != null)
166       return _stream.canWrite();
167     else
168       return false;
169   }
170
171   /**
172    * Writes a buffer to the underlying stream.
173    *
174    * @param buffer the byte array to write.
175    * @param offset the offset into the byte array.
176    * @param length the number of bytes to write.
177    * @param isEnd true when the write is flushing a close.
178    */

179   public void write(byte []buf, int offset, int length, boolean isEnd)
180     throws IOException JavaDoc
181   {
182     if (_stream != null)
183       _stream.write(buf, offset, length, isEnd);
184   }
185
186   /**
187    * The stream is readable.
188    */

189   public boolean canRead()
190   {
191     if (_stream != null)
192       return _stream.canRead();
193     else
194       return false;
195   }
196
197   /**
198    * Read data from the connection. If the request hasn't yet been sent
199    * to the server, send it.
200    */

201   public int read(byte []buf, int offset, int length) throws IOException JavaDoc
202   {
203     if (_stream != null)
204       return _stream.read(buf, offset, length);
205     else
206       return -1;
207   }
208
209   /**
210    * Returns the bytes still available.
211    */

212   public int getAvailable() throws IOException JavaDoc
213   {
214     if (_stream != null)
215       return _stream.getAvailable();
216     else
217       return -1;
218   }
219
220   /**
221    * Close the connection.
222    */

223   public void close() throws IOException JavaDoc
224   {
225     HmuxStream stream = _stream;
226     _stream = null;
227
228     if (stream != null)
229       stream.close();
230   }
231 }
232
Popular Tags