Mercurial Hosting > luan
annotate src/org/eclipse/jetty/io/ByteArrayEndPoint.java @ 913:17f4fe8271de
simplify connectors
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sat, 08 Oct 2016 23:57:11 -0600 |
parents | c60c1adfac3e |
children |
rev | line source |
---|---|
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
1 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
2 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
4 // ------------------------------------------------------------------------ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
5 // All rights reserved. This program and the accompanying materials |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
6 // are made available under the terms of the Eclipse Public License v1.0 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
7 // and Apache License v2.0 which accompanies this distribution. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
8 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
9 // The Eclipse Public License is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
10 // http://www.eclipse.org/legal/epl-v10.html |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
11 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
12 // The Apache License v2.0 is available at |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
13 // http://www.opensource.org/licenses/apache2.0.php |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
14 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
15 // You may elect to redistribute this code under either of these licenses. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
16 // ======================================================================== |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
17 // |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
18 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
19 package org.eclipse.jetty.io; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
20 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
22 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
23 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
24 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
25 /* ------------------------------------------------------------ */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
26 /** ByteArrayEndPoint. |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
27 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
28 * |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
29 */ |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
30 public class ByteArrayEndPoint implements ConnectedEndPoint |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
31 { |
909 | 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; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
40 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
41 |
909 | 42 /* ------------------------------------------------------------ */ |
43 /** | |
44 * | |
45 */ | |
46 public ByteArrayEndPoint() | |
47 { | |
48 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
49 |
909 | 50 /* ------------------------------------------------------------ */ |
51 /** | |
52 * @see org.eclipse.jetty.io.ConnectedEndPoint#getConnection() | |
53 */ | |
54 public Connection getConnection() | |
55 { | |
56 return _connection; | |
57 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
58 |
909 | 59 /* ------------------------------------------------------------ */ |
60 /** | |
61 * @see org.eclipse.jetty.io.ConnectedEndPoint#setConnection(org.eclipse.jetty.io.Connection) | |
62 */ | |
63 public void setConnection(Connection connection) | |
64 { | |
65 _connection=connection; | |
66 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
67 |
909 | 68 /* ------------------------------------------------------------ */ |
69 /** | |
70 * @return the nonBlocking | |
71 */ | |
72 public boolean isNonBlocking() | |
73 { | |
74 return _nonBlocking; | |
75 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
76 |
909 | 77 /* ------------------------------------------------------------ */ |
78 /** | |
79 * @param nonBlocking the nonBlocking to set | |
80 */ | |
81 public void setNonBlocking(boolean nonBlocking) | |
82 { | |
83 _nonBlocking=nonBlocking; | |
84 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
85 |
909 | 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 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
96 |
909 | 97 /* ------------------------------------------------------------ */ |
98 /** | |
99 * @return Returns the in. | |
100 */ | |
101 public ByteArrayBuffer getIn() | |
102 { | |
103 return _in; | |
104 } | |
105 /* ------------------------------------------------------------ */ | |
106 /** | |
107 * @param in The in to set. | |
108 */ | |
109 public void setIn(ByteArrayBuffer in) | |
110 { | |
111 _in = in; | |
112 } | |
113 /* ------------------------------------------------------------ */ | |
114 /** | |
115 * @return Returns the out. | |
116 */ | |
117 public ByteArrayBuffer getOut() | |
118 { | |
119 return _out; | |
120 } | |
121 /* ------------------------------------------------------------ */ | |
122 /** | |
123 * @param out The out to set. | |
124 */ | |
125 public void setOut(ByteArrayBuffer out) | |
126 { | |
127 _out = out; | |
128 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
129 |
909 | 130 /* ------------------------------------------------------------ */ |
131 /* | |
132 * @see org.eclipse.io.EndPoint#isOpen() | |
133 */ | |
134 public boolean isOpen() | |
135 { | |
136 return !_closed; | |
137 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
138 |
909 | 139 /* ------------------------------------------------------------ */ |
140 /* | |
141 * @see org.eclipse.jetty.io.EndPoint#isInputShutdown() | |
142 */ | |
143 public boolean isInputShutdown() | |
144 { | |
145 return _closed; | |
146 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
147 |
909 | 148 /* ------------------------------------------------------------ */ |
149 /* | |
150 * @see org.eclipse.jetty.io.EndPoint#isOutputShutdown() | |
151 */ | |
152 public boolean isOutputShutdown() | |
153 { | |
154 return _closed; | |
155 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
156 |
909 | 157 /* ------------------------------------------------------------ */ |
158 /* | |
159 * @see org.eclipse.io.EndPoint#isBlocking() | |
160 */ | |
161 public boolean isBlocking() | |
162 { | |
163 return !_nonBlocking; | |
164 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
165 |
909 | 166 /* ------------------------------------------------------------ */ |
167 public boolean blockReadable(long millisecs) | |
168 { | |
169 return true; | |
170 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
171 |
909 | 172 /* ------------------------------------------------------------ */ |
173 public boolean blockWritable(long millisecs) | |
174 { | |
175 return true; | |
176 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
177 |
909 | 178 /* ------------------------------------------------------------ */ |
179 /* | |
180 * @see org.eclipse.io.EndPoint#shutdownOutput() | |
181 */ | |
182 public void shutdownOutput() throws IOException | |
183 { | |
184 close(); | |
185 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
186 |
909 | 187 /* ------------------------------------------------------------ */ |
188 /* | |
189 * @see org.eclipse.io.EndPoint#shutdownInput() | |
190 */ | |
191 public void shutdownInput() throws IOException | |
192 { | |
193 close(); | |
194 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
195 |
909 | 196 /* ------------------------------------------------------------ */ |
197 /* | |
198 * @see org.eclipse.io.EndPoint#close() | |
199 */ | |
200 public void close() throws IOException | |
201 { | |
202 _closed=true; | |
203 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
204 |
909 | 205 /* ------------------------------------------------------------ */ |
206 /* | |
207 * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer) | |
208 */ | |
209 public int fill(Buffer buffer) throws IOException | |
210 { | |
211 if (_closed) | |
212 throw new IOException("CLOSED"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
213 |
909 | 214 if (_in!=null && _in.length()>0) |
215 { | |
216 int len = buffer.put(_in); | |
217 _in.skip(len); | |
218 return len; | |
219 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
220 |
909 | 221 if (_in!=null && _in.length()==0 && _nonBlocking) |
222 return 0; | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
223 |
909 | 224 close(); |
225 return -1; | |
226 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
227 |
909 | 228 /* ------------------------------------------------------------ */ |
229 /* | |
230 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer) | |
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(); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
239 |
909 | 240 if (buffer.length()>_out.space()) |
241 { | |
242 ByteArrayBuffer n = new ByteArrayBuffer(_out.putIndex()+buffer.length()); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
243 |
909 | 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 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
258 |
909 | 259 /* ------------------------------------------------------------ */ |
260 /* | |
261 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer) | |
262 */ | |
263 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException | |
264 { | |
265 if (_closed) | |
266 throw new IOException("CLOSED"); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
267 |
909 | 268 int flushed=0; |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
269 |
909 | 270 if (header!=null && header.length()>0) |
271 flushed=flush(header); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
272 |
909 | 273 if (header==null || header.length()==0) |
274 { | |
275 if (buffer!=null && buffer.length()>0) | |
276 flushed+=flush(buffer); | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
277 |
909 | 278 if (buffer==null || buffer.length()==0) |
279 { | |
280 if (trailer!=null && trailer.length()>0) | |
281 { | |
282 flushed+=flush(trailer); | |
283 } | |
284 } | |
285 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
286 |
909 | 287 return flushed; |
288 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
289 |
909 | 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 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
302 |
909 | 303 /* ------------------------------------------------------------ */ |
304 /* | |
305 * @see org.eclipse.io.EndPoint#getLocalAddr() | |
306 */ | |
307 public String getLocalAddr() | |
308 { | |
309 return null; | |
310 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
311 |
909 | 312 /* ------------------------------------------------------------ */ |
313 /* | |
314 * @see org.eclipse.io.EndPoint#getLocalHost() | |
315 */ | |
316 public String getLocalHost() | |
317 { | |
318 return null; | |
319 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
320 |
909 | 321 /* ------------------------------------------------------------ */ |
322 /* | |
323 * @see org.eclipse.io.EndPoint#getLocalPort() | |
324 */ | |
325 public int getLocalPort() | |
326 { | |
327 return 0; | |
328 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
329 |
909 | 330 /* ------------------------------------------------------------ */ |
331 /* | |
332 * @see org.eclipse.io.EndPoint#getRemoteAddr() | |
333 */ | |
334 public String getRemoteAddr() | |
335 { | |
336 return null; | |
337 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
338 |
909 | 339 /* ------------------------------------------------------------ */ |
340 /* | |
341 * @see org.eclipse.io.EndPoint#getRemoteHost() | |
342 */ | |
343 public String getRemoteHost() | |
344 { | |
345 return null; | |
346 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
347 |
909 | 348 /* ------------------------------------------------------------ */ |
349 /* | |
350 * @see org.eclipse.io.EndPoint#getRemotePort() | |
351 */ | |
352 public int getRemotePort() | |
353 { | |
354 return 0; | |
355 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
356 |
909 | 357 /* ------------------------------------------------------------ */ |
358 /* | |
359 * @see org.eclipse.io.EndPoint#getConnection() | |
360 */ | |
361 public Object getTransport() | |
362 { | |
363 return _inBytes; | |
364 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
365 |
909 | 366 /* ------------------------------------------------------------ */ |
367 public void flush() throws IOException | |
368 { | |
369 } | |
370 | |
371 /* ------------------------------------------------------------ */ | |
372 /** | |
373 * @return the growOutput | |
374 */ | |
375 public boolean isGrowOutput() | |
376 { | |
377 return _growOutput; | |
378 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
379 |
909 | 380 /* ------------------------------------------------------------ */ |
381 /** | |
382 * @param growOutput the growOutput to set | |
383 */ | |
384 public void setGrowOutput(boolean growOutput) | |
385 { | |
386 _growOutput=growOutput; | |
387 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
388 |
909 | 389 /* ------------------------------------------------------------ */ |
390 /** | |
391 * @see org.eclipse.jetty.io.EndPoint#getMaxIdleTime() | |
392 */ | |
393 public int getMaxIdleTime() | |
394 { | |
395 return _maxIdleTime; | |
396 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
397 |
909 | 398 /* ------------------------------------------------------------ */ |
399 /** | |
400 * @see org.eclipse.jetty.io.EndPoint#setMaxIdleTime(int) | |
401 */ | |
402 public void setMaxIdleTime(int timeMs) throws IOException | |
403 { | |
404 _maxIdleTime=timeMs; | |
405 } | |
802
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
406 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
407 |
3428c60d7cfc
replace jetty jars with source
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff
changeset
|
408 } |