KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > vfs > HttpStreamWrapper


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.vfs;
30
31 import java.io.IOException JavaDoc;
32 import java.net.SocketException JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.logging.Logger JavaDoc;
35
36 /**
37  * Facade to HttpStream to properly handle the close.
38  */

39 class HttpStreamWrapper extends StreamImpl
40 {
41   private static final Logger JavaDoc log
42     = Logger.getLogger(HttpStream.class.getName());
43   
44   private HttpStream _stream;
45
46   /**
47    * Create a new HTTP stream.
48    */

49   HttpStreamWrapper(HttpStream stream)
50     throws IOException JavaDoc
51   {
52     _stream = stream;
53   }
54
55   /**
56    * Set if this should be an SSL connection.
57    */

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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