Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | * ==================================================================== |
michael@0 | 3 | * Licensed to the Apache Software Foundation (ASF) under one |
michael@0 | 4 | * or more contributor license agreements. See the NOTICE file |
michael@0 | 5 | * distributed with this work for additional information |
michael@0 | 6 | * regarding copyright ownership. The ASF licenses this file |
michael@0 | 7 | * to you under the Apache License, Version 2.0 (the |
michael@0 | 8 | * "License"); you may not use this file except in compliance |
michael@0 | 9 | * with the License. You may obtain a copy of the License at |
michael@0 | 10 | * |
michael@0 | 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
michael@0 | 12 | * |
michael@0 | 13 | * Unless required by applicable law or agreed to in writing, |
michael@0 | 14 | * software distributed under the License is distributed on an |
michael@0 | 15 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
michael@0 | 16 | * KIND, either express or implied. See the License for the |
michael@0 | 17 | * specific language governing permissions and limitations |
michael@0 | 18 | * under the License. |
michael@0 | 19 | * ==================================================================== |
michael@0 | 20 | * |
michael@0 | 21 | * This software consists of voluntary contributions made by many |
michael@0 | 22 | * individuals on behalf of the Apache Software Foundation. For more |
michael@0 | 23 | * information on the Apache Software Foundation, please see |
michael@0 | 24 | * <http://www.apache.org/>. |
michael@0 | 25 | * |
michael@0 | 26 | */ |
michael@0 | 27 | |
michael@0 | 28 | package ch.boye.httpclientandroidlib.message; |
michael@0 | 29 | |
michael@0 | 30 | import java.util.NoSuchElementException; |
michael@0 | 31 | |
michael@0 | 32 | import ch.boye.httpclientandroidlib.FormattedHeader; |
michael@0 | 33 | import ch.boye.httpclientandroidlib.Header; |
michael@0 | 34 | import ch.boye.httpclientandroidlib.HeaderElement; |
michael@0 | 35 | import ch.boye.httpclientandroidlib.HeaderElementIterator; |
michael@0 | 36 | import ch.boye.httpclientandroidlib.HeaderIterator; |
michael@0 | 37 | import ch.boye.httpclientandroidlib.util.CharArrayBuffer; |
michael@0 | 38 | |
michael@0 | 39 | /** |
michael@0 | 40 | * Basic implementation of a {@link HeaderElementIterator}. |
michael@0 | 41 | * |
michael@0 | 42 | * @since 4.0 |
michael@0 | 43 | */ |
michael@0 | 44 | public class BasicHeaderElementIterator implements HeaderElementIterator { |
michael@0 | 45 | |
michael@0 | 46 | private final HeaderIterator headerIt; |
michael@0 | 47 | private final HeaderValueParser parser; |
michael@0 | 48 | |
michael@0 | 49 | private HeaderElement currentElement = null; |
michael@0 | 50 | private CharArrayBuffer buffer = null; |
michael@0 | 51 | private ParserCursor cursor = null; |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Creates a new instance of BasicHeaderElementIterator |
michael@0 | 55 | */ |
michael@0 | 56 | public BasicHeaderElementIterator( |
michael@0 | 57 | final HeaderIterator headerIterator, |
michael@0 | 58 | final HeaderValueParser parser) { |
michael@0 | 59 | if (headerIterator == null) { |
michael@0 | 60 | throw new IllegalArgumentException("Header iterator may not be null"); |
michael@0 | 61 | } |
michael@0 | 62 | if (parser == null) { |
michael@0 | 63 | throw new IllegalArgumentException("Parser may not be null"); |
michael@0 | 64 | } |
michael@0 | 65 | this.headerIt = headerIterator; |
michael@0 | 66 | this.parser = parser; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | |
michael@0 | 70 | public BasicHeaderElementIterator(final HeaderIterator headerIterator) { |
michael@0 | 71 | this(headerIterator, BasicHeaderValueParser.DEFAULT); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | |
michael@0 | 75 | private void bufferHeaderValue() { |
michael@0 | 76 | this.cursor = null; |
michael@0 | 77 | this.buffer = null; |
michael@0 | 78 | while (this.headerIt.hasNext()) { |
michael@0 | 79 | Header h = this.headerIt.nextHeader(); |
michael@0 | 80 | if (h instanceof FormattedHeader) { |
michael@0 | 81 | this.buffer = ((FormattedHeader) h).getBuffer(); |
michael@0 | 82 | this.cursor = new ParserCursor(0, this.buffer.length()); |
michael@0 | 83 | this.cursor.updatePos(((FormattedHeader) h).getValuePos()); |
michael@0 | 84 | break; |
michael@0 | 85 | } else { |
michael@0 | 86 | String value = h.getValue(); |
michael@0 | 87 | if (value != null) { |
michael@0 | 88 | this.buffer = new CharArrayBuffer(value.length()); |
michael@0 | 89 | this.buffer.append(value); |
michael@0 | 90 | this.cursor = new ParserCursor(0, this.buffer.length()); |
michael@0 | 91 | break; |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | private void parseNextElement() { |
michael@0 | 98 | // loop while there are headers left to parse |
michael@0 | 99 | while (this.headerIt.hasNext() || this.cursor != null) { |
michael@0 | 100 | if (this.cursor == null || this.cursor.atEnd()) { |
michael@0 | 101 | // get next header value |
michael@0 | 102 | bufferHeaderValue(); |
michael@0 | 103 | } |
michael@0 | 104 | // Anything buffered? |
michael@0 | 105 | if (this.cursor != null) { |
michael@0 | 106 | // loop while there is data in the buffer |
michael@0 | 107 | while (!this.cursor.atEnd()) { |
michael@0 | 108 | HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor); |
michael@0 | 109 | if (!(e.getName().length() == 0 && e.getValue() == null)) { |
michael@0 | 110 | // Found something |
michael@0 | 111 | this.currentElement = e; |
michael@0 | 112 | return; |
michael@0 | 113 | } |
michael@0 | 114 | } |
michael@0 | 115 | // if at the end of the buffer |
michael@0 | 116 | if (this.cursor.atEnd()) { |
michael@0 | 117 | // discard it |
michael@0 | 118 | this.cursor = null; |
michael@0 | 119 | this.buffer = null; |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | public boolean hasNext() { |
michael@0 | 126 | if (this.currentElement == null) { |
michael@0 | 127 | parseNextElement(); |
michael@0 | 128 | } |
michael@0 | 129 | return this.currentElement != null; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | public HeaderElement nextElement() throws NoSuchElementException { |
michael@0 | 133 | if (this.currentElement == null) { |
michael@0 | 134 | parseNextElement(); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | if (this.currentElement == null) { |
michael@0 | 138 | throw new NoSuchElementException("No more header elements available"); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | HeaderElement element = this.currentElement; |
michael@0 | 142 | this.currentElement = null; |
michael@0 | 143 | return element; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | public final Object next() throws NoSuchElementException { |
michael@0 | 147 | return nextElement(); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | public void remove() throws UnsupportedOperationException { |
michael@0 | 151 | throw new UnsupportedOperationException("Remove not supported"); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | } |