View Javadoc

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