KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > schlichtherle > util > Arrays


1 /*
2  * Arrays.java
3  *
4  * Created on 13. Oktober 2005, 02:46
5  */

6 /*
7  * Copyright 2005 Schlichtherle IT Services
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package de.schlichtherle.util;
23
24 /**
25  * @author Christian Schlichtherle
26  */

27 public class Arrays {
28     
29     /**
30      * Compares <tt>max</tt> bytes at the specified offsets of the given
31      * arrays.
32      * If the remaining bytes at the given offset of any array is smaller than
33      * <tt>max</tt> bytes, it must match the number of remaining bytes at the
34      * given offset in the other array.
35      */

36     public static boolean equals(
37             final byte[] b1,
38             final int b1off,
39             final byte[] b2,
40             final int b2off,
41             int max) {
42         if (b1 == null)
43             throw new NullPointerException JavaDoc("b1");
44         if (b2 == null)
45             throw new NullPointerException JavaDoc("b2");
46         if (0 > b1off || b1off > b1.length)
47             throw new IndexOutOfBoundsException JavaDoc("b1off = " + b1off + ": Not in [0, " + b1.length + "[!");
48         if (0 > b2off || b2off > b2.length)
49             throw new IndexOutOfBoundsException JavaDoc("b2off = " + b2off + ": Not in [0, " + b2.length + "[!");
50         if (max < 1)
51             throw new IllegalArgumentException JavaDoc("len = " + max + ": Too small!");
52
53         final int b1rem = b1.length - b1off;
54         final int b2rem = b2.length - b2off;
55         if (max > b1rem) {
56             max = b1rem;
57             if (max != b2rem)
58                 return false;
59         } else if (max > b2rem) {
60             max = b2rem;
61             if (max != b1rem)
62                 return false;
63         }
64
65         while (--max >= 0)
66             if (b1[b1off + max] != b2[b2off + max])
67                 return false;
68
69         return true;
70     }
71     
72     protected Arrays() {
73     }
74 }
Popular Tags