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