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