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