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 /**
26 *
27 *
28 * To change the template for this generated type comment go to
29 * Window - Preferences - Java - Code Generation - Code and Comments
30 */
31 public class StreamEndPoint implements EndPoint
32 {
33 InputStream _in;
34 OutputStream _out;
35 int _maxIdleTime;
36
37 /**
38 *
39 */
40 public StreamEndPoint(InputStream in, OutputStream out)
41 {
42 _in=in;
43 _out=out;
44 }
45
46 public boolean isBlocking()
47 {
48 return true;
49 }
50
51 public boolean blockReadable(long millisecs) throws IOException
52 {
53 return true;
54 }
55
56 public boolean blockWritable(long millisecs) throws IOException
57 {
58 return true;
59 }
60
61 /*
62 * @see org.eclipse.io.BufferIO#isOpen()
63 */
64 public boolean isOpen()
65 {
66 return _in!=null;
67 }
68
69 /*
70 * @see org.eclipse.io.BufferIO#isOpen()
71 */
72 public final boolean isClosed()
73 {
74 return !isOpen();
75 }
76
77 public void shutdownOutput() throws IOException
78 {
79 }
80
81 public boolean isInputShutdown()
82 {
83 return !isOpen();
84 }
85
86 public void shutdownInput() throws IOException
87 {
88 }
89
90 public boolean isOutputShutdown()
91 {
92 return !isOpen();
93 }
94
95 /*
96 * @see org.eclipse.io.BufferIO#close()
97 */
98 public void close() throws IOException
99 {
100 if (_in!=null)
101 _in.close();
102 _in=null;
103 if (_out!=null)
104 _out.close();
105 _out=null;
106 }
107
108 protected void idleExpired() throws IOException
109 {
110 _in.close();
111 }
112
113 /* (non-Javadoc)
114 * @see org.eclipse.io.BufferIO#fill(org.eclipse.io.Buffer)
115 */
116 public int fill(Buffer buffer) throws IOException
117 {
118 // TODO handle null array()
119 if (_in==null)
120 return 0;
121
122 int space=buffer.space();
123 if (space<=0)
124 {
125 if (buffer.hasContent())
126 return 0;
127 throw new IOException("FULL");
128 }
129
130 try
131 {
132 return buffer.readFrom(_in,space);
133 }
134 catch(SocketTimeoutException e)
135 {
136 idleExpired();
137 return -1;
138 }
139 }
140
141 /* (non-Javadoc)
142 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer)
143 */
144 public int flush(Buffer buffer) throws IOException
145 {
146 // TODO handle null array()
147 if (_out==null)
148 return -1;
149 int length=buffer.length();
150 if (length>0)
151 buffer.writeTo(_out);
152 buffer.clear();
153 return length;
154 }
155
156 /* (non-Javadoc)
157 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
158 */
159 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
160 {
161 int len=0;
162
163 if (header!=null)
164 {
165 int tw=header.length();
166 if (tw>0)
167 {
168 int f=flush(header);
169 len=f;
170 if (f<tw)
171 return len;
172 }
173 }
174
175 if (buffer!=null)
176 {
177 int tw=buffer.length();
178 if (tw>0)
179 {
180 int f=flush(buffer);
181 if (f<0)
182 return len>0?len:f;
183 len+=f;
184 if (f<tw)
185 return len;
186 }
187 }
188
189 if (trailer!=null)
190 {
191 int tw=trailer.length();
192 if (tw>0)
193 {
194 int f=flush(trailer);
195 if (f<0)
196 return len>0?len:f;
197 len+=f;
198 }
199 }
200 return len;
201 }
202
203 /* ------------------------------------------------------------ */
204 /*
205 * @see org.eclipse.io.EndPoint#getLocalAddr()
206 */
207 public String getLocalAddr()
208 {
209 return null;
210 }
211
212 /* ------------------------------------------------------------ */
213 /*
214 * @see org.eclipse.io.EndPoint#getLocalHost()
215 */
216 public String getLocalHost()
217 {
218 return null;
219 }
220
221 /* ------------------------------------------------------------ */
222 /*
223 * @see org.eclipse.io.EndPoint#getLocalPort()
224 */
225 public int getLocalPort()
226 {
227 return 0;
228 }
229
230 /* ------------------------------------------------------------ */
231 /*
232 * @see org.eclipse.io.EndPoint#getRemoteAddr()
233 */
234 public String getRemoteAddr()
235 {
236 return null;
237 }
238
239 /* ------------------------------------------------------------ */
240 /*
241 * @see org.eclipse.io.EndPoint#getRemoteHost()
242 */
243 public String getRemoteHost()
244 {
245 return null;
246 }
247
248 /* ------------------------------------------------------------ */
249 /*
250 * @see org.eclipse.io.EndPoint#getRemotePort()
251 */
252 public int getRemotePort()
253 {
254 return 0;
255 }
256
257 /* ------------------------------------------------------------ */
258 /*
259 * @see org.eclipse.io.EndPoint#getConnection()
260 */
261 public Object getTransport()
262 {
263 return null;
264 }
265
266 /* ------------------------------------------------------------ */
267 public InputStream getInputStream()
268 {
269 return _in;
270 }
271
272 /* ------------------------------------------------------------ */
273 public void setInputStream(InputStream in)
274 {
275 _in=in;
276 }
277
278 /* ------------------------------------------------------------ */
279 public OutputStream getOutputStream()
280 {
281 return _out;
282 }
283
284 /* ------------------------------------------------------------ */
285 public void setOutputStream(OutputStream out)
286 {
287 _out=out;
288 }
289
290
291 /* ------------------------------------------------------------ */
292 public void flush()
293 throws IOException
294 {
295 if (_out != null)
296 _out.flush();
297 }
298
299 /* ------------------------------------------------------------ */
300 public boolean isBufferingInput()
301 {
302 return false;
303 }
304
305 /* ------------------------------------------------------------ */
306 public boolean isBufferingOutput()
307 {
308 return false;
309 }
310
311 /* ------------------------------------------------------------ */
312 public boolean isBufferred()
313 {
314 return false;
315 }
316
317 /* ------------------------------------------------------------ */
318 public int getMaxIdleTime()
319 {
320 return _maxIdleTime;
321 }
322
323 /* ------------------------------------------------------------ */
324 public void setMaxIdleTime(int timeMs) throws IOException
325 {
326 _maxIdleTime=timeMs;
327 }
328
329 }