1
2
3
4
5
6
7
8
9
10
11
12
13
14 package org.eclipse.jetty.io;
15
16 import java.io.IOException;
17
18
19
20
21
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
48
49 public Connection getConnection()
50 {
51 return _connection;
52 }
53
54
55
56
57
58 public void setConnection(Connection connection)
59 {
60 _connection=connection;
61 }
62
63
64
65
66
67 public boolean isNonBlocking()
68 {
69 return _nonBlocking;
70 }
71
72
73
74
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
95
96 public ByteArrayBuffer getIn()
97 {
98 return _in;
99 }
100
101
102
103
104 public void setIn(ByteArrayBuffer in)
105 {
106 _in = in;
107 }
108
109
110
111
112 public ByteArrayBuffer getOut()
113 {
114 return _out;
115 }
116
117
118
119
120 public void setOut(ByteArrayBuffer out)
121 {
122 _out = out;
123 }
124
125
126
127
128 public boolean isOpen()
129 {
130 return !_closed;
131 }
132
133
134
135
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
157
158 public void close() throws IOException
159 {
160 _closed=true;
161 }
162
163
164
165
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
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
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
257
258 public String getLocalAddr()
259 {
260 return null;
261 }
262
263
264
265
266
267 public String getLocalHost()
268 {
269 return null;
270 }
271
272
273
274
275
276 public int getLocalPort()
277 {
278 return 0;
279 }
280
281
282
283
284
285 public String getRemoteAddr()
286 {
287 return null;
288 }
289
290
291
292
293
294 public String getRemoteHost()
295 {
296 return null;
297 }
298
299
300
301
302
303 public int getRemotePort()
304 {
305 return 0;
306 }
307
308
309
310
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
343
344 public boolean isGrowOutput()
345 {
346 return _growOutput;
347 }
348
349
350
351
352
353 public void setGrowOutput(boolean growOutput)
354 {
355 _growOutput=growOutput;
356 }
357
358
359
360
361
362 public int getMaxIdleTime()
363 {
364 return _maxIdleTime;
365 }
366
367
368
369
370
371 public void setMaxIdleTime(int timeMs) throws IOException
372 {
373 _maxIdleTime=timeMs;
374 }
375
376
377 }