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 public void shutdownOutput() throws IOException
77 {
78 }
79
80 /*
81 * @see org.eclipse.io.BufferIO#close()
82 */
83 public void close() throws IOException
84 {
85 if (_in!=null)
86 _in.close();
87 _in=null;
88 if (_out!=null)
89 _out.close();
90 _out=null;
91 }
92
93 /* (non-Javadoc)
94 * @see org.eclipse.io.BufferIO#fill(org.eclipse.io.Buffer)
95 */
96 public int fill(Buffer buffer) throws IOException
97 {
98 // TODO handle null array()
99 if (_in==null)
100 return 0;
101
102 int space=buffer.space();
103 if (space<=0)
104 {
105 if (buffer.hasContent())
106 return 0;
107 throw new IOException("FULL");
108 }
109
110 return buffer.readFrom(_in,space);
111 }
112
113 /* (non-Javadoc)
114 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer)
115 */
116 public int flush(Buffer buffer) throws IOException
117 {
118 // TODO handle null array()
119 if (_out==null)
120 return -1;
121 int length=buffer.length();
122 if (length>0)
123 buffer.writeTo(_out);
124 if (!buffer.isImmutable())
125 buffer.clear();
126 return length;
127 }
128
129 /* (non-Javadoc)
130 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
131 */
132 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
133 {
134 int len=0;
135
136 if (header!=null)
137 {
138 int tw=header.length();
139 if (tw>0)
140 {
141 int f=flush(header);
142 len=f;
143 if (f<tw)
144 return len;
145 }
146 }
147
148 if (buffer!=null)
149 {
150 int tw=buffer.length();
151 if (tw>0)
152 {
153 int f=flush(buffer);
154 if (f<0)
155 return len>0?len:f;
156 len+=f;
157 if (f<tw)
158 return len;
159 }
160 }
161
162 if (trailer!=null)
163 {
164 int tw=trailer.length();
165 if (tw>0)
166 {
167 int f=flush(trailer);
168 if (f<0)
169 return len>0?len:f;
170 len+=f;
171 }
172 }
173 return len;
174 }
175
176 /* ------------------------------------------------------------ */
177 /*
178 * @see org.eclipse.io.EndPoint#getLocalAddr()
179 */
180 public String getLocalAddr()
181 {
182 return null;
183 }
184
185 /* ------------------------------------------------------------ */
186 /*
187 * @see org.eclipse.io.EndPoint#getLocalHost()
188 */
189 public String getLocalHost()
190 {
191 return null;
192 }
193
194 /* ------------------------------------------------------------ */
195 /*
196 * @see org.eclipse.io.EndPoint#getLocalPort()
197 */
198 public int getLocalPort()
199 {
200 return 0;
201 }
202
203 /* ------------------------------------------------------------ */
204 /*
205 * @see org.eclipse.io.EndPoint#getRemoteAddr()
206 */
207 public String getRemoteAddr()
208 {
209 return null;
210 }
211
212 /* ------------------------------------------------------------ */
213 /*
214 * @see org.eclipse.io.EndPoint#getRemoteHost()
215 */
216 public String getRemoteHost()
217 {
218 return null;
219 }
220
221 /* ------------------------------------------------------------ */
222 /*
223 * @see org.eclipse.io.EndPoint#getRemotePort()
224 */
225 public int getRemotePort()
226 {
227 return 0;
228 }
229
230 /* ------------------------------------------------------------ */
231 /*
232 * @see org.eclipse.io.EndPoint#getConnection()
233 */
234 public Object getTransport()
235 {
236 return null;
237 }
238
239 /* ------------------------------------------------------------ */
240 public InputStream getInputStream()
241 {
242 return _in;
243 }
244
245 /* ------------------------------------------------------------ */
246 public void setInputStream(InputStream in)
247 {
248 _in=in;
249 }
250
251 /* ------------------------------------------------------------ */
252 public OutputStream getOutputStream()
253 {
254 return _out;
255 }
256
257 /* ------------------------------------------------------------ */
258 public void setOutputStream(OutputStream out)
259 {
260 _out=out;
261 }
262
263
264 /* ------------------------------------------------------------ */
265 public void flush()
266 throws IOException
267 {
268 if (_out != null)
269 _out.flush();
270 }
271
272 /* ------------------------------------------------------------ */
273 public boolean isBufferingInput()
274 {
275 return false;
276 }
277
278 /* ------------------------------------------------------------ */
279 public boolean isBufferingOutput()
280 {
281 return false;
282 }
283
284 /* ------------------------------------------------------------ */
285 public boolean isBufferred()
286 {
287 return false;
288 }
289
290 /* ------------------------------------------------------------ */
291 public int getMaxIdleTime()
292 {
293 return _maxIdleTime;
294 }
295
296 /* ------------------------------------------------------------ */
297 public void setMaxIdleTime(int timeMs) throws IOException
298 {
299 _maxIdleTime=timeMs;
300 }
301
302 }