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