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         if (!buffer.isImmutable())
166             buffer.clear();
167         return length;
168     }
169 
170     /* (non-Javadoc)
171      * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
172      */
173     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
174     {
175         int len=0;
176 
177         if (header!=null)
178         {
179             int tw=header.length();
180             if (tw>0)
181             {
182                 int f=flush(header);
183                 len=f;
184                 if (f<tw)
185                     return len;
186             }
187         }
188 
189         if (buffer!=null)
190         {
191             int tw=buffer.length();
192             if (tw>0)
193             {
194                 int f=flush(buffer);
195                 if (f<0)
196                     return len>0?len:f;
197                 len+=f;
198                 if (f<tw)
199                     return len;
200             }
201         }
202 
203         if (trailer!=null)
204         {
205             int tw=trailer.length();
206             if (tw>0)
207             {
208                 int f=flush(trailer);
209                 if (f<0)
210                     return len>0?len:f;
211                 len+=f;
212             }
213         }
214         return len;
215     }
216 
217     /* ------------------------------------------------------------ */
218     /*
219      * @see org.eclipse.io.EndPoint#getLocalAddr()
220      */
221     public String getLocalAddr()
222     {
223         return null;
224     }
225 
226     /* ------------------------------------------------------------ */
227     /*
228      * @see org.eclipse.io.EndPoint#getLocalHost()
229      */
230     public String getLocalHost()
231     {
232         return null;
233     }
234 
235     /* ------------------------------------------------------------ */
236     /*
237      * @see org.eclipse.io.EndPoint#getLocalPort()
238      */
239     public int getLocalPort()
240     {
241         return 0;
242     }
243 
244     /* ------------------------------------------------------------ */
245     /*
246      * @see org.eclipse.io.EndPoint#getRemoteAddr()
247      */
248     public String getRemoteAddr()
249     {
250         return null;
251     }
252 
253     /* ------------------------------------------------------------ */
254     /*
255      * @see org.eclipse.io.EndPoint#getRemoteHost()
256      */
257     public String getRemoteHost()
258     {
259         return null;
260     }
261 
262     /* ------------------------------------------------------------ */
263     /*
264      * @see org.eclipse.io.EndPoint#getRemotePort()
265      */
266     public int getRemotePort()
267     {
268         return 0;
269     }
270 
271     /* ------------------------------------------------------------ */
272     /*
273      * @see org.eclipse.io.EndPoint#getConnection()
274      */
275     public Object getTransport()
276     {
277         return null;
278     }
279 
280     /* ------------------------------------------------------------ */
281     public InputStream getInputStream()
282     {
283         return _in;
284     }
285 
286     /* ------------------------------------------------------------ */
287     public void setInputStream(InputStream in)
288     {
289         _in=in;
290     }
291 
292     /* ------------------------------------------------------------ */
293     public OutputStream getOutputStream()
294     {
295         return _out;
296     }
297 
298     /* ------------------------------------------------------------ */
299     public void setOutputStream(OutputStream out)
300     {
301         _out=out;
302     }
303 
304 
305     /* ------------------------------------------------------------ */
306     public void flush()
307         throws IOException
308     {
309         if (_out != null)
310             _out.flush();
311     }
312 
313     /* ------------------------------------------------------------ */
314     public boolean isBufferingInput()
315     {
316         return false;
317     }
318 
319     /* ------------------------------------------------------------ */
320     public boolean isBufferingOutput()
321     {
322         return false;
323     }
324 
325     /* ------------------------------------------------------------ */
326     public boolean isBufferred()
327     {
328         return false;
329     }
330 
331     /* ------------------------------------------------------------ */
332     public int getMaxIdleTime()
333     {
334         return _maxIdleTime;
335     }
336 
337     /* ------------------------------------------------------------ */
338     public void setMaxIdleTime(int timeMs) throws IOException
339     {
340         _maxIdleTime=timeMs;
341     }
342 
343 }