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