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              public void enteredFirstComponent(int componentIndex, ByteArray component) {
60                  // Do nothing.
61              }
62  
63              public void enteredLastComponent(int componentIndex, ByteArray component) {
64                  assert false;
65              }
66  
67              public void enteredNextComponent(int componentIndex, ByteArray component) {
68                  cursorPassedFirstComponent();
69              }
70  
71              public void enteredPreviousComponent(int componentIndex, ByteArray component) {
72                  assert false;
73              }
74  
75          });
76      }
77  
78      /**
79       * @inheritDoc
80       */
81      public final int getRemaining() {
82          return cursor.getRemaining();
83      }
84  
85      /**
86       * @inheritDoc
87       */
88      public final boolean hasRemaining() {
89          return cursor.hasRemaining();
90      }
91  
92      /**
93       * @inheritDoc
94       */
95      public ByteOrder order() {
96          return cba.order();
97      }
98  
99      /**
100      * Make a <code>ByteArray</code> available for access at the end of this object.
101      */
102     public final void append(ByteArray ba) {
103         cba.addLast(ba);
104     }
105 
106     /**
107      * Free all resources associated with this object.
108      */
109     public final void free() {
110         cba.free();
111     }
112 
113     /**
114      * Get the index that will be used for the next access.
115      */
116     public final int getIndex() {
117         return cursor.getIndex();
118     }
119 
120     /**
121      * Get the index after the last byte that can be accessed.
122      */
123     public final int last() {
124         return cba.last();
125     }
126 
127     /**
128      * Called whenever the cursor has passed from the <code>cba</code>'s
129      * first component. As the first component is no longer used, this provides
130      * a good opportunity for subclasses to perform some action on it (such as
131      * freeing it).
132      */
133     protected abstract void cursorPassedFirstComponent();
134 
135 }