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