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