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