Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/util/ByteArrayLexOrder.java
38830 views
/*1* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package sun.security.util;2728import java.util.Comparator;2930/**31* Compare two byte arrays in lexicographical order.32*33* @author D. N. Hoover34*/35public class ByteArrayLexOrder implements Comparator<byte[]> {3637/**38* Perform lexicographical comparison of two byte arrays,39* regarding each byte as unsigned. That is, compare array entries40* in order until they differ--the array with the smaller entry41* is "smaller". If array entries are42* equal till one array ends, then the longer array is "bigger".43*44* @param bytes1 first byte array to compare.45* @param bytes2 second byte array to compare.46* @return negative number if bytes1 < bytes2, 0 if bytes1 == bytes2,47* positive number if bytes1 > bytes2.48*49* @exception <code>ClassCastException</code>50* if either argument is not a byte array.51*/52public final int compare( byte[] bytes1, byte[] bytes2) {53int diff;54for (int i = 0; i < bytes1.length && i < bytes2.length; i++) {55diff = (bytes1[i] & 0xFF) - (bytes2[i] & 0xFF);56if (diff != 0) {57return diff;58}59}60// if array entries are equal till the first ends, then the61// longer is "bigger"62return bytes1.length - bytes2.length;63}646566}676869