1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.archive.util; 19 20 /** 21 * Helpers for the archive module. 22 */ 23 public class Util { 24 25 /** 26 * Returns whether the given source string ends with the suffix, ignoring 27 * case and assuming that the strings are ascii encoded. 28 * 29 * @param source 30 * the string to match. 31 * @param suffix 32 * the suffix to test. 33 * @return {@code true} if the source does end with the given suffix, or 34 * {@code false} if not. 35 */ 36 public static boolean asciiEndsWithIgnoreCase(String source, String suffix) { 37 int length = suffix.length(); 38 if (length > source.length()) { 39 return false; 40 } 41 int offset = source.length() - length; 42 for (int i = 0; i < length; i++) { 43 char c1 = source.charAt(i + offset); 44 char c2 = suffix.charAt(i); 45 if (c1 != c2 && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) { 46 return false; 47 } 48 } 49 return true; 50 } 51 52 /** 53 * Compares the given byte arrays and returns whether they are equal, 54 * ignoring case differences and assuming they are ascii-encoded strings. 55 * 56 * @param buf1 57 * first byte array to compare. 58 * @param buf2 59 * second byte array to compare. 60 * @return the result of the comparison. 61 */ 62 public static boolean asciiEqualsIgnoreCase(byte[] buf1, byte[] buf2) { 63 if (buf1 == null || buf2 == null) { 64 return false; 65 } 66 if (buf1 == buf2) { 67 return true; 68 } 69 if (buf1.length != buf2.length) { 70 return false; 71 } 72 73 for (int i = 0; i < buf1.length; i++) { 74 byte b1 = buf1[i]; 75 byte b2 = buf2[i]; 76 if (b1 != b2 && toASCIIUpperCase(b1) != toASCIIUpperCase(b2)) { 77 return false; 78 } 79 } 80 return true; 81 } 82 83 /** 84 * Compares the given strings and returns whether they are equal, ignoring 85 * case differences and assuming they are ascii-encoded strings. 86 * 87 * @param s1 88 * first string to compare. 89 * @param s2 90 * second string to compare. 91 * @return the result of the comparison. 92 */ 93 public static boolean asciiEqualsIgnoreCase(String s1, String s2) { 94 if (s1 == null || s2 == null) { 95 return false; 96 } 97 if (s1 == s2) { 98 return true; 99 } 100 101 int length = s1.length(); 102 if (length != s2.length()) { 103 return false; 104 } 105 106 for (int i = 0; i < length; i++) { 107 char c1 = s1.charAt(i); 108 char c2 = s2.charAt(i); 109 if (c1 != c2 && toASCIIUpperCase(c1) != toASCIIUpperCase(c2)) { 110 return false; 111 } 112 } 113 return true; 114 } 115 116 private static final byte toASCIIUpperCase(byte b) { 117 if ('a' <= b && b <= 'z') { 118 return (byte) (b - ('a' - 'A')); 119 } 120 return b; 121 } 122 123 private static final char toASCIIUpperCase(char c) { 124 if ('a' <= c && c <= 'z') { 125 return (char) (c - ('a' - 'A')); 126 } 127 return c; 128 } 129 } 130