mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicHeaderElementIterator.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/ch/boye/httpclientandroidlib/message/BasicHeaderElementIterator.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,154 @@
     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.FormattedHeader;
    1.36 +import ch.boye.httpclientandroidlib.Header;
    1.37 +import ch.boye.httpclientandroidlib.HeaderElement;
    1.38 +import ch.boye.httpclientandroidlib.HeaderElementIterator;
    1.39 +import ch.boye.httpclientandroidlib.HeaderIterator;
    1.40 +import ch.boye.httpclientandroidlib.util.CharArrayBuffer;
    1.41 +
    1.42 +/**
    1.43 + * Basic implementation of a {@link HeaderElementIterator}.
    1.44 + *
    1.45 + * @since 4.0
    1.46 + */
    1.47 +public class BasicHeaderElementIterator implements HeaderElementIterator {
    1.48 +
    1.49 +    private final HeaderIterator headerIt;
    1.50 +    private final HeaderValueParser parser;
    1.51 +
    1.52 +    private HeaderElement currentElement = null;
    1.53 +    private CharArrayBuffer buffer = null;
    1.54 +    private ParserCursor cursor = null;
    1.55 +
    1.56 +    /**
    1.57 +     * Creates a new instance of BasicHeaderElementIterator
    1.58 +     */
    1.59 +    public BasicHeaderElementIterator(
    1.60 +            final HeaderIterator headerIterator,
    1.61 +            final HeaderValueParser parser) {
    1.62 +        if (headerIterator == null) {
    1.63 +            throw new IllegalArgumentException("Header iterator may not be null");
    1.64 +        }
    1.65 +        if (parser == null) {
    1.66 +            throw new IllegalArgumentException("Parser may not be null");
    1.67 +        }
    1.68 +        this.headerIt = headerIterator;
    1.69 +        this.parser = parser;
    1.70 +    }
    1.71 +
    1.72 +
    1.73 +    public BasicHeaderElementIterator(final HeaderIterator headerIterator) {
    1.74 +        this(headerIterator, BasicHeaderValueParser.DEFAULT);
    1.75 +    }
    1.76 +
    1.77 +
    1.78 +    private void bufferHeaderValue() {
    1.79 +        this.cursor = null;
    1.80 +        this.buffer = null;
    1.81 +        while (this.headerIt.hasNext()) {
    1.82 +            Header h = this.headerIt.nextHeader();
    1.83 +            if (h instanceof FormattedHeader) {
    1.84 +                this.buffer = ((FormattedHeader) h).getBuffer();
    1.85 +                this.cursor = new ParserCursor(0, this.buffer.length());
    1.86 +                this.cursor.updatePos(((FormattedHeader) h).getValuePos());
    1.87 +                break;
    1.88 +            } else {
    1.89 +                String value = h.getValue();
    1.90 +                if (value != null) {
    1.91 +                    this.buffer = new CharArrayBuffer(value.length());
    1.92 +                    this.buffer.append(value);
    1.93 +                    this.cursor = new ParserCursor(0, this.buffer.length());
    1.94 +                    break;
    1.95 +                }
    1.96 +            }
    1.97 +        }
    1.98 +    }
    1.99 +
   1.100 +    private void parseNextElement() {
   1.101 +        // loop while there are headers left to parse
   1.102 +        while (this.headerIt.hasNext() || this.cursor != null) {
   1.103 +            if (this.cursor == null || this.cursor.atEnd()) {
   1.104 +                // get next header value
   1.105 +                bufferHeaderValue();
   1.106 +            }
   1.107 +            // Anything buffered?
   1.108 +            if (this.cursor != null) {
   1.109 +                // loop while there is data in the buffer
   1.110 +                while (!this.cursor.atEnd()) {
   1.111 +                    HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor);
   1.112 +                    if (!(e.getName().length() == 0 && e.getValue() == null)) {
   1.113 +                        // Found something
   1.114 +                        this.currentElement = e;
   1.115 +                        return;
   1.116 +                    }
   1.117 +                }
   1.118 +                // if at the end of the buffer
   1.119 +                if (this.cursor.atEnd()) {
   1.120 +                    // discard it
   1.121 +                    this.cursor = null;
   1.122 +                    this.buffer = null;
   1.123 +                }
   1.124 +            }
   1.125 +        }
   1.126 +    }
   1.127 +
   1.128 +    public boolean hasNext() {
   1.129 +        if (this.currentElement == null) {
   1.130 +            parseNextElement();
   1.131 +        }
   1.132 +        return this.currentElement != null;
   1.133 +    }
   1.134 +
   1.135 +    public HeaderElement nextElement() throws NoSuchElementException {
   1.136 +        if (this.currentElement == null) {
   1.137 +            parseNextElement();
   1.138 +        }
   1.139 +
   1.140 +        if (this.currentElement == null) {
   1.141 +            throw new NoSuchElementException("No more header elements available");
   1.142 +        }
   1.143 +
   1.144 +        HeaderElement element = this.currentElement;
   1.145 +        this.currentElement = null;
   1.146 +        return element;
   1.147 +    }
   1.148 +
   1.149 +    public final Object next() throws NoSuchElementException {
   1.150 +        return nextElement();
   1.151 +    }
   1.152 +
   1.153 +    public void remove() throws UnsupportedOperationException {
   1.154 +        throw new UnsupportedOperationException("Remove not supported");
   1.155 +    }
   1.156 +
   1.157 +}

mercurial