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