View Javadoc

1   //========================================================================
2   //$Id: ByteArrayEndPoint.java,v 1.2 2005/11/05 08:32:41 gregwilkins 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  package org.mortbay.io;
17  
18  import java.io.IOException;
19  
20  
21  
22  /* ------------------------------------------------------------ */
23  /** ByteArrayEndPoint.
24   * @author gregw
25   *
26   */
27  public class ByteArrayEndPoint implements EndPoint
28  {
29      byte[] _inBytes;
30      ByteArrayBuffer _in;
31      ByteArrayBuffer _out;
32      boolean _closed;
33      boolean _nonBlocking;
34      boolean _growOutput;
35  
36      /* ------------------------------------------------------------ */
37      /**
38       * 
39       */
40      public ByteArrayEndPoint()
41      {
42      }
43      
44      /* ------------------------------------------------------------ */
45      /**
46       * @return the nonBlocking
47       */
48      public boolean isNonBlocking()
49      {
50          return _nonBlocking;
51      }
52  
53      /* ------------------------------------------------------------ */
54      /**
55       * @param nonBlocking the nonBlocking to set
56       */
57      public void setNonBlocking(boolean nonBlocking)
58      {
59          _nonBlocking=nonBlocking;
60      }
61  
62      /* ------------------------------------------------------------ */
63      /**
64       * 
65       */
66      public ByteArrayEndPoint(byte[] input, int outputSize)
67      {
68          _inBytes=input;
69          _in=new ByteArrayBuffer(input);
70          _out=new ByteArrayBuffer(outputSize);
71      }
72  
73      /* ------------------------------------------------------------ */
74      /**
75       * @return Returns the in.
76       */
77      public ByteArrayBuffer getIn()
78      {
79          return _in;
80      }
81      /* ------------------------------------------------------------ */
82      /**
83       * @param in The in to set.
84       */
85      public void setIn(ByteArrayBuffer in)
86      {
87          _in = in;
88      }
89      /* ------------------------------------------------------------ */
90      /**
91       * @return Returns the out.
92       */
93      public ByteArrayBuffer getOut()
94      {
95          return _out;
96      }
97      /* ------------------------------------------------------------ */
98      /**
99       * @param out The out to set.
100      */
101     public void setOut(ByteArrayBuffer out)
102     {
103         _out = out;
104     }
105     /* ------------------------------------------------------------ */
106     /* 
107      * @see org.mortbay.io.EndPoint#isOpen()
108      */
109     public boolean isOpen()
110     {
111         return !_closed;
112     }
113 
114     /* ------------------------------------------------------------ */
115     /* 
116      * @see org.mortbay.io.EndPoint#isBlocking()
117      */
118     public boolean isBlocking()
119     {
120         return !_nonBlocking;
121     }
122 
123     /* ------------------------------------------------------------ */
124     public boolean blockReadable(long millisecs)
125     {
126         return true;
127     }
128 
129     /* ------------------------------------------------------------ */
130     public boolean blockWritable(long millisecs)
131     {
132         return true;
133     }
134 
135     /* ------------------------------------------------------------ */
136     /* 
137      * @see org.mortbay.io.EndPoint#shutdownOutput()
138      */
139     public void shutdownOutput() throws IOException
140     {
141     }
142     
143     /* ------------------------------------------------------------ */
144     /* 
145      * @see org.mortbay.io.EndPoint#close()
146      */
147     public void close() throws IOException
148     {
149         _closed=true;
150     }
151 
152     /* ------------------------------------------------------------ */
153     /* 
154      * @see org.mortbay.io.EndPoint#fill(org.mortbay.io.Buffer)
155      */
156     public int fill(Buffer buffer) throws IOException
157     {
158         if (_closed)
159             throw new IOException("CLOSED");
160         if (_in==null)
161             return -1;
162         if (_in.length()<=0)
163             return _nonBlocking?0:-1;
164         int len = buffer.put(_in);
165         _in.skip(len);
166         return len;
167     }
168 
169     /* ------------------------------------------------------------ */
170     /* 
171      * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer)
172      */
173     public int flush(Buffer buffer) throws IOException
174     {
175         if (_closed)
176             throw new IOException("CLOSED");
177         if (_growOutput && buffer.length()>_out.space())
178         {
179             _out.compact();
180 
181             if (buffer.length()>_out.space())
182             {
183                 ByteArrayBuffer n = new ByteArrayBuffer(_out.putIndex()+buffer.length());
184 
185                 n.put(_out.peek(0,_out.putIndex()));
186                 if (_out.getIndex()>0)
187                 {
188                     n.mark();
189                     n.setGetIndex(_out.getIndex());
190                 }
191                 _out=n;
192             }
193         }
194         int len = _out.put(buffer);
195         buffer.skip(len);
196         return len;
197     }
198 
199     /* ------------------------------------------------------------ */
200     /* 
201      * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer)
202      */
203     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
204     {
205         if (_closed)
206             throw new IOException("CLOSED");
207         
208         int flushed=0;
209         
210         if (header!=null && header.length()>0)
211             flushed=flush(header);
212         
213         if (header==null || header.length()==0)
214         {
215             if (buffer!=null && buffer.length()>0)
216                 flushed+=flush(buffer);
217             
218             if (buffer==null || buffer.length()==0)
219             {
220                 if (trailer!=null && trailer.length()>0)
221                 {
222                     flushed+=flush(trailer);
223                 }
224             }
225         }
226         
227         return flushed;
228     }
229 
230     /* ------------------------------------------------------------ */
231     /**
232      * 
233      */
234     public void reset()
235     {
236         _closed=false;
237         _in.clear();
238         _out.clear();
239         if (_inBytes!=null)
240             _in.setPutIndex(_inBytes.length);
241     }
242 
243     /* ------------------------------------------------------------ */
244     /* 
245      * @see org.mortbay.io.EndPoint#getLocalAddr()
246      */
247     public String getLocalAddr()
248     {
249         return null;
250     }
251 
252     /* ------------------------------------------------------------ */
253     /* 
254      * @see org.mortbay.io.EndPoint#getLocalHost()
255      */
256     public String getLocalHost()
257     {
258         return null;
259     }
260 
261     /* ------------------------------------------------------------ */
262     /* 
263      * @see org.mortbay.io.EndPoint#getLocalPort()
264      */
265     public int getLocalPort()
266     {
267         return 0;
268     }
269 
270     /* ------------------------------------------------------------ */
271     /* 
272      * @see org.mortbay.io.EndPoint#getRemoteAddr()
273      */
274     public String getRemoteAddr()
275     {
276         return null;
277     }
278 
279     /* ------------------------------------------------------------ */
280     /* 
281      * @see org.mortbay.io.EndPoint#getRemoteHost()
282      */
283     public String getRemoteHost()
284     {
285         return null;
286     }
287 
288     /* ------------------------------------------------------------ */
289     /* 
290      * @see org.mortbay.io.EndPoint#getRemotePort()
291      */
292     public int getRemotePort()
293     {
294         return 0;
295     }
296 
297     /* ------------------------------------------------------------ */
298     /* 
299      * @see org.mortbay.io.EndPoint#getConnection()
300      */
301     public Object getTransport()
302     {
303         return _inBytes;
304     }
305 
306     /* ------------------------------------------------------------ */
307     public void flush() throws IOException
308     {   
309     }
310 
311     /* ------------------------------------------------------------ */
312     public boolean isBufferingInput()
313     {
314         return false;
315     }
316 
317     /* ------------------------------------------------------------ */
318     public boolean isBufferingOutput()
319     {
320         return false;
321     }
322 
323     /* ------------------------------------------------------------ */
324     public boolean isBufferred()
325     {
326         return false;
327     }
328 
329     /* ------------------------------------------------------------ */
330     /**
331      * @return the growOutput
332      */
333     public boolean isGrowOutput()
334     {
335         return _growOutput;
336     }
337 
338     /* ------------------------------------------------------------ */
339     /**
340      * @param growOutput the growOutput to set
341      */
342     public void setGrowOutput(boolean growOutput)
343     {
344         _growOutput=growOutput;
345     }
346 
347 
348 }