View Javadoc

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  import java.net.SocketTimeoutException;
21  
22  import org.eclipse.jetty.io.Buffer;
23  import org.eclipse.jetty.io.EndPoint;
24  
25  public class StreamEndPoint implements EndPoint
26  {
27      InputStream _in;
28      OutputStream _out;
29      int _maxIdleTime;
30      boolean _ishut;
31      boolean _oshut;
32  
33      /**
34       *
35       */
36      public StreamEndPoint(InputStream in, OutputStream out)
37      {
38          _in=in;
39          _out=out;
40      }
41  
42      public boolean isBlocking()
43      {
44          return true;
45      }
46  
47      public boolean blockReadable(long millisecs) throws IOException
48      {
49          return true;
50      }
51  
52      public boolean blockWritable(long millisecs) throws IOException
53      {
54          return true;
55      }
56  
57      /*
58       * @see org.eclipse.io.BufferIO#isOpen()
59       */
60      public boolean isOpen()
61      {
62          return _in!=null;
63      }
64  
65      /*
66       * @see org.eclipse.io.BufferIO#isOpen()
67       */
68      public final boolean isClosed()
69      {
70          return !isOpen();
71      }
72  
73      public void shutdownOutput() throws IOException
74      {
75          _oshut = true;
76          if (_ishut && _out!=null)
77              _out.close();
78      }
79  
80      public boolean isInputShutdown()
81      {
82          return _ishut;
83      }
84  
85      public void shutdownInput() throws IOException
86      {
87          _ishut = true;
88          if (_oshut&&_in!=null)
89              _in.close();
90      }
91  
92      public boolean isOutputShutdown()
93      {
94          return _oshut;
95      }
96  
97      /*
98       * @see org.eclipse.io.BufferIO#close()
99       */
100     public void close() throws IOException
101     {
102         if (_in!=null)
103             _in.close();
104         _in=null;
105         if (_out!=null)
106             _out.close();
107         _out=null;
108     }
109 
110     protected void idleExpired() throws IOException
111     {
112         if (_in!=null)
113             _in.close();
114     }
115 
116     /* (non-Javadoc)
117      * @see org.eclipse.io.BufferIO#fill(org.eclipse.io.Buffer)
118      */
119     public int fill(Buffer buffer) throws IOException
120     {
121         if (_ishut)
122             return -1;
123         if (_in==null)
124             return 0;
125 
126         int space=buffer.space();
127         if (space<=0)
128         {
129             if (buffer.hasContent())
130                 return 0;
131             throw new IOException("FULL");
132         }
133 
134         try
135         {
136             int filled=buffer.readFrom(_in, space);
137             if (filled<0)
138                 shutdownInput();
139             return filled;
140         }
141         catch(SocketTimeoutException e)
142         {
143             idleExpired();
144             return -1;
145         }
146     }
147 
148     /* (non-Javadoc)
149      * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer)
150      */
151     public int flush(Buffer buffer) throws IOException
152     {
153         if (_oshut)
154             return -1;
155         if (_out==null)
156             return 0;
157         int length=buffer.length();
158         if (length>0)
159             buffer.writeTo(_out);
160         buffer.clear();
161         return length;
162     }
163 
164     /* (non-Javadoc)
165      * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
166      */
167     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
168     {
169         int len=0;
170 
171         if (header!=null)
172         {
173             int tw=header.length();
174             if (tw>0)
175             {
176                 int f=flush(header);
177                 len=f;
178                 if (f<tw)
179                     return len;
180             }
181         }
182 
183         if (buffer!=null)
184         {
185             int tw=buffer.length();
186             if (tw>0)
187             {
188                 int f=flush(buffer);
189                 if (f<0)
190                     return len>0?len:f;
191                 len+=f;
192                 if (f<tw)
193                     return len;
194             }
195         }
196 
197         if (trailer!=null)
198         {
199             int tw=trailer.length();
200             if (tw>0)
201             {
202                 int f=flush(trailer);
203                 if (f<0)
204                     return len>0?len:f;
205                 len+=f;
206             }
207         }
208         return len;
209     }
210 
211     /* ------------------------------------------------------------ */
212     /*
213      * @see org.eclipse.io.EndPoint#getLocalAddr()
214      */
215     public String getLocalAddr()
216     {
217         return null;
218     }
219 
220     /* ------------------------------------------------------------ */
221     /*
222      * @see org.eclipse.io.EndPoint#getLocalHost()
223      */
224     public String getLocalHost()
225     {
226         return null;
227     }
228 
229     /* ------------------------------------------------------------ */
230     /*
231      * @see org.eclipse.io.EndPoint#getLocalPort()
232      */
233     public int getLocalPort()
234     {
235         return 0;
236     }
237 
238     /* ------------------------------------------------------------ */
239     /*
240      * @see org.eclipse.io.EndPoint#getRemoteAddr()
241      */
242     public String getRemoteAddr()
243     {
244         return null;
245     }
246 
247     /* ------------------------------------------------------------ */
248     /*
249      * @see org.eclipse.io.EndPoint#getRemoteHost()
250      */
251     public String getRemoteHost()
252     {
253         return null;
254     }
255 
256     /* ------------------------------------------------------------ */
257     /*
258      * @see org.eclipse.io.EndPoint#getRemotePort()
259      */
260     public int getRemotePort()
261     {
262         return 0;
263     }
264 
265     /* ------------------------------------------------------------ */
266     /*
267      * @see org.eclipse.io.EndPoint#getConnection()
268      */
269     public Object getTransport()
270     {
271         return null;
272     }
273 
274     /* ------------------------------------------------------------ */
275     public InputStream getInputStream()
276     {
277         return _in;
278     }
279 
280     /* ------------------------------------------------------------ */
281     public void setInputStream(InputStream in)
282     {
283         _in=in;
284     }
285 
286     /* ------------------------------------------------------------ */
287     public OutputStream getOutputStream()
288     {
289         return _out;
290     }
291 
292     /* ------------------------------------------------------------ */
293     public void setOutputStream(OutputStream out)
294     {
295         _out=out;
296     }
297 
298 
299     /* ------------------------------------------------------------ */
300     public void flush()
301         throws IOException
302     {
303         if (_out != null)
304             _out.flush();
305     }
306 
307     /* ------------------------------------------------------------ */
308     public int getMaxIdleTime()
309     {
310         return _maxIdleTime;
311     }
312 
313     /* ------------------------------------------------------------ */
314     public void setMaxIdleTime(int timeMs) throws IOException
315     {
316         _maxIdleTime=timeMs;
317     }
318 
319 }