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          if (_oshut)
76              return;
77          _oshut = true;
78          if (_out!=null)
79              _out.close();
80      }
81  
82      public boolean isInputShutdown()
83      {
84          return _ishut;
85      }
86  
87      public void shutdownInput() throws IOException
88      {
89          if (_ishut)
90              return;
91          _ishut = true;
92          if (_in!=null)
93              _in.close();
94      }
95  
96      public boolean isOutputShutdown()
97      {
98          return _oshut;
99      }
100 
101     /*
102      * @see org.eclipse.io.BufferIO#close()
103      */
104     public void close() throws IOException
105     {
106         if (_in!=null)
107             _in.close();
108         _in=null;
109         if (_out!=null)
110             _out.close();
111         _out=null;
112     }
113 
114     protected void idleExpired() throws IOException
115     {
116         if (_in!=null)
117             _in.close();
118     }
119 
120     /* (non-Javadoc)
121      * @see org.eclipse.io.BufferIO#fill(org.eclipse.io.Buffer)
122      */
123     public int fill(Buffer buffer) throws IOException
124     {
125         if (_in==null)
126             return 0;
127 
128         int space=buffer.space();
129         if (space<=0)
130         {
131             if (buffer.hasContent())
132                 return 0;
133             throw new IOException("FULL");
134         }
135 
136         try
137         {
138             int read=buffer.readFrom(_in, space);
139             if (read<0 && isOpen())
140             {
141                 if (!isInputShutdown())
142                     shutdownInput();
143                 else if (isOutputShutdown())
144                     close();
145             }
146             return read;
147         }
148         catch(SocketTimeoutException e)
149         {
150             idleExpired();
151             return -1;
152         }
153     }
154 
155     /* (non-Javadoc)
156      * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer)
157      */
158     public int flush(Buffer buffer) throws IOException
159     {
160         if (_out==null)
161             return -1;
162         int length=buffer.length();
163         if (length>0)
164             buffer.writeTo(_out);
165         buffer.clear();
166         return length;
167     }
168 
169     /* (non-Javadoc)
170      * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
171      */
172     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
173     {
174         int len=0;
175 
176         if (header!=null)
177         {
178             int tw=header.length();
179             if (tw>0)
180             {
181                 int f=flush(header);
182                 len=f;
183                 if (f<tw)
184                     return len;
185             }
186         }
187 
188         if (buffer!=null)
189         {
190             int tw=buffer.length();
191             if (tw>0)
192             {
193                 int f=flush(buffer);
194                 if (f<0)
195                     return len>0?len:f;
196                 len+=f;
197                 if (f<tw)
198                     return len;
199             }
200         }
201 
202         if (trailer!=null)
203         {
204             int tw=trailer.length();
205             if (tw>0)
206             {
207                 int f=flush(trailer);
208                 if (f<0)
209                     return len>0?len:f;
210                 len+=f;
211             }
212         }
213         return len;
214     }
215 
216     /* ------------------------------------------------------------ */
217     /*
218      * @see org.eclipse.io.EndPoint#getLocalAddr()
219      */
220     public String getLocalAddr()
221     {
222         return null;
223     }
224 
225     /* ------------------------------------------------------------ */
226     /*
227      * @see org.eclipse.io.EndPoint#getLocalHost()
228      */
229     public String getLocalHost()
230     {
231         return null;
232     }
233 
234     /* ------------------------------------------------------------ */
235     /*
236      * @see org.eclipse.io.EndPoint#getLocalPort()
237      */
238     public int getLocalPort()
239     {
240         return 0;
241     }
242 
243     /* ------------------------------------------------------------ */
244     /*
245      * @see org.eclipse.io.EndPoint#getRemoteAddr()
246      */
247     public String getRemoteAddr()
248     {
249         return null;
250     }
251 
252     /* ------------------------------------------------------------ */
253     /*
254      * @see org.eclipse.io.EndPoint#getRemoteHost()
255      */
256     public String getRemoteHost()
257     {
258         return null;
259     }
260 
261     /* ------------------------------------------------------------ */
262     /*
263      * @see org.eclipse.io.EndPoint#getRemotePort()
264      */
265     public int getRemotePort()
266     {
267         return 0;
268     }
269 
270     /* ------------------------------------------------------------ */
271     /*
272      * @see org.eclipse.io.EndPoint#getConnection()
273      */
274     public Object getTransport()
275     {
276         return null;
277     }
278 
279     /* ------------------------------------------------------------ */
280     public InputStream getInputStream()
281     {
282         return _in;
283     }
284 
285     /* ------------------------------------------------------------ */
286     public void setInputStream(InputStream in)
287     {
288         _in=in;
289     }
290 
291     /* ------------------------------------------------------------ */
292     public OutputStream getOutputStream()
293     {
294         return _out;
295     }
296 
297     /* ------------------------------------------------------------ */
298     public void setOutputStream(OutputStream out)
299     {
300         _out=out;
301     }
302 
303 
304     /* ------------------------------------------------------------ */
305     public void flush()
306         throws IOException
307     {
308         if (_out != null)
309             _out.flush();
310     }
311 
312     /* ------------------------------------------------------------ */
313     public boolean isBufferingInput()
314     {
315         return false;
316     }
317 
318     /* ------------------------------------------------------------ */
319     public boolean isBufferingOutput()
320     {
321         return false;
322     }
323 
324     /* ------------------------------------------------------------ */
325     public boolean isBufferred()
326     {
327         return false;
328     }
329 
330     /* ------------------------------------------------------------ */
331     public int getMaxIdleTime()
332     {
333         return _maxIdleTime;
334     }
335 
336     /* ------------------------------------------------------------ */
337     public void setMaxIdleTime(int timeMs) throws IOException
338     {
339         _maxIdleTime=timeMs;
340     }
341 
342 }