1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicHeaderIterator.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,174 @@ 1.4 +/* 1.5 + * ==================================================================== 1.6 + * Licensed to the Apache Software Foundation (ASF) under one 1.7 + * or more contributor license agreements. See the NOTICE file 1.8 + * distributed with this work for additional information 1.9 + * regarding copyright ownership. The ASF licenses this file 1.10 + * to you under the Apache License, Version 2.0 (the 1.11 + * "License"); you may not use this file except in compliance 1.12 + * with the License. You may obtain a copy of the License at 1.13 + * 1.14 + * http://www.apache.org/licenses/LICENSE-2.0 1.15 + * 1.16 + * Unless required by applicable law or agreed to in writing, 1.17 + * software distributed under the License is distributed on an 1.18 + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 1.19 + * KIND, either express or implied. See the License for the 1.20 + * specific language governing permissions and limitations 1.21 + * under the License. 1.22 + * ==================================================================== 1.23 + * 1.24 + * This software consists of voluntary contributions made by many 1.25 + * individuals on behalf of the Apache Software Foundation. For more 1.26 + * information on the Apache Software Foundation, please see 1.27 + * <http://www.apache.org/>. 1.28 + * 1.29 + */ 1.30 + 1.31 +package ch.boye.httpclientandroidlib.message; 1.32 + 1.33 +import java.util.NoSuchElementException; 1.34 + 1.35 +import ch.boye.httpclientandroidlib.Header; 1.36 +import ch.boye.httpclientandroidlib.HeaderIterator; 1.37 + 1.38 +/** 1.39 + * Basic implementation of a {@link HeaderIterator}. 1.40 + * 1.41 + * @since 4.0 1.42 + */ 1.43 +public class BasicHeaderIterator implements HeaderIterator { 1.44 + 1.45 + /** 1.46 + * An array of headers to iterate over. 1.47 + * Not all elements of this array are necessarily part of the iteration. 1.48 + * This array will never be modified by the iterator. 1.49 + * Derived implementations are expected to adhere to this restriction. 1.50 + */ 1.51 + protected final Header[] allHeaders; 1.52 + 1.53 + 1.54 + /** 1.55 + * The position of the next header in {@link #allHeaders allHeaders}. 1.56 + * Negative if the iteration is over. 1.57 + */ 1.58 + protected int currentIndex; 1.59 + 1.60 + 1.61 + /** 1.62 + * The header name to filter by. 1.63 + * <code>null</code> to iterate over all headers in the array. 1.64 + */ 1.65 + protected String headerName; 1.66 + 1.67 + 1.68 + 1.69 + /** 1.70 + * Creates a new header iterator. 1.71 + * 1.72 + * @param headers an array of headers over which to iterate 1.73 + * @param name the name of the headers over which to iterate, or 1.74 + * <code>null</code> for any 1.75 + */ 1.76 + public BasicHeaderIterator(Header[] headers, String name) { 1.77 + if (headers == null) { 1.78 + throw new IllegalArgumentException 1.79 + ("Header array must not be null."); 1.80 + } 1.81 + 1.82 + this.allHeaders = headers; 1.83 + this.headerName = name; 1.84 + this.currentIndex = findNext(-1); 1.85 + } 1.86 + 1.87 + 1.88 + /** 1.89 + * Determines the index of the next header. 1.90 + * 1.91 + * @param from one less than the index to consider first, 1.92 + * -1 to search for the first header 1.93 + * 1.94 + * @return the index of the next header that matches the filter name, 1.95 + * or negative if there are no more headers 1.96 + */ 1.97 + protected int findNext(int from) { 1.98 + if (from < -1) 1.99 + return -1; 1.100 + 1.101 + final int to = this.allHeaders.length-1; 1.102 + boolean found = false; 1.103 + while (!found && (from < to)) { 1.104 + from++; 1.105 + found = filterHeader(from); 1.106 + } 1.107 + return found ? from : -1; 1.108 + } 1.109 + 1.110 + 1.111 + /** 1.112 + * Checks whether a header is part of the iteration. 1.113 + * 1.114 + * @param index the index of the header to check 1.115 + * 1.116 + * @return <code>true</code> if the header should be part of the 1.117 + * iteration, <code>false</code> to skip 1.118 + */ 1.119 + protected boolean filterHeader(int index) { 1.120 + return (this.headerName == null) || 1.121 + this.headerName.equalsIgnoreCase(this.allHeaders[index].getName()); 1.122 + } 1.123 + 1.124 + 1.125 + // non-javadoc, see interface HeaderIterator 1.126 + public boolean hasNext() { 1.127 + return (this.currentIndex >= 0); 1.128 + } 1.129 + 1.130 + 1.131 + /** 1.132 + * Obtains the next header from this iteration. 1.133 + * 1.134 + * @return the next header in this iteration 1.135 + * 1.136 + * @throws NoSuchElementException if there are no more headers 1.137 + */ 1.138 + public Header nextHeader() 1.139 + throws NoSuchElementException { 1.140 + 1.141 + final int current = this.currentIndex; 1.142 + if (current < 0) { 1.143 + throw new NoSuchElementException("Iteration already finished."); 1.144 + } 1.145 + 1.146 + this.currentIndex = findNext(current); 1.147 + 1.148 + return this.allHeaders[current]; 1.149 + } 1.150 + 1.151 + 1.152 + /** 1.153 + * Returns the next header. 1.154 + * Same as {@link #nextHeader nextHeader}, but not type-safe. 1.155 + * 1.156 + * @return the next header in this iteration 1.157 + * 1.158 + * @throws NoSuchElementException if there are no more headers 1.159 + */ 1.160 + public final Object next() 1.161 + throws NoSuchElementException { 1.162 + return nextHeader(); 1.163 + } 1.164 + 1.165 + 1.166 + /** 1.167 + * Removing headers is not supported. 1.168 + * 1.169 + * @throws UnsupportedOperationException always 1.170 + */ 1.171 + public void remove() 1.172 + throws UnsupportedOperationException { 1.173 + 1.174 + throw new UnsupportedOperationException 1.175 + ("Removing headers is not supported."); 1.176 + } 1.177 +}