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
35 /**
36 *
37 */
38 public StreamEndPoint(InputStream in, OutputStream out)
39 {
40 _in=in;
41 _out=out;
42 }
43
44 public boolean isBlocking()
45 {
46 return true;
47 }
48
49 public boolean blockReadable(long millisecs) throws IOException
50 {
51 return true;
52 }
53
54 public boolean blockWritable(long millisecs) throws IOException
55 {
56 return true;
57 }
58
59 /*
60 * @see org.eclipse.io.BufferIO#isOpen()
61 */
62 public boolean isOpen()
63 {
64 return _in!=null;
65 }
66
67 /*
68 * @see org.eclipse.io.BufferIO#isOpen()
69 */
70 public final boolean isClosed()
71 {
72 return !isOpen();
73 }
74
75 /*
76 * @see org.eclipse.io.BufferIO#close()
77 */
78 public void close() throws IOException
79 {
80 if (_in!=null)
81 _in.close();
82 _in=null;
83 if (_out!=null)
84 _out.close();
85 _out=null;
86 }
87
88 /* (non-Javadoc)
89 * @see org.eclipse.io.BufferIO#fill(org.eclipse.io.Buffer)
90 */
91 public int fill(Buffer buffer) throws IOException
92 {
93 // TODO handle null array()
94 if (_in==null)
95 return 0;
96
97 int space=buffer.space();
98 if (space<=0)
99 {
100 if (buffer.hasContent())
101 return 0;
102 throw new IOException("FULL");
103 }
104
105 int len = buffer.readFrom(_in,space);
106
107 return len;
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer)
112 */
113 public int flush(Buffer buffer) throws IOException
114 {
115 // TODO handle null array()
116 if (_out==null)
117 return -1;
118 int length=buffer.length();
119 if (length>0)
120 buffer.writeTo(_out);
121 buffer.clear();
122 return length;
123 }
124
125 /* (non-Javadoc)
126 * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
127 */
128 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
129 {
130 int len=0;
131
132 // TODO consider copying buffer and trailer into header if there is space.
133
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 _out.flush();
268 }
269
270 /* ------------------------------------------------------------ */
271 public boolean isBufferingInput()
272 {
273 return false;
274 }
275
276 /* ------------------------------------------------------------ */
277 public boolean isBufferingOutput()
278 {
279 return false;
280 }
281
282 /* ------------------------------------------------------------ */
283 public boolean isBufferred()
284 {
285 return false;
286 }
287
288 }