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