View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  
20  package org.eclipse.jetty.io.bio;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  import java.net.SocketTimeoutException;
26  
27  import org.eclipse.jetty.io.Buffer;
28  import org.eclipse.jetty.io.EndPoint;
29  
30  public class StreamEndPoint implements EndPoint
31  {
32      InputStream _in;
33      OutputStream _out;
34      int _maxIdleTime;
35      boolean _ishut;
36      boolean _oshut;
37  
38      /**
39       *
40       */
41      public StreamEndPoint(InputStream in, OutputStream out)
42      {
43          _in=in;
44          _out=out;
45      }
46  
47      public boolean isBlocking()
48      {
49          return true;
50      }
51  
52      public boolean blockReadable(long millisecs) throws IOException
53      {
54          return true;
55      }
56  
57      public boolean blockWritable(long millisecs) throws IOException
58      {
59          return true;
60      }
61  
62      /*
63       * @see org.eclipse.io.BufferIO#isOpen()
64       */
65      public boolean isOpen()
66      {
67          return _in!=null;
68      }
69  
70      /*
71       * @see org.eclipse.io.BufferIO#isOpen()
72       */
73      public final boolean isClosed()
74      {
75          return !isOpen();
76      }
77  
78      public void shutdownOutput() throws IOException
79      {
80          _oshut = true;
81          if (_ishut && _out!=null)
82              _out.close();
83      }
84  
85      public boolean isInputShutdown()
86      {
87          return _ishut;
88      }
89  
90      public void shutdownInput() throws IOException
91      {
92          _ishut = true;
93          if (_oshut&&_in!=null)
94              _in.close();
95      }
96  
97      public boolean isOutputShutdown()
98      {
99          return _oshut;
100     }
101 
102     /*
103      * @see org.eclipse.io.BufferIO#close()
104      */
105     public void close() throws IOException
106     {
107         if (_in!=null)
108             _in.close();
109         _in=null;
110         if (_out!=null)
111             _out.close();
112         _out=null;
113     }
114 
115     protected void idleExpired() throws IOException
116     {
117         if (_in!=null)
118             _in.close();
119     }
120 
121     /* (non-Javadoc)
122      * @see org.eclipse.io.BufferIO#fill(org.eclipse.io.Buffer)
123      */
124     public int fill(Buffer buffer) throws IOException
125     {
126         if (_ishut)
127             return -1;
128         if (_in==null)
129             return 0;
130 
131         int space=buffer.space();
132         if (space<=0)
133         {
134             if (buffer.hasContent())
135                 return 0;
136             throw new IOException("FULL");
137         }
138 
139         try
140         {
141             int filled=buffer.readFrom(_in, space);
142             if (filled<0)
143                 shutdownInput();
144             return filled;
145         }
146         catch(SocketTimeoutException e)
147         {
148             idleExpired();
149             return -1;
150         }
151     }
152 
153     /* (non-Javadoc)
154      * @see org.eclipse.io.BufferIO#flush(org.eclipse.io.Buffer)
155      */
156     public int flush(Buffer buffer) throws IOException
157     {
158         if (_oshut)
159             return -1;
160         if (_out==null)
161             return 0;
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 int getMaxIdleTime()
315     {
316         return _maxIdleTime;
317     }
318 
319     /* ------------------------------------------------------------ */
320     public void setMaxIdleTime(int timeMs) throws IOException
321     {
322         _maxIdleTime=timeMs;
323     }
324 
325 }