1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.io;
20
21 import java.io.IOException;
22
23
24
25
26
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
53
54 public Connection getConnection()
55 {
56 return _connection;
57 }
58
59
60
61
62
63 public void setConnection(Connection connection)
64 {
65 _connection=connection;
66 }
67
68
69
70
71
72 public boolean isNonBlocking()
73 {
74 return _nonBlocking;
75 }
76
77
78
79
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
100
101 public ByteArrayBuffer getIn()
102 {
103 return _in;
104 }
105
106
107
108
109 public void setIn(ByteArrayBuffer in)
110 {
111 _in = in;
112 }
113
114
115
116
117 public ByteArrayBuffer getOut()
118 {
119 return _out;
120 }
121
122
123
124
125 public void setOut(ByteArrayBuffer out)
126 {
127 _out = out;
128 }
129
130
131
132
133
134 public boolean isOpen()
135 {
136 return !_closed;
137 }
138
139
140
141
142
143 public boolean isInputShutdown()
144 {
145 return _closed;
146 }
147
148
149
150
151
152 public boolean isOutputShutdown()
153 {
154 return _closed;
155 }
156
157
158
159
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
181
182 public void shutdownOutput() throws IOException
183 {
184 close();
185 }
186
187
188
189
190
191 public void shutdownInput() throws IOException
192 {
193 close();
194 }
195
196
197
198
199
200 public void close() throws IOException
201 {
202 _closed=true;
203 }
204
205
206
207
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
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
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
306
307 public String getLocalAddr()
308 {
309 return null;
310 }
311
312
313
314
315
316 public String getLocalHost()
317 {
318 return null;
319 }
320
321
322
323
324
325 public int getLocalPort()
326 {
327 return 0;
328 }
329
330
331
332
333
334 public String getRemoteAddr()
335 {
336 return null;
337 }
338
339
340
341
342
343 public String getRemoteHost()
344 {
345 return null;
346 }
347
348
349
350
351
352 public int getRemotePort()
353 {
354 return 0;
355 }
356
357
358
359
360
361 public Object getTransport()
362 {
363 return _inBytes;
364 }
365
366
367 public void flush() throws IOException
368 {
369 }
370
371
372
373
374
375 public boolean isGrowOutput()
376 {
377 return _growOutput;
378 }
379
380
381
382
383
384 public void setGrowOutput(boolean growOutput)
385 {
386 _growOutput=growOutput;
387 }
388
389
390
391
392
393 public int getMaxIdleTime()
394 {
395 return _maxIdleTime;
396 }
397
398
399
400
401
402 public void setMaxIdleTime(int timeMs) throws IOException
403 {
404 _maxIdleTime=timeMs;
405 }
406
407
408 }