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  
25  import org.apache.mina.util.byteaccess.ByteArray.Cursor;
26  import org.apache.mina.util.byteaccess.CompositeByteArray.CursorListener;
27  
28  
29  /**
30   * Provides common functionality between the
31   * <code>CompositeByteArrayRelativeReader</code> and
32   * <code>CompositeByteArrayRelativeWriter</code>.
33   * 
34   * @author The Apache MINA Project (dev@mina.apache.org)
35   * @version $Rev$, $Date$
36   */
37  abstract class CompositeByteArrayRelativeBase
38  {
39  
40      /**
41       * The underlying <code>CompositeByteArray</code>.
42       */
43      protected final CompositeByteArray cba;
44  
45      /**
46       * A cursor of the underlying <code>CompositeByteArray</code>. This
47       * cursor is never moved directly; its position only changes through calls
48       * to relative read or write methods.
49       */
50      protected final Cursor cursor;
51  
52      /**
53       * 
54       * Creates a new instance of CompositeByteArrayRelativeBase.
55       *
56       * @param cba
57       *  The {@link CompositeByteArray} that will be the base for this class
58       */
59      public CompositeByteArrayRelativeBase( CompositeByteArray cba )
60      {
61          this.cba = cba;
62          cursor = cba.cursor( cba.first(), new CursorListener()
63          {
64  
65              public void enteredFirstComponent( int componentIndex, ByteArray component )
66              {
67                  // Do nothing.
68              }
69  
70  
71              public void enteredLastComponent( int componentIndex, ByteArray component )
72              {
73                  assert false;
74              }
75  
76  
77              public void enteredNextComponent( int componentIndex, ByteArray component )
78              {
79                  cursorPassedFirstComponent();
80              }
81  
82  
83              public void enteredPreviousComponent( int componentIndex, ByteArray component )
84              {
85                  assert false;
86              }
87  
88          } );
89      }
90  
91  
92      /**
93       * @inheritDoc
94       */
95      public final int getRemaining()
96      {
97          return cursor.getRemaining();
98      }
99  
100 
101     /**
102      * @inheritDoc
103      */
104     public final boolean hasRemaining()
105     {
106         return cursor.hasRemaining();
107     }
108 
109 
110     /**
111      * @inheritDoc
112      */
113     public ByteOrder order()
114     {
115         return cba.order();
116     }
117 
118 
119     /**
120      * Make a <code>ByteArray</code> available for access at the end of this object.
121      */
122     public final void append( ByteArray ba )
123     {
124         cba.addLast( ba );
125     }
126 
127 
128     /**
129      * Free all resources associated with this object.
130      */
131     public final void free()
132     {
133         cba.free();
134     }
135 
136 
137     /**
138      * Get the index that will be used for the next access.
139      */
140     public final int getIndex()
141     {
142         return cursor.getIndex();
143     }
144 
145 
146     /**
147      * Get the index after the last byte that can be accessed.
148      */
149     public final int last()
150     {
151         return cba.last();
152     }
153 
154 
155     /**
156      * Called whenever the cursor has passed from the <code>cba</code>'s
157      * first component. As the first component is no longer used, this provides
158      * a good opportunity for subclasses to perform some action on it (such as
159      * freeing it).
160      */
161     protected abstract void cursorPassedFirstComponent();
162 
163 }