@NotThreadSafe public class BasicTokenIterator extends Object implements TokenIterator
TokenIterator
.
This implementation parses #token
sequences as
defined by RFC 2616, section 2.
It extends that definition somewhat beyond US-ASCII.Modifier and Type | Field and Description |
---|---|
protected String |
currentHeader
The value of the current header.
|
protected String |
currentToken
The token to be returned by the next call to
nextToken() . |
protected HeaderIterator |
headerIt
The iterator from which to obtain the next header.
|
static String |
HTTP_SEPARATORS
The HTTP separator characters.
|
protected int |
searchPos
The position after
currentToken in currentHeader . |
Constructor and Description |
---|
BasicTokenIterator(HeaderIterator headerIterator)
Creates a new instance of
BasicTokenIterator . |
Modifier and Type | Method and Description |
---|---|
protected String |
createToken(String value,
int start,
int end)
Creates a new token to be returned.
|
protected int |
findNext(int pos)
Determines the next token.
|
protected int |
findTokenEnd(int from)
Determines the ending position of the current token.
|
protected int |
findTokenSeparator(int pos)
Determines the position of the next token separator.
|
protected int |
findTokenStart(int pos)
Determines the starting position of the next token.
|
boolean |
hasNext()
Indicates whether there is another token in this iteration.
|
protected boolean |
isHttpSeparator(char ch)
Checks whether a character is an HTTP separator.
|
protected boolean |
isTokenChar(char ch)
Checks whether a character is a valid token character.
|
protected boolean |
isTokenSeparator(char ch)
Checks whether a character is a token separator.
|
protected boolean |
isWhitespace(char ch)
Checks whether a character is a whitespace character.
|
Object |
next()
Returns the next token.
|
String |
nextToken()
Obtains the next token from this iteration.
|
void |
remove()
Removing tokens is not supported.
|
public static final String HTTP_SEPARATORS
protected final HeaderIterator headerIt
protected String currentHeader
currentToken
.
Undefined if the iteration is over.protected String currentToken
nextToken()
.
null
if the iteration is over.protected int searchPos
currentToken
in currentHeader
.
Undefined if the iteration is over.public BasicTokenIterator(HeaderIterator headerIterator)
BasicTokenIterator
.headerIterator
- the iterator for the headers to tokenizepublic boolean hasNext()
TokenIterator
hasNext
in interface Iterator<Object>
hasNext
in interface TokenIterator
true
if there is another token,
false
otherwisepublic String nextToken() throws NoSuchElementException, ParseException
nextToken
in interface TokenIterator
NoSuchElementException
- if the iteration is already overParseException
- if an invalid header value is encounteredpublic final Object next() throws NoSuchElementException, ParseException
nextToken()
, but with generic return type.next
in interface Iterator<Object>
NoSuchElementException
- if there are no more tokensParseException
- if an invalid header value is encounteredpublic final void remove() throws UnsupportedOperationException
remove
in interface Iterator<Object>
UnsupportedOperationException
- alwaysprotected int findNext(int pos) throws ParseException
currentToken
.
The return value indicates the position after the token
in currentHeader
. If necessary, the next header
will be obtained from headerIt
.
If not found, currentToken
is set to null
.pos
- the position in the current header at which to
start the search, -1 to search in the first headerParseException
- if an invalid header value is encounteredprotected String createToken(String value, int start, int end)
findNext
after the token is identified.
The default implementation simply calls
String.substring
.
If header values are significantly longer than tokens, and some tokens are permanently referenced by the application, there can be problems with garbage collection. A substring will hold a reference to the full characters of the original string and therefore occupies more memory than might be expected. To avoid this, override this method and create a new string instead of a substring.
value
- the full header value from which to create a tokenstart
- the index of the first token characterend
- the index after the last token characterprotected int findTokenStart(int pos)
pos
- the position in the current header at which to
start the searchprotected int findTokenSeparator(int pos)
pos
- the position in the current header at which to
start the searchParseException
- if a new token is found before a token separator.
RFC 2616, section 2.1 explicitly requires a comma between
tokens for #
.protected int findTokenEnd(int from)
from
- the position of the first character of the tokenfrom
does not
point to a token character in the current header value.protected boolean isTokenSeparator(char ch)
#token
sequences. The end of a header value will
also separate tokens, but that is not a character check.ch
- the character to checktrue
if the character is a token separator,
false
otherwiseprotected boolean isWhitespace(char ch)
ch
- the character to checktrue
if the character is whitespace,
false
otherwiseprotected boolean isTokenChar(char ch)
ch
- the character to checktrue
if the character is a valid token start,
false
otherwiseprotected boolean isHttpSeparator(char ch)
ch
- the character to checktrue
if the character is an HTTP separatorCopyright © 2005–2015 The Apache Software Foundation. All rights reserved.