• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * $HeadURL: http://svn.apache.org/repos/asf/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/BasicHeaderElementIterator.java $
3  * $Revision: 592088 $
4  * $Date: 2007-11-05 09:03:39 -0800 (Mon, 05 Nov 2007) $
5  *
6  * ====================================================================
7  * Licensed to the Apache Software Foundation (ASF) under one
8  * or more contributor license agreements.  See the NOTICE file
9  * distributed with this work for additional information
10  * regarding copyright ownership.  The ASF licenses this file
11  * to you under the Apache License, Version 2.0 (the
12  * "License"); you may not use this file except in compliance
13  * with the License.  You may obtain a copy of the License at
14  *
15  *   http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing,
18  * software distributed under the License is distributed on an
19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20  * KIND, either express or implied.  See the License for the
21  * specific language governing permissions and limitations
22  * under the License.
23  * ====================================================================
24  *
25  * This software consists of voluntary contributions made by many
26  * individuals on behalf of the Apache Software Foundation.  For more
27  * information on the Apache Software Foundation, please see
28  * <http://www.apache.org/>.
29  *
30  */
31 
32 package org.apache.http.message;
33 
34 import java.util.NoSuchElementException;
35 
36 import org.apache.http.FormattedHeader;
37 import org.apache.http.Header;
38 import org.apache.http.HeaderElement;
39 import org.apache.http.HeaderElementIterator;
40 import org.apache.http.HeaderIterator;
41 import org.apache.http.util.CharArrayBuffer;
42 
43 /**
44  * Basic implementation of a {@link HeaderElementIterator}.
45  *
46  * @version $Revision: 592088 $
47  *
48  * @author Andrea Selva <selva.andre at gmail.com>
49  * @author Oleg Kalnichevski <oleg at ural.ru>
50  *
51  * @deprecated Please use {@link java.net.URL#openConnection} instead.
52  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
53  *     for further details.
54  */
55 @Deprecated
56 public class BasicHeaderElementIterator implements HeaderElementIterator {
57 
58     private final HeaderIterator headerIt;
59     private final HeaderValueParser parser;
60 
61     private HeaderElement currentElement = null;
62     private CharArrayBuffer buffer = null;
63     private ParserCursor cursor = null;
64 
65     /**
66      * Creates a new instance of BasicHeaderElementIterator
67      */
BasicHeaderElementIterator( final HeaderIterator headerIterator, final HeaderValueParser parser)68     public BasicHeaderElementIterator(
69             final HeaderIterator headerIterator,
70             final HeaderValueParser parser) {
71         if (headerIterator == null) {
72             throw new IllegalArgumentException("Header iterator may not be null");
73         }
74         if (parser == null) {
75             throw new IllegalArgumentException("Parser may not be null");
76         }
77         this.headerIt = headerIterator;
78         this.parser = parser;
79     }
80 
81 
BasicHeaderElementIterator(final HeaderIterator headerIterator)82     public BasicHeaderElementIterator(final HeaderIterator headerIterator) {
83         this(headerIterator, BasicHeaderValueParser.DEFAULT);
84     }
85 
86 
bufferHeaderValue()87     private void bufferHeaderValue() {
88         this.cursor = null;
89         this.buffer = null;
90         while (this.headerIt.hasNext()) {
91             Header h = this.headerIt.nextHeader();
92             if (h instanceof FormattedHeader) {
93                 this.buffer = ((FormattedHeader) h).getBuffer();
94                 this.cursor = new ParserCursor(0, this.buffer.length());
95                 this.cursor.updatePos(((FormattedHeader) h).getValuePos());
96                 break;
97             } else {
98                 String value = h.getValue();
99                 if (value != null) {
100                     this.buffer = new CharArrayBuffer(value.length());
101                     this.buffer.append(value);
102                     this.cursor = new ParserCursor(0, this.buffer.length());
103                     break;
104                 }
105             }
106         }
107     }
108 
parseNextElement()109     private void parseNextElement() {
110         // loop while there are headers left to parse
111         while (this.headerIt.hasNext() || this.cursor != null) {
112             if (this.cursor == null || this.cursor.atEnd()) {
113                 // get next header value
114                 bufferHeaderValue();
115             }
116             // Anything buffered?
117             if (this.cursor != null) {
118                 // loop while there is data in the buffer
119                 while (!this.cursor.atEnd()) {
120                     HeaderElement e = this.parser.parseHeaderElement(this.buffer, this.cursor);
121                     if (!(e.getName().length() == 0 && e.getValue() == null)) {
122                         // Found something
123                         this.currentElement = e;
124                         return;
125                     }
126                 }
127                 // if at the end of the buffer
128                 if (this.cursor.atEnd()) {
129                     // discard it
130                     this.cursor = null;
131                     this.buffer = null;
132                 }
133             }
134         }
135     }
136 
hasNext()137     public boolean hasNext() {
138         if (this.currentElement == null) {
139             parseNextElement();
140         }
141         return this.currentElement != null;
142     }
143 
nextElement()144     public HeaderElement nextElement() throws NoSuchElementException {
145         if (this.currentElement == null) {
146             parseNextElement();
147         }
148 
149         if (this.currentElement == null) {
150             throw new NoSuchElementException("No more header elements available");
151         }
152 
153         HeaderElement element = this.currentElement;
154         this.currentElement = null;
155         return element;
156     }
157 
next()158     public final Object next() throws NoSuchElementException {
159         return nextElement();
160     }
161 
remove()162     public void remove() throws UnsupportedOperationException {
163         throw new UnsupportedOperationException("Remove not supported");
164     }
165 
166 }