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 EndPoint
26  {
27      byte[] _inBytes;
28      ByteArrayBuffer _in;
29      ByteArrayBuffer _out;
30      boolean _closed;
31      boolean _nonBlocking;
32      boolean _growOutput;
33  
34      /* ------------------------------------------------------------ */
35      /**
36       * 
37       */
38      public ByteArrayEndPoint()
39      {
40      }
41      
42      /* ------------------------------------------------------------ */
43      /**
44       * @return the nonBlocking
45       */
46      public boolean isNonBlocking()
47      {
48          return _nonBlocking;
49      }
50  
51      /* ------------------------------------------------------------ */
52      /**
53       * @param nonBlocking the nonBlocking to set
54       */
55      public void setNonBlocking(boolean nonBlocking)
56      {
57          _nonBlocking=nonBlocking;
58      }
59  
60      /* ------------------------------------------------------------ */
61      /**
62       * 
63       */
64      public ByteArrayEndPoint(byte[] input, int outputSize)
65      {
66          _inBytes=input;
67          _in=new ByteArrayBuffer(input);
68          _out=new ByteArrayBuffer(outputSize);
69      }
70  
71      /* ------------------------------------------------------------ */
72      /**
73       * @return Returns the in.
74       */
75      public ByteArrayBuffer getIn()
76      {
77          return _in;
78      }
79      /* ------------------------------------------------------------ */
80      /**
81       * @param in The in to set.
82       */
83      public void setIn(ByteArrayBuffer in)
84      {
85          _in = in;
86      }
87      /* ------------------------------------------------------------ */
88      /**
89       * @return Returns the out.
90       */
91      public ByteArrayBuffer getOut()
92      {
93          return _out;
94      }
95      /* ------------------------------------------------------------ */
96      /**
97       * @param out The out to set.
98       */
99      public void setOut(ByteArrayBuffer out)
100     {
101         _out = out;
102     }
103     /* ------------------------------------------------------------ */
104     /* 
105      * @see org.eclipse.io.EndPoint#isOpen()
106      */
107     public boolean isOpen()
108     {
109         return !_closed;
110     }
111 
112     /* ------------------------------------------------------------ */
113     /* 
114      * @see org.eclipse.io.EndPoint#isBlocking()
115      */
116     public boolean isBlocking()
117     {
118         return !_nonBlocking;
119     }
120 
121     /* ------------------------------------------------------------ */
122     public boolean blockReadable(long millisecs)
123     {
124         return true;
125     }
126 
127     /* ------------------------------------------------------------ */
128     public boolean blockWritable(long millisecs)
129     {
130         return true;
131     }
132 
133     /* ------------------------------------------------------------ */
134     /* 
135      * @see org.eclipse.io.EndPoint#close()
136      */
137     public void close() throws IOException
138     {
139         _closed=true;
140     }
141 
142     /* ------------------------------------------------------------ */
143     /* 
144      * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer)
145      */
146     public int fill(Buffer buffer) throws IOException
147     {
148         if (_closed)
149             throw new IOException("CLOSED");
150         if (_in==null)
151             return -1;
152         if (_in.length()<=0)
153             return _nonBlocking?0:-1;
154         int len = buffer.put(_in);
155         _in.skip(len);
156         return len;
157     }
158 
159     /* ------------------------------------------------------------ */
160     /* 
161      * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer)
162      */
163     public int flush(Buffer buffer) throws IOException
164     {
165         if (_closed)
166             throw new IOException("CLOSED");
167         if (_growOutput && buffer.length()>_out.space())
168         {
169             _out.compact();
170 
171             if (buffer.length()>_out.space())
172             {
173                 ByteArrayBuffer n = new ByteArrayBuffer(_out.putIndex()+buffer.length());
174 
175                 n.put(_out.peek(0,_out.putIndex()));
176                 if (_out.getIndex()>0)
177                 {
178                     n.mark();
179                     n.setGetIndex(_out.getIndex());
180                 }
181                 _out=n;
182             }
183         }
184         int len = _out.put(buffer);
185         buffer.skip(len);
186         return len;
187     }
188 
189     /* ------------------------------------------------------------ */
190     /* 
191      * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
192      */
193     public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
194     {
195         if (_closed)
196             throw new IOException("CLOSED");
197         
198         int flushed=0;
199         
200         if (header!=null && header.length()>0)
201             flushed=flush(header);
202         
203         if (header==null || header.length()==0)
204         {
205             if (buffer!=null && buffer.length()>0)
206                 flushed+=flush(buffer);
207             
208             if (buffer==null || buffer.length()==0)
209             {
210                 if (trailer!=null && trailer.length()>0)
211                 {
212                     flushed+=flush(trailer);
213                 }
214             }
215         }
216         
217         return flushed;
218     }
219 
220     /* ------------------------------------------------------------ */
221     /**
222      * 
223      */
224     public void reset()
225     {
226         _closed=false;
227         _in.clear();
228         _out.clear();
229         if (_inBytes!=null)
230             _in.setPutIndex(_inBytes.length);
231     }
232 
233     /* ------------------------------------------------------------ */
234     /* 
235      * @see org.eclipse.io.EndPoint#getLocalAddr()
236      */
237     public String getLocalAddr()
238     {
239         return null;
240     }
241 
242     /* ------------------------------------------------------------ */
243     /* 
244      * @see org.eclipse.io.EndPoint#getLocalHost()
245      */
246     public String getLocalHost()
247     {
248         return null;
249     }
250 
251     /* ------------------------------------------------------------ */
252     /* 
253      * @see org.eclipse.io.EndPoint#getLocalPort()
254      */
255     public int getLocalPort()
256     {
257         return 0;
258     }
259 
260     /* ------------------------------------------------------------ */
261     /* 
262      * @see org.eclipse.io.EndPoint#getRemoteAddr()
263      */
264     public String getRemoteAddr()
265     {
266         return null;
267     }
268 
269     /* ------------------------------------------------------------ */
270     /* 
271      * @see org.eclipse.io.EndPoint#getRemoteHost()
272      */
273     public String getRemoteHost()
274     {
275         return null;
276     }
277 
278     /* ------------------------------------------------------------ */
279     /* 
280      * @see org.eclipse.io.EndPoint#getRemotePort()
281      */
282     public int getRemotePort()
283     {
284         return 0;
285     }
286 
287     /* ------------------------------------------------------------ */
288     /* 
289      * @see org.eclipse.io.EndPoint#getConnection()
290      */
291     public Object getTransport()
292     {
293         return _inBytes;
294     }
295 
296     /* ------------------------------------------------------------ */
297     public void flush() throws IOException
298     {   
299     }
300 
301     /* ------------------------------------------------------------ */
302     public boolean isBufferingInput()
303     {
304         return false;
305     }
306 
307     /* ------------------------------------------------------------ */
308     public boolean isBufferingOutput()
309     {
310         return false;
311     }
312 
313     /* ------------------------------------------------------------ */
314     public boolean isBufferred()
315     {
316         return false;
317     }
318 
319     /* ------------------------------------------------------------ */
320     /**
321      * @return the growOutput
322      */
323     public boolean isGrowOutput()
324     {
325         return _growOutput;
326     }
327 
328     /* ------------------------------------------------------------ */
329     /**
330      * @param growOutput the growOutput to set
331      */
332     public void setGrowOutput(boolean growOutput)
333     {
334         _growOutput=growOutput;
335     }
336 
337 
338 }