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