1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.util.byteaccess;
21
22
23 import java.nio.ByteOrder;
24 import java.util.Collections;
25
26 import org.apache.mina.core.buffer.IoBuffer;
27
28
29
30
31
32
33
34
35
36
37 public abstract class BufferByteArray extends AbstractByteArray
38 {
39
40
41
42
43 protected IoBuffer bb;
44
45
46
47
48
49
50
51
52
53 public BufferByteArray( IoBuffer bb )
54 {
55 this.bb = bb;
56 }
57
58
59
60
61
62 public Iterable<IoBuffer> getIoBuffers()
63 {
64 return Collections.singletonList( bb );
65 }
66
67
68
69
70
71 public IoBuffer getSingleIoBuffer()
72 {
73 return bb;
74 }
75
76
77
78
79
80
81
82 public ByteArray slice( int index, int length )
83 {
84 int oldLimit = bb.limit();
85 bb.position( index );
86 bb.limit( index + length );
87 IoBuffer slice = bb.slice();
88 bb.limit( oldLimit );
89 return new BufferByteArray( slice )
90 {
91
92 @Override
93 public void free()
94 {
95
96 }
97 };
98 }
99
100
101
102
103
104 public abstract void free();
105
106
107
108
109
110 public Cursor cursor()
111 {
112 return new CursorImpl();
113 }
114
115
116
117
118
119 public Cursor cursor( int index )
120 {
121 return new CursorImpl( index );
122 }
123
124
125
126
127
128 public int first()
129 {
130 return 0;
131 }
132
133
134
135
136
137 public int last()
138 {
139 return bb.limit();
140 }
141
142
143
144
145
146 public ByteOrder order()
147 {
148 return bb.order();
149 }
150
151
152
153
154
155 public void order( ByteOrder order )
156 {
157 bb.order( order );
158 }
159
160
161
162
163
164 public byte get( int index )
165 {
166 return bb.get( index );
167 }
168
169
170
171
172
173 public void put( int index, byte b )
174 {
175 bb.put( index, b );
176 }
177
178
179
180
181
182 public void get( int index, IoBuffer other )
183 {
184 bb.position( index );
185 other.put( bb );
186 }
187
188
189
190
191
192 public void put( int index, IoBuffer other )
193 {
194 bb.position( index );
195 bb.put( other );
196 }
197
198
199
200
201
202 public short getShort( int index )
203 {
204 return bb.getShort( index );
205 }
206
207
208
209
210
211 public void putShort( int index, short s )
212 {
213 bb.putShort( index, s );
214 }
215
216
217
218
219
220 public int getInt( int index )
221 {
222 return bb.getInt( index );
223 }
224
225
226
227
228
229 public void putInt( int index, int i )
230 {
231 bb.putInt( index, i );
232 }
233
234
235
236
237
238 public long getLong( int index )
239 {
240 return bb.getLong( index );
241 }
242
243
244
245
246
247 public void putLong( int index, long l )
248 {
249 bb.putLong( index, l );
250 }
251
252
253
254
255
256 public float getFloat( int index )
257 {
258 return bb.getFloat( index );
259 }
260
261
262
263
264
265 public void putFloat( int index, float f )
266 {
267 bb.putFloat( index, f );
268 }
269
270
271
272
273
274 public double getDouble( int index )
275 {
276 return bb.getDouble( index );
277 }
278
279
280
281
282
283 public void putDouble( int index, double d )
284 {
285 bb.putDouble( index, d );
286 }
287
288
289
290
291
292 public char getChar( int index )
293 {
294 return bb.getChar( index );
295 }
296
297
298
299
300
301 public void putChar( int index, char c )
302 {
303 bb.putChar( index, c );
304 }
305
306 private class CursorImpl implements Cursor
307 {
308
309 private int index;
310
311
312 public CursorImpl()
313 {
314
315 }
316
317
318 public CursorImpl( int index )
319 {
320 setIndex( index );
321 }
322
323
324
325
326
327 public int getRemaining()
328 {
329 return last() - index;
330 }
331
332
333
334
335
336 public boolean hasRemaining()
337 {
338 return getRemaining() > 0;
339 }
340
341
342
343
344
345 public int getIndex()
346 {
347 return index;
348 }
349
350
351
352
353
354 public void setIndex( int index )
355 {
356 if ( index < 0 || index > last() )
357 {
358 throw new IndexOutOfBoundsException();
359 }
360 this.index = index;
361 }
362
363
364 public void skip( int length )
365 {
366 setIndex( index + length );
367 }
368
369
370 public ByteArray slice( int length )
371 {
372 ByteArray slice = BufferByteArray.this.slice( index, length );
373 index += length;
374 return slice;
375 }
376
377
378
379
380
381 public ByteOrder order()
382 {
383 return BufferByteArray.this.order();
384 }
385
386
387
388
389
390 public byte get()
391 {
392 byte b = BufferByteArray.this.get( index );
393 index += 1;
394 return b;
395 }
396
397
398
399
400
401 public void put( byte b )
402 {
403 BufferByteArray.this.put( index, b );
404 index += 1;
405 }
406
407
408
409
410
411 public void get( IoBuffer bb )
412 {
413 int size = Math.min( getRemaining(), bb.remaining() );
414 BufferByteArray.this.get( index, bb );
415 index += size;
416 }
417
418
419
420
421
422 public void put( IoBuffer bb )
423 {
424 int size = bb.remaining();
425 BufferByteArray.this.put( index, bb );
426 index += size;
427 }
428
429
430
431
432
433 public short getShort()
434 {
435 short s = BufferByteArray.this.getShort( index );
436 index += 2;
437 return s;
438 }
439
440
441
442
443
444 public void putShort( short s )
445 {
446 BufferByteArray.this.putShort( index, s );
447 index += 2;
448 }
449
450
451
452
453
454 public int getInt()
455 {
456 int i = BufferByteArray.this.getInt( index );
457 index += 4;
458 return i;
459 }
460
461
462
463
464
465 public void putInt( int i )
466 {
467 BufferByteArray.this.putInt( index, i );
468 index += 4;
469 }
470
471
472
473
474
475 public long getLong()
476 {
477 long l = BufferByteArray.this.getLong( index );
478 index += 8;
479 return l;
480 }
481
482
483
484
485
486 public void putLong( long l )
487 {
488 BufferByteArray.this.putLong( index, l );
489 index += 8;
490 }
491
492
493
494
495
496 public float getFloat()
497 {
498 float f = BufferByteArray.this.getFloat( index );
499 index += 4;
500 return f;
501 }
502
503
504
505
506
507 public void putFloat( float f )
508 {
509 BufferByteArray.this.putFloat( index, f );
510 index += 4;
511 }
512
513
514
515
516
517 public double getDouble()
518 {
519 double d = BufferByteArray.this.getDouble( index );
520 index += 8;
521 return d;
522 }
523
524
525
526
527
528 public void putDouble( double d )
529 {
530 BufferByteArray.this.putDouble( index, d );
531 index += 8;
532 }
533
534
535
536
537
538 public char getChar()
539 {
540 char c = BufferByteArray.this.getChar( index );
541 index += 2;
542 return c;
543 }
544
545
546
547
548
549 public void putChar( char c )
550 {
551 BufferByteArray.this.putChar( index, c );
552 index += 2;
553 }
554 }
555 }