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