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         if (!buffer.isImmutable())
250             buffer.skip(len);
251         return len;
252     }
253 
254     /* ------------------------------------------------------------ */
255     /*
256      * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
257      */
258     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
259     {
260         if (_closed)
261             throw new IOException("CLOSED");
262 
263         int flushed=0;
264 
265         if (header!=null && header.length()>0)
266             flushed=flush(header);
267 
268         if (header==null || header.length()==0)
269         {
270             if (buffer!=null && buffer.length()>0)
271                 flushed+=flush(buffer);
272 
273             if (buffer==null || buffer.length()==0)
274             {
275                 if (trailer!=null && trailer.length()>0)
276                 {
277                     flushed+=flush(trailer);
278                 }
279             }
280         }
281 
282         return flushed;
283     }
284 
285     /* ------------------------------------------------------------ */
286     /**
287      *
288      */
289     public void reset()
290     {
291         _closed=false;
292         _in.clear();
293         _out.clear();
294         if (_inBytes!=null)
295             _in.setPutIndex(_inBytes.length);
296     }
297 
298     /* ------------------------------------------------------------ */
299     /*
300      * @see org.eclipse.io.EndPoint#getLocalAddr()
301      */
302     public String getLocalAddr()
303     {
304         return null;
305     }
306 
307     /* ------------------------------------------------------------ */
308     /*
309      * @see org.eclipse.io.EndPoint#getLocalHost()
310      */
311     public String getLocalHost()
312     {
313         return null;
314     }
315 
316     /* ------------------------------------------------------------ */
317     /*
318      * @see org.eclipse.io.EndPoint#getLocalPort()
319      */
320     public int getLocalPort()
321     {
322         return 0;
323     }
324 
325     /* ------------------------------------------------------------ */
326     /*
327      * @see org.eclipse.io.EndPoint#getRemoteAddr()
328      */
329     public String getRemoteAddr()
330     {
331         return null;
332     }
333 
334     /* ------------------------------------------------------------ */
335     /*
336      * @see org.eclipse.io.EndPoint#getRemoteHost()
337      */
338     public String getRemoteHost()
339     {
340         return null;
341     }
342 
343     /* ------------------------------------------------------------ */
344     /*
345      * @see org.eclipse.io.EndPoint#getRemotePort()
346      */
347     public int getRemotePort()
348     {
349         return 0;
350     }
351 
352     /* ------------------------------------------------------------ */
353     /*
354      * @see org.eclipse.io.EndPoint#getConnection()
355      */
356     public Object getTransport()
357     {
358         return _inBytes;
359     }
360 
361     /* ------------------------------------------------------------ */
362     public void flush() throws IOException
363     {
364     }
365     
366     /* ------------------------------------------------------------ */
367     /**
368      * @return the growOutput
369      */
370     public boolean isGrowOutput()
371     {
372         return _growOutput;
373     }
374 
375     /* ------------------------------------------------------------ */
376     /**
377      * @param growOutput the growOutput to set
378      */
379     public void setGrowOutput(boolean growOutput)
380     {
381         _growOutput=growOutput;
382     }
383 
384     /* ------------------------------------------------------------ */
385     /**
386      * @see org.eclipse.jetty.io.EndPoint#getMaxIdleTime()
387      */
388     public int getMaxIdleTime()
389     {
390         return _maxIdleTime;
391     }
392 
393     /* ------------------------------------------------------------ */
394     /**
395      * @see org.eclipse.jetty.io.EndPoint#setMaxIdleTime(int)
396      */
397     public void setMaxIdleTime(int timeMs) throws IOException
398     {
399         _maxIdleTime=timeMs;
400     }
401 
402 
403 }