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 org.apache.mina.core.buffer.IoBuffer;
24  
25  
26  /**
27   * Provides restricted, relative, read-only access to the bytes in a
28   * <code>CompositeByteArray</code>. Using this interface has the advantage
29   * that it can be automatically determined when a component
30   * <code>ByteArray</code> can no longer be read, and thus components can be
31   * automatically freed. This makes it easier to use pooling for underlying
32   * <code>ByteArray</code>s.
33   * 
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev$, $Date$
36   */
37  public class CompositeByteArrayRelativeReader extends CompositeByteArrayRelativeBase implements IoRelativeReader
38  {
39  
40      /**
41       * Whether or not to free component <code>CompositeByteArray</code>s when
42       * the cursor moves past them.
43       */
44      private final boolean autoFree;
45  
46      /**
47       * 
48       * Creates a new instance of CompositeByteArrayRelativeReader.
49       *
50       * @param cba
51       *  The backing ByteArray
52       * @param autoFree
53       *  If data should be freed once it has been passed in the list
54       */
55      public CompositeByteArrayRelativeReader( CompositeByteArray cba, boolean autoFree )
56      {
57          super( cba );
58          this.autoFree = autoFree;
59      }
60  
61  
62      @Override
63      protected void cursorPassedFirstComponent()
64      {
65          if ( autoFree )
66          {
67              cba.removeFirst().free();
68          }
69      }
70  
71  
72      /**
73       * @inheritDoc
74       */
75      public void skip( int length )
76      {
77          cursor.skip( length );
78      }
79  
80  
81      /**
82       * @inheritDoc
83       */
84      public ByteArray slice( int length )
85      {
86          return cursor.slice( length );
87      }
88  
89      /**
90       * Returns the byte at the current position in the buffer
91       * 
92       */
93      public byte get()
94      {
95          return cursor.get();
96      }
97  
98      /**
99       * places the data starting at current position into the
100      * supplied {@link IoBuffer}
101      */
102     public void get( IoBuffer bb )
103     {
104         cursor.get( bb );
105     }
106 
107 
108     /**
109      * @inheritDoc
110      */
111     public short getShort()
112     {
113         return cursor.getShort();
114     }
115 
116 
117     /**
118      * @inheritDoc
119      */
120     public int getInt()
121     {
122         return cursor.getInt();
123     }
124 
125 
126     /**
127      * @inheritDoc
128      */
129     public long getLong()
130     {
131         return cursor.getLong();
132     }
133 
134 
135     /**
136      * @inheritDoc
137      */
138     public float getFloat()
139     {
140         return cursor.getFloat();
141     }
142 
143 
144     /**
145      * @inheritDoc
146      */
147     public double getDouble()
148     {
149         return cursor.getDouble();
150     }
151 
152 
153     /**
154      * @inheritDoc
155      */
156     public char getChar()
157     {
158         return cursor.getChar();
159     }
160 
161 }