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