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   * 
24   * Abstract class that implements {@link ByteArray}.  This class will only be 
25   * used internally and should not be used by end users.
26   *
27   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
28   */
29  abstract class AbstractByteArray implements ByteArray {
30      /**
31       * {@inheritDoc}
32       */
33      @Override
34      public final int length() {
35          return last() - first();
36      }
37      
38      
39      /**
40       * {@inheritDoc}
41       */
42      @Override
43      public abstract int hashCode();
44  
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public final boolean equals(Object other) {
50          // Optimization: compare pointers.
51          if (other == this) {
52              return true;
53          }
54          
55          // Compare types.
56          if (!(other instanceof ByteArray)) {
57              return false;
58          }
59          
60          ByteArray otherByteArray = (ByteArray) other;
61          
62          // Compare properties.
63          if (first() != otherByteArray.first() || last() != otherByteArray.last()
64                  || !order().equals(otherByteArray.order())) {
65              return false;
66          }
67          
68          // Compare bytes.
69          Cursor cursor = cursor();
70          Cursor otherCursor = otherByteArray.cursor();
71          
72          for (int remaining = cursor.getRemaining(); remaining > 0;) {
73              // Optimization: prefer int comparisons over byte comparisons
74              if (remaining >= 4) {
75                  int i = cursor.getInt();
76                  int otherI = otherCursor.getInt();
77                  
78                  if (i != otherI) {
79                      return false;
80                  }
81              } else {
82                  byte b = cursor.get();
83                  byte otherB = otherCursor.get();
84                  
85                  if (b != otherB) {
86                      return false;
87                  }
88              }
89          }
90          
91          return true;
92      }
93  }