Home | History | Annotate | Download | only in http
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.net.http;
     18 
     19 import org.apache.http.util.CharArrayBuffer;
     20 import org.apache.http.protocol.HTTP;
     21 
     22 /**
     23  * Utility methods for working on CharArrayBuffers.
     24  *
     25  * {@hide}
     26  */
     27 class CharArrayBuffers {
     28 
     29     static final char uppercaseAddon = 'a' - 'A';
     30 
     31     /**
     32      * Returns true if the buffer contains the given string. Ignores leading
     33      * whitespace and case.
     34      *
     35      * @param buffer to search
     36      * @param beginIndex index at which we should start
     37      * @param str to search for
     38      */
     39     static boolean containsIgnoreCaseTrimmed(CharArrayBuffer buffer,
     40             int beginIndex, final String str) {
     41         int len = buffer.length();
     42         char[] chars = buffer.buffer();
     43         while (beginIndex < len && HTTP.isWhitespace(chars[beginIndex])) {
     44             beginIndex++;
     45         }
     46         int size = str.length();
     47         boolean ok = len >= beginIndex + size;
     48         for (int j=0; ok && (j<size); j++) {
     49             char a = chars[beginIndex+j];
     50             char b = str.charAt(j);
     51             if (a != b) {
     52                 a = toLower(a);
     53                 b = toLower(b);
     54                 ok = a == b;
     55             }
     56         }
     57         return ok;
     58     }
     59 
     60     /**
     61      * Returns index of first occurence ch. Lower cases characters leading up
     62      * to first occurrence of ch.
     63      */
     64     static int setLowercaseIndexOf(CharArrayBuffer buffer, final int ch) {
     65 
     66         int beginIndex = 0;
     67         int endIndex = buffer.length();
     68         char[] chars = buffer.buffer();
     69 
     70         for (int i = beginIndex; i < endIndex; i++) {
     71             char current = chars[i];
     72             if (current == ch) {
     73                 return i;
     74             } else if (current >= 'A' && current <= 'Z'){
     75                 // make lower case
     76                 current += uppercaseAddon;
     77                 chars[i] = current;
     78             }
     79         }
     80         return -1;
     81     }
     82 
     83     private static char toLower(char c) {
     84         if (c >= 'A' && c <= 'Z'){
     85             c += uppercaseAddon;
     86         }
     87         return c;
     88     }
     89 }
     90