View Javadoc

1   //========================================================================
2   //$Id: StreamEndPoint.java,v 1.1 2005/10/05 14:09:39 janb Exp $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at 
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  
17  package org.mortbay.io.bio;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  import java.io.OutputStream;
22  
23  import org.mortbay.io.Buffer;
24  import org.mortbay.io.EndPoint;
25  
26  /**
27   * @author gregw
28   *
29   * To change the template for this generated type comment go to
30   * Window - Preferences - Java - Code Generation - Code and Comments
31   */
32  public class StreamEndPoint implements EndPoint
33  {
34      InputStream _in;
35      OutputStream _out;
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.mortbay.io.BufferIO#isOpen()
63       */
64      public boolean isOpen()
65      {
66          return _in!=null;
67      }
68  
69      /* 
70       * @see org.mortbay.io.BufferIO#isOpen()
71       */
72      public final boolean isClosed()
73      {
74          return !isOpen();
75      }
76      
77      /* 
78       * @see org.mortbay.io.EndPoint#shutdownOutput()
79       */
80      public void shutdownOutput() throws IOException
81      {
82      }
83      
84      /* 
85       * @see org.mortbay.io.BufferIO#close()
86       */
87      public void close() throws IOException
88      {
89          if (_in!=null)
90              _in.close();
91          _in=null;
92          if (_out!=null)
93              _out.close();
94          _out=null;
95      }
96  
97      /* (non-Javadoc)
98       * @see org.mortbay.io.BufferIO#fill(org.mortbay.io.Buffer)
99       */
100     public int fill(Buffer buffer) throws IOException
101     {
102         // TODO handle null array()
103         if (_in==null)
104             return 0;
105             
106     	int space=buffer.space();
107     	if (space<=0)
108     	{
109     	    if (buffer.hasContent())
110     	        return 0;
111     	    throw new IOException("FULL");
112     	}
113         
114         int len = buffer.readFrom(_in,space);
115     
116     	return len;
117     }
118 
119     /* (non-Javadoc)
120      * @see org.mortbay.io.BufferIO#flush(org.mortbay.io.Buffer)
121      */
122     public int flush(Buffer buffer) throws IOException
123     {
124         // TODO handle null array()
125         if (_out==null)
126             return -1;
127         int length=buffer.length();
128         if (length>0)
129             buffer.writeTo(_out);
130         buffer.clear();
131         return length;
132     }
133 
134     /* (non-Javadoc)
135      * @see org.mortbay.io.BufferIO#flush(org.mortbay.io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer)
136      */
137     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
138     {
139         int len=0;
140         
141         // TODO  consider copying buffer and trailer into header if there is space.
142         
143         
144         if (header!=null)
145         {
146             int tw=header.length();
147             if (tw>0)
148             {
149                 int f=flush(header);
150                 len=f;
151                 if (f<tw)
152                     return len;
153             }
154         }
155         
156         if (buffer!=null)
157         {
158             int tw=buffer.length();
159             if (tw>0)
160             {
161                 int f=flush(buffer);
162                 if (f<0)
163                     return len>0?len:f;
164                 len+=f;
165                 if (f<tw)
166                     return len;
167             }
168         }
169         
170         if (trailer!=null)
171         {
172             int tw=trailer.length();
173             if (tw>0)
174             {
175                 int f=flush(trailer);
176                 if (f<0)
177                     return len>0?len:f;
178                 len+=f;
179             }
180         }
181         return len;
182     }
183 
184     /* ------------------------------------------------------------ */
185     /* 
186      * @see org.mortbay.io.EndPoint#getLocalAddr()
187      */
188     public String getLocalAddr()
189     {
190         return null;
191     }
192 
193     /* ------------------------------------------------------------ */
194     /* 
195      * @see org.mortbay.io.EndPoint#getLocalHost()
196      */
197     public String getLocalHost()
198     {
199         return null;
200     }
201 
202     /* ------------------------------------------------------------ */
203     /* 
204      * @see org.mortbay.io.EndPoint#getLocalPort()
205      */
206     public int getLocalPort()
207     {
208         return 0;
209     }
210 
211     /* ------------------------------------------------------------ */
212     /* 
213      * @see org.mortbay.io.EndPoint#getRemoteAddr()
214      */
215     public String getRemoteAddr()
216     {
217         return null;
218     }
219 
220     /* ------------------------------------------------------------ */
221     /* 
222      * @see org.mortbay.io.EndPoint#getRemoteHost()
223      */
224     public String getRemoteHost()
225     {
226         return null;
227     }
228 
229     /* ------------------------------------------------------------ */
230     /* 
231      * @see org.mortbay.io.EndPoint#getRemotePort()
232      */
233     public int getRemotePort()
234     {
235         return 0;
236     }
237 
238     /* ------------------------------------------------------------ */
239     /* 
240      * @see org.mortbay.io.EndPoint#getConnection()
241      */
242     public Object getTransport()
243     {
244         return null;
245     }
246 
247     /* ------------------------------------------------------------ */
248     public InputStream getInputStream()
249     {
250         return _in;
251     }
252 
253     /* ------------------------------------------------------------ */
254     public void setInputStream(InputStream in)
255     {
256         _in=in;
257     }
258 
259     /* ------------------------------------------------------------ */
260     public OutputStream getOutputStream()
261     {
262         return _out;
263     }
264 
265     /* ------------------------------------------------------------ */
266     public void setOutputStream(OutputStream out)
267     {
268         _out=out;
269     }
270 
271 
272     /* ------------------------------------------------------------ */
273     public void flush()
274         throws IOException
275     {   
276         _out.flush();
277     }
278 
279     /* ------------------------------------------------------------ */
280     public boolean isBufferingInput()
281     {
282         return false;
283     }
284 
285     /* ------------------------------------------------------------ */
286     public boolean isBufferingOutput()
287     {
288         return false;
289     }
290 
291     /* ------------------------------------------------------------ */
292     public boolean isBufferred()
293     {
294         return false;
295     }
296 
297 }