comparison src/org/eclipse/jetty/io/ByteArrayEndPoint.java @ 909:c60c1adfac3e

simplify connectors
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 08 Oct 2016 21:15:28 -0600
parents 3428c60d7cfc
children
comparison
equal deleted inserted replaced
908:e003b20780c4 909:c60c1adfac3e
27 * 27 *
28 * 28 *
29 */ 29 */
30 public class ByteArrayEndPoint implements ConnectedEndPoint 30 public class ByteArrayEndPoint implements ConnectedEndPoint
31 { 31 {
32 protected byte[] _inBytes; 32 protected byte[] _inBytes;
33 protected ByteArrayBuffer _in; 33 protected ByteArrayBuffer _in;
34 protected ByteArrayBuffer _out; 34 protected ByteArrayBuffer _out;
35 protected boolean _closed; 35 protected boolean _closed;
36 protected boolean _nonBlocking; 36 protected boolean _nonBlocking;
37 protected boolean _growOutput; 37 protected boolean _growOutput;
38 protected Connection _connection; 38 protected Connection _connection;
39 protected int _maxIdleTime; 39 protected int _maxIdleTime;
40 40
41 41
42 /* ------------------------------------------------------------ */ 42 /* ------------------------------------------------------------ */
43 /** 43 /**
44 * 44 *
45 */ 45 */
46 public ByteArrayEndPoint() 46 public ByteArrayEndPoint()
47 { 47 {
48 } 48 }
49 49
50 /* ------------------------------------------------------------ */ 50 /* ------------------------------------------------------------ */
51 /** 51 /**
52 * @see org.eclipse.jetty.io.ConnectedEndPoint#getConnection() 52 * @see org.eclipse.jetty.io.ConnectedEndPoint#getConnection()
53 */ 53 */
54 public Connection getConnection() 54 public Connection getConnection()
55 { 55 {
56 return _connection; 56 return _connection;
57 } 57 }
58 58
59 /* ------------------------------------------------------------ */ 59 /* ------------------------------------------------------------ */
60 /** 60 /**
61 * @see org.eclipse.jetty.io.ConnectedEndPoint#setConnection(org.eclipse.jetty.io.Connection) 61 * @see org.eclipse.jetty.io.ConnectedEndPoint#setConnection(org.eclipse.jetty.io.Connection)
62 */ 62 */
63 public void setConnection(Connection connection) 63 public void setConnection(Connection connection)
64 { 64 {
65 _connection=connection; 65 _connection=connection;
66 } 66 }
67 67
68 /* ------------------------------------------------------------ */ 68 /* ------------------------------------------------------------ */
69 /** 69 /**
70 * @return the nonBlocking 70 * @return the nonBlocking
71 */ 71 */
72 public boolean isNonBlocking() 72 public boolean isNonBlocking()
73 { 73 {
74 return _nonBlocking; 74 return _nonBlocking;
75 } 75 }
76 76
77 /* ------------------------------------------------------------ */ 77 /* ------------------------------------------------------------ */
78 /** 78 /**
79 * @param nonBlocking the nonBlocking to set 79 * @param nonBlocking the nonBlocking to set
80 */ 80 */
81 public void setNonBlocking(boolean nonBlocking) 81 public void setNonBlocking(boolean nonBlocking)
82 { 82 {
83 _nonBlocking=nonBlocking; 83 _nonBlocking=nonBlocking;
84 } 84 }
85 85
86 /* ------------------------------------------------------------ */ 86 /* ------------------------------------------------------------ */
87 /** 87 /**
88 * 88 *
89 */ 89 */
90 public ByteArrayEndPoint(byte[] input, int outputSize) 90 public ByteArrayEndPoint(byte[] input, int outputSize)
91 { 91 {
92 _inBytes=input; 92 _inBytes=input;
93 _in=new ByteArrayBuffer(input); 93 _in=new ByteArrayBuffer(input);
94 _out=new ByteArrayBuffer(outputSize); 94 _out=new ByteArrayBuffer(outputSize);
95 } 95 }
96 96
97 /* ------------------------------------------------------------ */ 97 /* ------------------------------------------------------------ */
98 /** 98 /**
99 * @return Returns the in. 99 * @return Returns the in.
100 */ 100 */
101 public ByteArrayBuffer getIn() 101 public ByteArrayBuffer getIn()
102 { 102 {
103 return _in; 103 return _in;
104 } 104 }
105 /* ------------------------------------------------------------ */ 105 /* ------------------------------------------------------------ */
106 /** 106 /**
107 * @param in The in to set. 107 * @param in The in to set.
108 */ 108 */
109 public void setIn(ByteArrayBuffer in) 109 public void setIn(ByteArrayBuffer in)
110 { 110 {
111 _in = in; 111 _in = in;
112 } 112 }
113 /* ------------------------------------------------------------ */ 113 /* ------------------------------------------------------------ */
114 /** 114 /**
115 * @return Returns the out. 115 * @return Returns the out.
116 */ 116 */
117 public ByteArrayBuffer getOut() 117 public ByteArrayBuffer getOut()
118 { 118 {
119 return _out; 119 return _out;
120 } 120 }
121 /* ------------------------------------------------------------ */ 121 /* ------------------------------------------------------------ */
122 /** 122 /**
123 * @param out The out to set. 123 * @param out The out to set.
124 */ 124 */
125 public void setOut(ByteArrayBuffer out) 125 public void setOut(ByteArrayBuffer out)
126 { 126 {
127 _out = out; 127 _out = out;
128 } 128 }
129 129
130 /* ------------------------------------------------------------ */ 130 /* ------------------------------------------------------------ */
131 /* 131 /*
132 * @see org.eclipse.io.EndPoint#isOpen() 132 * @see org.eclipse.io.EndPoint#isOpen()
133 */ 133 */
134 public boolean isOpen() 134 public boolean isOpen()
135 { 135 {
136 return !_closed; 136 return !_closed;
137 } 137 }
138 138
139 /* ------------------------------------------------------------ */ 139 /* ------------------------------------------------------------ */
140 /* 140 /*
141 * @see org.eclipse.jetty.io.EndPoint#isInputShutdown() 141 * @see org.eclipse.jetty.io.EndPoint#isInputShutdown()
142 */ 142 */
143 public boolean isInputShutdown() 143 public boolean isInputShutdown()
144 { 144 {
145 return _closed; 145 return _closed;
146 } 146 }
147 147
148 /* ------------------------------------------------------------ */ 148 /* ------------------------------------------------------------ */
149 /* 149 /*
150 * @see org.eclipse.jetty.io.EndPoint#isOutputShutdown() 150 * @see org.eclipse.jetty.io.EndPoint#isOutputShutdown()
151 */ 151 */
152 public boolean isOutputShutdown() 152 public boolean isOutputShutdown()
153 { 153 {
154 return _closed; 154 return _closed;
155 } 155 }
156 156
157 /* ------------------------------------------------------------ */ 157 /* ------------------------------------------------------------ */
158 /* 158 /*
159 * @see org.eclipse.io.EndPoint#isBlocking() 159 * @see org.eclipse.io.EndPoint#isBlocking()
160 */ 160 */
161 public boolean isBlocking() 161 public boolean isBlocking()
162 { 162 {
163 return !_nonBlocking; 163 return !_nonBlocking;
164 } 164 }
165 165
166 /* ------------------------------------------------------------ */ 166 /* ------------------------------------------------------------ */
167 public boolean blockReadable(long millisecs) 167 public boolean blockReadable(long millisecs)
168 { 168 {
169 return true; 169 return true;
170 } 170 }
171 171
172 /* ------------------------------------------------------------ */ 172 /* ------------------------------------------------------------ */
173 public boolean blockWritable(long millisecs) 173 public boolean blockWritable(long millisecs)
174 { 174 {
175 return true; 175 return true;
176 } 176 }
177 177
178 /* ------------------------------------------------------------ */ 178 /* ------------------------------------------------------------ */
179 /* 179 /*
180 * @see org.eclipse.io.EndPoint#shutdownOutput() 180 * @see org.eclipse.io.EndPoint#shutdownOutput()
181 */ 181 */
182 public void shutdownOutput() throws IOException 182 public void shutdownOutput() throws IOException
183 { 183 {
184 close(); 184 close();
185 } 185 }
186 186
187 /* ------------------------------------------------------------ */ 187 /* ------------------------------------------------------------ */
188 /* 188 /*
189 * @see org.eclipse.io.EndPoint#shutdownInput() 189 * @see org.eclipse.io.EndPoint#shutdownInput()
190 */ 190 */
191 public void shutdownInput() throws IOException 191 public void shutdownInput() throws IOException
192 { 192 {
193 close(); 193 close();
194 } 194 }
195 195
196 /* ------------------------------------------------------------ */ 196 /* ------------------------------------------------------------ */
197 /* 197 /*
198 * @see org.eclipse.io.EndPoint#close() 198 * @see org.eclipse.io.EndPoint#close()
199 */ 199 */
200 public void close() throws IOException 200 public void close() throws IOException
201 { 201 {
202 _closed=true; 202 _closed=true;
203 } 203 }
204 204
205 /* ------------------------------------------------------------ */ 205 /* ------------------------------------------------------------ */
206 /* 206 /*
207 * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer) 207 * @see org.eclipse.io.EndPoint#fill(org.eclipse.io.Buffer)
208 */ 208 */
209 public int fill(Buffer buffer) throws IOException 209 public int fill(Buffer buffer) throws IOException
210 { 210 {
211 if (_closed) 211 if (_closed)
212 throw new IOException("CLOSED"); 212 throw new IOException("CLOSED");
213 213
214 if (_in!=null && _in.length()>0) 214 if (_in!=null && _in.length()>0)
215 { 215 {
216 int len = buffer.put(_in); 216 int len = buffer.put(_in);
217 _in.skip(len); 217 _in.skip(len);
218 return len; 218 return len;
219 } 219 }
220 220
221 if (_in!=null && _in.length()==0 && _nonBlocking) 221 if (_in!=null && _in.length()==0 && _nonBlocking)
222 return 0; 222 return 0;
223 223
224 close(); 224 close();
225 return -1; 225 return -1;
226 } 226 }
227 227
228 /* ------------------------------------------------------------ */ 228 /* ------------------------------------------------------------ */
229 /* 229 /*
230 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer) 230 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer)
231 */ 231 */
232 public int flush(Buffer buffer) throws IOException 232 public int flush(Buffer buffer) throws IOException
233 { 233 {
234 if (_closed) 234 if (_closed)
235 throw new IOException("CLOSED"); 235 throw new IOException("CLOSED");
236 if (_growOutput && buffer.length()>_out.space()) 236 if (_growOutput && buffer.length()>_out.space())
237 { 237 {
238 _out.compact(); 238 _out.compact();
239 239
240 if (buffer.length()>_out.space()) 240 if (buffer.length()>_out.space())
241 { 241 {
242 ByteArrayBuffer n = new ByteArrayBuffer(_out.putIndex()+buffer.length()); 242 ByteArrayBuffer n = new ByteArrayBuffer(_out.putIndex()+buffer.length());
243 243
244 n.put(_out.peek(0,_out.putIndex())); 244 n.put(_out.peek(0,_out.putIndex()));
245 if (_out.getIndex()>0) 245 if (_out.getIndex()>0)
246 { 246 {
247 n.mark(); 247 n.mark();
248 n.setGetIndex(_out.getIndex()); 248 n.setGetIndex(_out.getIndex());
249 } 249 }
250 _out=n; 250 _out=n;
251 } 251 }
252 } 252 }
253 int len = _out.put(buffer); 253 int len = _out.put(buffer);
254 if (!buffer.isImmutable()) 254 if (!buffer.isImmutable())
255 buffer.skip(len); 255 buffer.skip(len);
256 return len; 256 return len;
257 } 257 }
258 258
259 /* ------------------------------------------------------------ */ 259 /* ------------------------------------------------------------ */
260 /* 260 /*
261 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer) 261 * @see org.eclipse.io.EndPoint#flush(org.eclipse.io.Buffer, org.eclipse.io.Buffer, org.eclipse.io.Buffer)
262 */ 262 */
263 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException 263 public int flush(Buffer header, Buffer buffer, Buffer trailer) throws IOException
264 { 264 {
265 if (_closed) 265 if (_closed)
266 throw new IOException("CLOSED"); 266 throw new IOException("CLOSED");
267 267
268 int flushed=0; 268 int flushed=0;
269 269
270 if (header!=null && header.length()>0) 270 if (header!=null && header.length()>0)
271 flushed=flush(header); 271 flushed=flush(header);
272 272
273 if (header==null || header.length()==0) 273 if (header==null || header.length()==0)
274 { 274 {
275 if (buffer!=null && buffer.length()>0) 275 if (buffer!=null && buffer.length()>0)
276 flushed+=flush(buffer); 276 flushed+=flush(buffer);
277 277
278 if (buffer==null || buffer.length()==0) 278 if (buffer==null || buffer.length()==0)
279 { 279 {
280 if (trailer!=null && trailer.length()>0) 280 if (trailer!=null && trailer.length()>0)
281 { 281 {
282 flushed+=flush(trailer); 282 flushed+=flush(trailer);
283 } 283 }
284 } 284 }
285 } 285 }
286 286
287 return flushed; 287 return flushed;
288 } 288 }
289 289
290 /* ------------------------------------------------------------ */ 290 /* ------------------------------------------------------------ */
291 /** 291 /**
292 * 292 *
293 */ 293 */
294 public void reset() 294 public void reset()
295 { 295 {
296 _closed=false; 296 _closed=false;
297 _in.clear(); 297 _in.clear();
298 _out.clear(); 298 _out.clear();
299 if (_inBytes!=null) 299 if (_inBytes!=null)
300 _in.setPutIndex(_inBytes.length); 300 _in.setPutIndex(_inBytes.length);
301 } 301 }
302 302
303 /* ------------------------------------------------------------ */ 303 /* ------------------------------------------------------------ */
304 /* 304 /*
305 * @see org.eclipse.io.EndPoint#getLocalAddr() 305 * @see org.eclipse.io.EndPoint#getLocalAddr()
306 */ 306 */
307 public String getLocalAddr() 307 public String getLocalAddr()
308 { 308 {
309 return null; 309 return null;
310 } 310 }
311 311
312 /* ------------------------------------------------------------ */ 312 /* ------------------------------------------------------------ */
313 /* 313 /*
314 * @see org.eclipse.io.EndPoint#getLocalHost() 314 * @see org.eclipse.io.EndPoint#getLocalHost()
315 */ 315 */
316 public String getLocalHost() 316 public String getLocalHost()
317 { 317 {
318 return null; 318 return null;
319 } 319 }
320 320
321 /* ------------------------------------------------------------ */ 321 /* ------------------------------------------------------------ */
322 /* 322 /*
323 * @see org.eclipse.io.EndPoint#getLocalPort() 323 * @see org.eclipse.io.EndPoint#getLocalPort()
324 */ 324 */
325 public int getLocalPort() 325 public int getLocalPort()
326 { 326 {
327 return 0; 327 return 0;
328 } 328 }
329 329
330 /* ------------------------------------------------------------ */ 330 /* ------------------------------------------------------------ */
331 /* 331 /*
332 * @see org.eclipse.io.EndPoint#getRemoteAddr() 332 * @see org.eclipse.io.EndPoint#getRemoteAddr()
333 */ 333 */
334 public String getRemoteAddr() 334 public String getRemoteAddr()
335 { 335 {
336 return null; 336 return null;
337 } 337 }
338 338
339 /* ------------------------------------------------------------ */ 339 /* ------------------------------------------------------------ */
340 /* 340 /*
341 * @see org.eclipse.io.EndPoint#getRemoteHost() 341 * @see org.eclipse.io.EndPoint#getRemoteHost()
342 */ 342 */
343 public String getRemoteHost() 343 public String getRemoteHost()
344 { 344 {
345 return null; 345 return null;
346 } 346 }
347 347
348 /* ------------------------------------------------------------ */ 348 /* ------------------------------------------------------------ */
349 /* 349 /*
350 * @see org.eclipse.io.EndPoint#getRemotePort() 350 * @see org.eclipse.io.EndPoint#getRemotePort()
351 */ 351 */
352 public int getRemotePort() 352 public int getRemotePort()
353 { 353 {
354 return 0; 354 return 0;
355 } 355 }
356 356
357 /* ------------------------------------------------------------ */ 357 /* ------------------------------------------------------------ */
358 /* 358 /*
359 * @see org.eclipse.io.EndPoint#getConnection() 359 * @see org.eclipse.io.EndPoint#getConnection()
360 */ 360 */
361 public Object getTransport() 361 public Object getTransport()
362 { 362 {
363 return _inBytes; 363 return _inBytes;
364 } 364 }
365 365
366 /* ------------------------------------------------------------ */ 366 /* ------------------------------------------------------------ */
367 public void flush() throws IOException 367 public void flush() throws IOException
368 { 368 {
369 } 369 }
370 370
371 /* ------------------------------------------------------------ */ 371 /* ------------------------------------------------------------ */
372 /** 372 /**
373 * @return the growOutput 373 * @return the growOutput
374 */ 374 */
375 public boolean isGrowOutput() 375 public boolean isGrowOutput()
376 { 376 {
377 return _growOutput; 377 return _growOutput;
378 } 378 }
379 379
380 /* ------------------------------------------------------------ */ 380 /* ------------------------------------------------------------ */
381 /** 381 /**
382 * @param growOutput the growOutput to set 382 * @param growOutput the growOutput to set
383 */ 383 */
384 public void setGrowOutput(boolean growOutput) 384 public void setGrowOutput(boolean growOutput)
385 { 385 {
386 _growOutput=growOutput; 386 _growOutput=growOutput;
387 } 387 }
388 388
389 /* ------------------------------------------------------------ */ 389 /* ------------------------------------------------------------ */
390 /** 390 /**
391 * @see org.eclipse.jetty.io.EndPoint#getMaxIdleTime() 391 * @see org.eclipse.jetty.io.EndPoint#getMaxIdleTime()
392 */ 392 */
393 public int getMaxIdleTime() 393 public int getMaxIdleTime()
394 { 394 {
395 return _maxIdleTime; 395 return _maxIdleTime;
396 } 396 }
397 397
398 /* ------------------------------------------------------------ */ 398 /* ------------------------------------------------------------ */
399 /** 399 /**
400 * @see org.eclipse.jetty.io.EndPoint#setMaxIdleTime(int) 400 * @see org.eclipse.jetty.io.EndPoint#setMaxIdleTime(int)
401 */ 401 */
402 public void setMaxIdleTime(int timeMs) throws IOException 402 public void setMaxIdleTime(int timeMs) throws IOException
403 { 403 {
404 _maxIdleTime=timeMs; 404 _maxIdleTime=timeMs;
405 } 405 }
406 406
407 407
408 } 408 }