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