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#close()
138      */
139     public void close() throws IOException
140     {
141         _closed=true;
142     }
143 
144     /* ------------------------------------------------------------ */
145     /* 
146      * @see org.mortbay.io.EndPoint#fill(org.mortbay.io.Buffer)
147      */
148     public int fill(Buffer buffer) throws IOException
149     {
150         if (_closed)
151             throw new IOException("CLOSED");
152         if (_in==null)
153             return -1;
154         if (_in.length()<=0)
155             return _nonBlocking?0:-1;
156         int len = buffer.put(_in);
157         _in.skip(len);
158         return len;
159     }
160 
161     /* ------------------------------------------------------------ */
162     /* 
163      * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer)
164      */
165     public int flush(Buffer buffer) throws IOException
166     {
167         if (_closed)
168             throw new IOException("CLOSED");
169         if (_growOutput && buffer.length()>_out.space())
170         {
171             _out.compact();
172 
173             if (buffer.length()>_out.space())
174             {
175                 ByteArrayBuffer n = new ByteArrayBuffer(_out.putIndex()+buffer.length());
176 
177                 n.put(_out.peek(0,_out.putIndex()));
178                 if (_out.getIndex()>0)
179                 {
180                     n.mark();
181                     n.setGetIndex(_out.getIndex());
182                 }
183                 _out=n;
184             }
185         }
186         int len = _out.put(buffer);
187         buffer.skip(len);
188         return len;
189     }
190 
191     /* ------------------------------------------------------------ */
192     /* 
193      * @see org.mortbay.io.EndPoint#flush(org.mortbay.io.Buffer, org.mortbay.io.Buffer, org.mortbay.io.Buffer)
194      */
195     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
196     {
197         if (_closed)
198             throw new IOException("CLOSED");
199         
200         int flushed=0;
201         
202         if (header!=null && header.length()>0)
203             flushed=flush(header);
204         
205         if (header==null || header.length()==0)
206         {
207             if (buffer!=null && buffer.length()>0)
208                 flushed+=flush(buffer);
209             
210             if (buffer==null || buffer.length()==0)
211             {
212                 if (trailer!=null && trailer.length()>0)
213                 {
214                     flushed+=flush(trailer);
215                 }
216             }
217         }
218         
219         return flushed;
220     }
221 
222     /* ------------------------------------------------------------ */
223     /**
224      * 
225      */
226     public void reset()
227     {
228         _closed=false;
229         _in.clear();
230         _out.clear();
231         if (_inBytes!=null)
232             _in.setPutIndex(_inBytes.length);
233     }
234 
235     /* ------------------------------------------------------------ */
236     /* 
237      * @see org.mortbay.io.EndPoint#getLocalAddr()
238      */
239     public String getLocalAddr()
240     {
241         return null;
242     }
243 
244     /* ------------------------------------------------------------ */
245     /* 
246      * @see org.mortbay.io.EndPoint#getLocalHost()
247      */
248     public String getLocalHost()
249     {
250         return null;
251     }
252 
253     /* ------------------------------------------------------------ */
254     /* 
255      * @see org.mortbay.io.EndPoint#getLocalPort()
256      */
257     public int getLocalPort()
258     {
259         return 0;
260     }
261 
262     /* ------------------------------------------------------------ */
263     /* 
264      * @see org.mortbay.io.EndPoint#getRemoteAddr()
265      */
266     public String getRemoteAddr()
267     {
268         return null;
269     }
270 
271     /* ------------------------------------------------------------ */
272     /* 
273      * @see org.mortbay.io.EndPoint#getRemoteHost()
274      */
275     public String getRemoteHost()
276     {
277         return null;
278     }
279 
280     /* ------------------------------------------------------------ */
281     /* 
282      * @see org.mortbay.io.EndPoint#getRemotePort()
283      */
284     public int getRemotePort()
285     {
286         return 0;
287     }
288 
289     /* ------------------------------------------------------------ */
290     /* 
291      * @see org.mortbay.io.EndPoint#getConnection()
292      */
293     public Object getTransport()
294     {
295         return _inBytes;
296     }
297 
298     /* ------------------------------------------------------------ */
299     public void flush() throws IOException
300     {   
301     }
302 
303     /* ------------------------------------------------------------ */
304     public boolean isBufferingInput()
305     {
306         return false;
307     }
308 
309     /* ------------------------------------------------------------ */
310     public boolean isBufferingOutput()
311     {
312         return false;
313     }
314 
315     /* ------------------------------------------------------------ */
316     public boolean isBufferred()
317     {
318         return false;
319     }
320 
321     /* ------------------------------------------------------------ */
322     /**
323      * @return the growOutput
324      */
325     public boolean isGrowOutput()
326     {
327         return _growOutput;
328     }
329 
330     /* ------------------------------------------------------------ */
331     /**
332      * @param growOutput the growOutput to set
333      */
334     public void setGrowOutput(boolean growOutput)
335     {
336         _growOutput=growOutput;
337     }
338 
339 
340 }