Path: blob/master/src/java.base/share/classes/sun/security/util/ByteArrayLexOrder.java
41159 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 {@code bytes1 < bytes2},47* 0 if {@code bytes1 == bytes2},48* positive number if {@code bytes1 > bytes2}.49*50* @exception <code>ClassCastException</code>51* if either argument is not a byte array.52*/53public final int compare( byte[] bytes1, byte[] bytes2) {54int diff;55for (int i = 0; i < bytes1.length && i < bytes2.length; i++) {56diff = (bytes1[i] & 0xFF) - (bytes2[i] & 0xFF);57if (diff != 0) {58return diff;59}60}61// if array entries are equal till the first ends, then the62// longer is "bigger"63return bytes1.length - bytes2.length;64}656667}686970