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 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
45
46 public boolean isNonBlocking()
47 {
48 return _nonBlocking;
49 }
50
51
52
53
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
74
75 public ByteArrayBuffer getIn()
76 {
77 return _in;
78 }
79
80
81
82
83 public void setIn(ByteArrayBuffer in)
84 {
85 _in = in;
86 }
87
88
89
90
91 public ByteArrayBuffer getOut()
92 {
93 return _out;
94 }
95
96
97
98
99 public void setOut(ByteArrayBuffer out)
100 {
101 _out = out;
102 }
103
104
105
106
107 public boolean isOpen()
108 {
109 return !_closed;
110 }
111
112
113
114
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
136
137 public void close() throws IOException
138 {
139 _closed=true;
140 }
141
142
143
144
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
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
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
236
237 public String getLocalAddr()
238 {
239 return null;
240 }
241
242
243
244
245
246 public String getLocalHost()
247 {
248 return null;
249 }
250
251
252
253
254
255 public int getLocalPort()
256 {
257 return 0;
258 }
259
260
261
262
263
264 public String getRemoteAddr()
265 {
266 return null;
267 }
268
269
270
271
272
273 public String getRemoteHost()
274 {
275 return null;
276 }
277
278
279
280
281
282 public int getRemotePort()
283 {
284 return 0;
285 }
286
287
288
289
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
322
323 public boolean isGrowOutput()
324 {
325 return _growOutput;
326 }
327
328
329
330
331
332 public void setGrowOutput(boolean growOutput)
333 {
334 _growOutput=growOutput;
335 }
336
337
338 }