1 //========================================================================
2 //$Id: StreamEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
3 //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4 //------------------------------------------------------------------------
5 //Licensed under the Apache License, Version 2.0 (the "License");
6 //you may not use this file except in compliance with the License.
7 //You may obtain a copy of the License at
8 //http://www.apache.org/licenses/LICENSE-2.0
9 //Unless required by applicable law or agreed to in writing, software
10 //distributed under the License is distributed on an "AS IS" BASIS,
11 //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //See the License for the specific language governing permissions and
13 //limitations under the License.
14 //========================================================================
15
16
17 package org.mortbay.io.bio;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22
23 import org.mortbay.io.Buffer;
24 import org.mortbay.io.EndPoint;
25
26 /**
27 * @author gregw
28 *
29 * To change the template for this generated type comment go to
30 * Window - Preferences - Java - Code Generation - Code and Comments
31 */
32 public class StreamEndPoint implements EndPoint
33 {
34 InputStream _in;
35 OutputStream _out;
36
37 /**
38 *
39 */
40 public StreamEndPoint(InputStream in, OutputStream out)
41 {
42 _in=in;
43 _out=out;
44 }
45
46 public boolean isBlocking()
47 {
48 return true;
49 }
50
51 public boolean blockReadable(long millisecs) throws IOException
52 {
53 return true;
54 }
55
56 public boolean blockWritable(long millisecs) throws IOException
57 {
58 return true;
59 }
60
61 /*
62 * @see org.mortbay.io.BufferIO#isOpen()
63 */
64 public boolean isOpen()
65 {
66 return _in!=null;
67 }
68
69 /*
70 * @see org.mortbay.io.BufferIO#isOpen()
71 */
72 public final boolean isClosed()
73 {
74 return !isOpen();
75 }
76
77 /*
78 * @see org.mortbay.io.BufferIO#close()
79 */
80 public void close() throws IOException
81 {
82 if (_in!=null)
83 _in.close();
84 _in=null;
85 if (_out!=null)
86 _out.close();
87 _out=null;
88 }
89
90 /* (non-Javadoc)
91 * @see org.mortbay.io.BufferIO#fill(org.mortbay.io.Buffer)
92 */
93 public int fill(Buffer buffer) throws IOException
94 {
95 // TODO handle null array()
96 if (_in==null)
97 return 0;
98
99 int space=buffer.space();
100 if (space<=0)
101 {
102 if (buffer.hasContent())
103 return 0;
104 throw new IOException("FULL");
105 }
106
107 int len = buffer.readFrom(_in,space);
108
109 return len;
110 }
111
112 /* (non-Javadoc)
113 * @see org.mortbay.io.BufferIO#flush(org.mortbay.io.Buffer)
114 */
115 public int flush(Buffer buffer) throws IOException
116 {
117 // TODO handle null array()
118 if (_out==null)
119 return -1;
120 int length=buffer.length();
121 if (length>0)
122 buffer.writeTo(_out);
123 buffer.clear();
124 return length;
125 }
126
127 /* (non-Javadoc)
128 * @see org.mortbay.io.BufferIO#flush(org.mortbay.io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer)
129 */
130 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
131 {
132 int len=0;
133
134 // TODO consider copying buffer and trailer into header if there is space.
135
136
137 if (header!=null)
138 {
139 int tw=header.length();
140 if (tw>0)
141 {
142 int f=flush(header);
143 len=f;
144 if (f<tw)
145 return len;
146 }
147 }
148
149 if (buffer!=null)
150 {
151 int tw=buffer.length();
152 if (tw>0)
153 {
154 int f=flush(buffer);
155 if (f<0)
156 return len>0?len:f;
157 len+=f;
158 if (f<tw)
159 return len;
160 }
161 }
162
163 if (trailer!=null)
164 {
165 int tw=trailer.length();
166 if (tw>0)
167 {
168 int f=flush(trailer);
169 if (f<0)
170 return len>0?len:f;
171 len+=f;
172 }
173 }
174 return len;
175 }
176
177 /* ------------------------------------------------------------ */
178 /*
179 * @see org.mortbay.io.EndPoint#getLocalAddr()
180 */
181 public String getLocalAddr()
182 {
183 return null;
184 }
185
186 /* ------------------------------------------------------------ */
187 /*
188 * @see org.mortbay.io.EndPoint#getLocalHost()
189 */
190 public String getLocalHost()
191 {
192 return null;
193 }
194
195 /* ------------------------------------------------------------ */
196 /*
197 * @see org.mortbay.io.EndPoint#getLocalPort()
198 */
199 public int getLocalPort()
200 {
201 return 0;
202 }
203
204 /* ------------------------------------------------------------ */
205 /*
206 * @see org.mortbay.io.EndPoint#getRemoteAddr()
207 */
208 public String getRemoteAddr()
209 {
210 return null;
211 }
212
213 /* ------------------------------------------------------------ */
214 /*
215 * @see org.mortbay.io.EndPoint#getRemoteHost()
216 */
217 public String getRemoteHost()
218 {
219 return null;
220 }
221
222 /* ------------------------------------------------------------ */
223 /*
224 * @see org.mortbay.io.EndPoint#getRemotePort()
225 */
226 public int getRemotePort()
227 {
228 return 0;
229 }
230
231 /* ------------------------------------------------------------ */
232 /*
233 * @see org.mortbay.io.EndPoint#getConnection()
234 */
235 public Object getTransport()
236 {
237 return null;
238 }
239
240 /* ------------------------------------------------------------ */
241 public InputStream getInputStream()
242 {
243 return _in;
244 }
245
246 /* ------------------------------------------------------------ */
247 public void setInputStream(InputStream in)
248 {
249 _in=in;
250 }
251
252 /* ------------------------------------------------------------ */
253 public OutputStream getOutputStream()
254 {
255 return _out;
256 }
257
258 /* ------------------------------------------------------------ */
259 public void setOutputStream(OutputStream out)
260 {
261 _out=out;
262 }
263
264
265 /* ------------------------------------------------------------ */
266 public void flush()
267 throws IOException
268 {
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 }