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 buffer.clear();
125 return length;
126 }
127
128 /* (non-Javadoc)
129 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
130 */
131 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
132 {
133 int len=0;
134
135 if (header!=null)
136 {
137 int tw=header.length();
138 if (tw>0)
139 {
140 int f=flush(header);
141 len=f;
142 if (f<tw)
143 return len;
144 }
145 }
146
147 if (buffer!=null)
148 {
149 int tw=buffer.length();
150 if (tw>0)
151 {
152 int f=flush(buffer);
153 if (f<0)
154 return len>0?len:f;
155 len+=f;
156 if (f<tw)
157 return len;
158 }
159 }
160
161 if (trailer!=null)
162 {
163 int tw=trailer.length();
164 if (tw>0)
165 {
166 int f=flush(trailer);
167 if (f<0)
168 return len>0?len:f;
169 len+=f;
170 }
171 }
172 return len;
173 }
174
175 /* ------------------------------------------------------------ */
176 /*
177 * @see org.eclipse.io.EndPoint#getLocalAddr()
178 */
179 public String getLocalAddr()
180 {
181 return null;
182 }
183
184 /* ------------------------------------------------------------ */
185 /*
186 * @see org.eclipse.io.EndPoint#getLocalHost()
187 */
188 public String getLocalHost()
189 {
190 return null;
191 }
192
193 /* ------------------------------------------------------------ */
194 /*
195 * @see org.eclipse.io.EndPoint#getLocalPort()
196 */
197 public int getLocalPort()
198 {
199 return 0;
200 }
201
202 /* ------------------------------------------------------------ */
203 /*
204 * @see org.eclipse.io.EndPoint#getRemoteAddr()
205 */
206 public String getRemoteAddr()
207 {
208 return null;
209 }
210
211 /* ------------------------------------------------------------ */
212 /*
213 * @see org.eclipse.io.EndPoint#getRemoteHost()
214 */
215 public String getRemoteHost()
216 {
217 return null;
218 }
219
220 /* ------------------------------------------------------------ */
221 /*
222 * @see org.eclipse.io.EndPoint#getRemotePort()
223 */
224 public int getRemotePort()
225 {
226 return 0;
227 }
228
229 /* ------------------------------------------------------------ */
230 /*
231 * @see org.eclipse.io.EndPoint#getConnection()
232 */
233 public Object getTransport()
234 {
235 return null;
236 }
237
238 /* ------------------------------------------------------------ */
239 public InputStream getInputStream()
240 {
241 return _in;
242 }
243
244 /* ------------------------------------------------------------ */
245 public void setInputStream(InputStream in)
246 {
247 _in=in;
248 }
249
250 /* ------------------------------------------------------------ */
251 public OutputStream getOutputStream()
252 {
253 return _out;
254 }
255
256 /* ------------------------------------------------------------ */
257 public void setOutputStream(OutputStream out)
258 {
259 _out=out;
260 }
261
262
263 /* ------------------------------------------------------------ */
264 public void flush()
265 throws IOException
266 {
267 if (_out != null)
268 _out.flush();
269 }
270
271 /* ------------------------------------------------------------ */
272 public boolean isBufferingInput()
273 {
274 return false;
275 }
276
277 /* ------------------------------------------------------------ */
278 public boolean isBufferingOutput()
279 {
280 return false;
281 }
282
283 /* ------------------------------------------------------------ */
284 public boolean isBufferred()
285 {
286 return false;
287 }
288
289 /* ------------------------------------------------------------ */
290 public int getMaxIdleTime()
291 {
292 return _maxIdleTime;
293 }
294
295 /* ------------------------------------------------------------ */
296 public void setMaxIdleTime(int timeMs) throws IOException
297 {
298 _maxIdleTime=timeMs;
299 }
300
301 }