View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
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   * A <code>ByteArray</code> backed by a <code>IoBuffer</code>. This class
31   * is abstract. Subclasses need to override the <code>free()</code> method. An
32   * implementation backed by a heap <code>IoBuffer</code> can be created with
33   * a <code>SimpleByteArrayFactory</code>.
34   *
35   * @author The Apache MINA Project (dev@mina.apache.org)
36   * @version $Rev$, $Date$
37   */
38  public abstract class BufferByteArray extends AbstractByteArray
39  {
40  
41      /**
42       * The backing <code>IoBuffer</code>.
43       */
44      protected IoBuffer bb;
45  
46      /**
47       * 
48       * Creates a new instance of BufferByteArray and uses the supplied
49       * {@link IoBuffer} to back this class
50       *
51       * @param bb
52       *  The backing buffer
53       */
54      public BufferByteArray( IoBuffer bb )
55      {
56          this.bb = bb;
57      }
58  
59  
60      /**
61       * @inheritDoc
62       */
63      public Iterable<IoBuffer> getIoBuffers()
64      {
65          return Collections.singletonList( bb );
66      }
67  
68  
69      /**
70       * @inheritDoc
71       */
72      public IoBuffer getSingleIoBuffer()
73      {
74          return bb;
75      }
76  
77  
78      /**
79       * @inheritDoc
80       * 
81       * Calling <code>free()</code> on the returned slice has no effect.
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                  // Do nothing.
97              }
98          };
99      }
100 
101 
102     /**
103      * @inheritDoc
104      */
105     public abstract void free();
106 
107 
108     /**
109      * @inheritDoc
110      */
111     public Cursor cursor()
112     {
113         return new CursorImpl();
114     }
115 
116 
117     /**
118      * @inheritDoc
119      */
120     public Cursor cursor( int index )
121     {
122         return new CursorImpl( index );
123     }
124 
125 
126     /**
127      * @inheritDoc
128      */
129     public int first()
130     {
131         return 0;
132     }
133 
134 
135     /**
136      * @inheritDoc
137      */
138     public int last()
139     {
140         return bb.limit();
141     }
142 
143 
144     /**
145      * @inheritDoc
146      */
147     public ByteOrder order()
148     {
149         return bb.order();
150     }
151 
152 
153     /**
154      * @inheritDoc
155      */
156     public void order( ByteOrder order )
157     {
158         bb.order( order );
159     }
160 
161 
162     /**
163      * @inheritDoc
164      */
165     public byte get( int index )
166     {
167         return bb.get( index );
168     }
169 
170 
171     /**
172      * @inheritDoc
173      */
174     public void put( int index, byte b )
175     {
176         bb.put( index, b );
177     }
178 
179 
180     /**
181      * @inheritDoc
182      */
183     public void get( int index, IoBuffer other )
184     {
185         bb.position( index );
186         other.put( bb );
187     }
188 
189 
190     /**
191      * @inheritDoc
192      */
193     public void put( int index, IoBuffer other )
194     {
195         bb.position( index );
196         bb.put( other );
197     }
198 
199 
200     /**
201      * @inheritDoc
202      */
203     public short getShort( int index )
204     {
205         return bb.getShort( index );
206     }
207 
208 
209     /**
210      * @inheritDoc
211      */
212     public void putShort( int index, short s )
213     {
214         bb.putShort( index, s );
215     }
216 
217 
218     /**
219      * @inheritDoc
220      */
221     public int getInt( int index )
222     {
223         return bb.getInt( index );
224     }
225 
226 
227     /**
228      * @inheritDoc
229      */
230     public void putInt( int index, int i )
231     {
232         bb.putInt( index, i );
233     }
234 
235 
236     /**
237      * @inheritDoc
238      */
239     public long getLong( int index )
240     {
241         return bb.getLong( index );
242     }
243 
244 
245     /**
246      * @inheritDoc
247      */
248     public void putLong( int index, long l )
249     {
250         bb.putLong( index, l );
251     }
252 
253 
254     /**
255      * @inheritDoc
256      */
257     public float getFloat( int index )
258     {
259         return bb.getFloat( index );
260     }
261 
262 
263     /**
264      * @inheritDoc
265      */
266     public void putFloat( int index, float f )
267     {
268         bb.putFloat( index, f );
269     }
270 
271 
272     /**
273      * @inheritDoc
274      */
275     public double getDouble( int index )
276     {
277         return bb.getDouble( index );
278     }
279 
280 
281     /**
282      * @inheritDoc
283      */
284     public void putDouble( int index, double d )
285     {
286         bb.putDouble( index, d );
287     }
288 
289 
290     /**
291      * @inheritDoc
292      */
293     public char getChar( int index )
294     {
295         return bb.getChar( index );
296     }
297 
298 
299     /**
300      * @inheritDoc
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             // This space intentionally blank.
316         }
317 
318 
319         public CursorImpl( int index )
320         {
321             setIndex( index );
322         }
323 
324 
325         /**
326          * @inheritDoc
327          */
328         public int getRemaining()
329         {
330             return last() - index;
331         }
332 
333 
334         /**
335          * @inheritDoc
336          */
337         public boolean hasRemaining()
338         {
339             return getRemaining() > 0;
340         }
341 
342 
343         /**
344          * @inheritDoc
345          */
346         public int getIndex()
347         {
348             return index;
349         }
350 
351 
352         /**
353          * @inheritDoc
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          * @inheritDoc
381          */
382         public ByteOrder order()
383         {
384             return BufferByteArray.this.order();
385         }
386 
387 
388         /**
389          * @inheritDoc
390          */
391         public byte get()
392         {
393             byte b = BufferByteArray.this.get( index );
394             index += 1;
395             return b;
396         }
397 
398 
399         /**
400          * @inheritDoc
401          */
402         public void put( byte b )
403         {
404             BufferByteArray.this.put( index, b );
405             index += 1;
406         }
407 
408 
409         /**
410          * @inheritDoc
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          * @inheritDoc
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          * @inheritDoc
433          */
434         public short getShort()
435         {
436             short s = BufferByteArray.this.getShort( index );
437             index += 2;
438             return s;
439         }
440 
441 
442         /**
443          * @inheritDoc
444          */
445         public void putShort( short s )
446         {
447             BufferByteArray.this.putShort( index, s );
448             index += 2;
449         }
450 
451 
452         /**
453          * @inheritDoc
454          */
455         public int getInt()
456         {
457             int i = BufferByteArray.this.getInt( index );
458             index += 4;
459             return i;
460         }
461 
462 
463         /**
464          * @inheritDoc
465          */
466         public void putInt( int i )
467         {
468             BufferByteArray.this.putInt( index, i );
469             index += 4;
470         }
471 
472 
473         /**
474          * @inheritDoc
475          */
476         public long getLong()
477         {
478             long l = BufferByteArray.this.getLong( index );
479             index += 8;
480             return l;
481         }
482 
483 
484         /**
485          * @inheritDoc
486          */
487         public void putLong( long l )
488         {
489             BufferByteArray.this.putLong( index, l );
490             index += 8;
491         }
492 
493 
494         /**
495          * @inheritDoc
496          */
497         public float getFloat()
498         {
499             float f = BufferByteArray.this.getFloat( index );
500             index += 4;
501             return f;
502         }
503 
504 
505         /**
506          * @inheritDoc
507          */
508         public void putFloat( float f )
509         {
510             BufferByteArray.this.putFloat( index, f );
511             index += 4;
512         }
513 
514 
515         /**
516          * @inheritDoc
517          */
518         public double getDouble()
519         {
520             double d = BufferByteArray.this.getDouble( index );
521             index += 8;
522             return d;
523         }
524 
525 
526         /**
527          * @inheritDoc
528          */
529         public void putDouble( double d )
530         {
531             BufferByteArray.this.putDouble( index, d );
532             index += 8;
533         }
534 
535 
536         /**
537          * @inheritDoc
538          */
539         public char getChar()
540         {
541             char c = BufferByteArray.this.getChar( index );
542             index += 2;
543             return c;
544         }
545 
546 
547         /**
548          * @inheritDoc
549          */
550         public void putChar( char c )
551         {
552             BufferByteArray.this.putChar( index, c );
553             index += 2;
554         }
555     }
556 }