1 // ========================================================================
2 // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3 // ------------------------------------------------------------------------
4 // All rights reserved. This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v1.0
6 // and Apache License v2.0 which accompanies this distribution.
7 // The Eclipse Public License is available at
8 // http://www.eclipse.org/legal/epl-v10.html
9 // The Apache License v2.0 is available at
10 // http://www.opensource.org/licenses/apache2.0.php
11 // You may elect to redistribute this code under either of these licenses.
12 // ========================================================================
13
14
15 package org.eclipse.jetty.io.bio;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.OutputStream;
20 import java.net.SocketTimeoutException;
21
22 import org.eclipse.jetty.io.Buffer;
23 import org.eclipse.jetty.io.EndPoint;
24
25 public class StreamEndPoint implements EndPoint
26 {
27 InputStream _in;
28 OutputStream _out;
29 int _maxIdleTime;
30 boolean _ishut;
31 boolean _oshut;
32
33 /**
34 *
35 */
36 public StreamEndPoint(InputStream in, OutputStream out)
37 {
38 _in=in;
39 _out=out;
40 }
41
42 public boolean isBlocking()
43 {
44 return true;
45 }
46
47 public boolean blockReadable(long millisecs) throws IOException
48 {
49 return true;
50 }
51
52 public boolean blockWritable(long millisecs) throws IOException
53 {
54 return true;
55 }
56
57 /*
58 * @see org.eclipse.io.BufferIO#isOpen()
59 */
60 public boolean isOpen()
61 {
62 return _in!=null;
63 }
64
65 /*
66 * @see org.eclipse.io.BufferIO#isOpen()
67 */
68 public final boolean isClosed()
69 {
70 return !isOpen();
71 }
72
73 public void shutdownOutput() throws IOException
74 {
75 _oshut = true;
76 if (_ishut && _out!=null)
77 _out.close();
78 }
79
80 public boolean isInputShutdown()
81 {
82 return _ishut;
83 }
84
85 public void shutdownInput() throws IOException
86 {
87 _ishut = true;
88 if (_oshut&&_in!=null)
89 _in.close();
90 }
91
92 public boolean isOutputShutdown()
93 {
94 return _oshut;
95 }
96
97 /*
98 * @see org.eclipse.io.BufferIO#close()
99 */
100 public void close() throws IOException
101 {
102 if (_in!=null)
103 _in.close();
104 _in=null;
105 if (_out!=null)
106 _out.close();
107 _out=null;
108 }
109
110 protected void idleExpired() throws IOException
111 {
112 if (_in!=null)
113 _in.close();
114 }
115
116 /* (non-Javadoc)
117 * @see org.eclipse.io.BufferIO#fill(org.eclipse.io.Buffer)
118 */
119 public int fill(Buffer buffer) throws IOException
120 {
121 if (_ishut)
122 return -1;
123 if (_in==null)
124 return 0;
125
126 int space=buffer.space();
127 if (space<=0)
128 {
129 if (buffer.hasContent())
130 return 0;
131 throw new IOException("FULL");
132 }
133
134 try
135 {
136 int filled=buffer.readFrom(_in, space);
137 if (filled<0)
138 shutdownInput();
139 return filled;
140 }
141 catch(SocketTimeoutException e)
142 {
143 idleExpired();
144 return -1;
145 }
146 }
147
148 /* (non-Javadoc)
149 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer)
150 */
151 public int flush(Buffer buffer) throws IOException
152 {
153 if (_oshut)
154 return -1;
155 if (_out==null)
156 return 0;
157 int length=buffer.length();
158 if (length>0)
159 buffer.writeTo(_out);
160 if (!buffer.isImmutable())
161 buffer.clear();
162 return length;
163 }
164
165 /* (non-Javadoc)
166 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
167 */
168 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
169 {
170 int len=0;
171
172 if (header!=null)
173 {
174 int tw=header.length();
175 if (tw>0)
176 {
177 int f=flush(header);
178 len=f;
179 if (f<tw)
180 return len;
181 }
182 }
183
184 if (buffer!=null)
185 {
186 int tw=buffer.length();
187 if (tw>0)
188 {
189 int f=flush(buffer);
190 if (f<0)
191 return len>0?len:f;
192 len+=f;
193 if (f<tw)
194 return len;
195 }
196 }
197
198 if (trailer!=null)
199 {
200 int tw=trailer.length();
201 if (tw>0)
202 {
203 int f=flush(trailer);
204 if (f<0)
205 return len>0?len:f;
206 len+=f;
207 }
208 }
209 return len;
210 }
211
212 /* ------------------------------------------------------------ */
213 /*
214 * @see org.eclipse.io.EndPoint#getLocalAddr()
215 */
216 public String getLocalAddr()
217 {
218 return null;
219 }
220
221 /* ------------------------------------------------------------ */
222 /*
223 * @see org.eclipse.io.EndPoint#getLocalHost()
224 */
225 public String getLocalHost()
226 {
227 return null;
228 }
229
230 /* ------------------------------------------------------------ */
231 /*
232 * @see org.eclipse.io.EndPoint#getLocalPort()
233 */
234 public int getLocalPort()
235 {
236 return 0;
237 }
238
239 /* ------------------------------------------------------------ */
240 /*
241 * @see org.eclipse.io.EndPoint#getRemoteAddr()
242 */
243 public String getRemoteAddr()
244 {
245 return null;
246 }
247
248 /* ------------------------------------------------------------ */
249 /*
250 * @see org.eclipse.io.EndPoint#getRemoteHost()
251 */
252 public String getRemoteHost()
253 {
254 return null;
255 }
256
257 /* ------------------------------------------------------------ */
258 /*
259 * @see org.eclipse.io.EndPoint#getRemotePort()
260 */
261 public int getRemotePort()
262 {
263 return 0;
264 }
265
266 /* ------------------------------------------------------------ */
267 /*
268 * @see org.eclipse.io.EndPoint#getConnection()
269 */
270 public Object getTransport()
271 {
272 return null;
273 }
274
275 /* ------------------------------------------------------------ */
276 public InputStream getInputStream()
277 {
278 return _in;
279 }
280
281 /* ------------------------------------------------------------ */
282 public void setInputStream(InputStream in)
283 {
284 _in=in;
285 }
286
287 /* ------------------------------------------------------------ */
288 public OutputStream getOutputStream()
289 {
290 return _out;
291 }
292
293 /* ------------------------------------------------------------ */
294 public void setOutputStream(OutputStream out)
295 {
296 _out=out;
297 }
298
299
300 /* ------------------------------------------------------------ */
301 public void flush()
302 throws IOException
303 {
304 if (_out != null)
305 _out.flush();
306 }
307
308 /* ------------------------------------------------------------ */
309 public int getMaxIdleTime()
310 {
311 return _maxIdleTime;
312 }
313
314 /* ------------------------------------------------------------ */
315 public void setMaxIdleTime(int timeMs) throws IOException
316 {
317 _maxIdleTime=timeMs;
318 }
319
320 }