Top Description Fields Constructors Methods
org.apache.http.message

public Class BasicTokenIterator

extends Object
implements TokenIterator
Class Inheritance
All Implemented Interfaces
org.apache.http.TokenIterator, java.util.Iterator
Imports
java.util.NoSuchElementException, org.apache.http.HeaderIterator, .ParseException, .TokenIterator, org.apache.http.util.Args

Basic implementation of a TokenIterator. This implementation parses #token sequences as defined by RFC 2616, section 2. It extends that definition somewhat beyond US-ASCII.
Since
4.0

Field Summary

Modifier and TypeField 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 final HeaderIterator
headerIt

The iterator from which to obtain the next header.

public static final String
HTTP_SEPARATORS

The HTTP separator characters.

protected int
searchPos

The position after currentToken in currentHeader.

Constructor Summary

AccessConstructor and Description
public
BasicTokenIterator(final HeaderIterator
the iterator for the headers to tokenize
headerIterator
)

Creates a new instance of BasicTokenIterator.

Method Summary

Modifier and TypeMethod and Description
protected String

Returns:

a string representing the token identified by the arguments
createToken
(final String
the full header value from which to create a token
value
,
final int
the index of the first token character
start
,
final int
the index after the last token character
end
)

Creates a new token to be returned.

protected int

Returns:

the position after the found token in the current header, or negative if there was no next token
findNext
(final int
the position in the current header at which to start the search, -1 to search in the first header
pos
)

Determines the next token.

protected int

Returns:

the position after the last character of the token. The behavior is undefined if from does not point to a token character in the current header value.
findTokenEnd
(final int
the position of the first character of the token
from
)

Determines the ending position of the current token.

protected int

Returns:

the position of a token separator in the current header, or at the end
findTokenSeparator
(final int
the position in the current header at which to start the search
pos
)

Determines the position of the next token separator.

protected int

Returns:

the position of the token start in the current header, negative if no token start could be found
findTokenStart
(final int
the position in the current header at which to start the search
pos
)

Determines the starting position of the next token.

public boolean
hasNext()

Implements org.apache.http.TokenIterator.hasNext.

Indicates whether there is another token in this iteration.
protected boolean

Returns:

true if the character is an HTTP separator
isHttpSeparator
(final char
the character to check
ch
)

Checks whether a character is an HTTP separator.

protected boolean

Returns:

true if the character is a valid token start, false otherwise
isTokenChar
(final char
the character to check
ch
)

Checks whether a character is a valid token character.

protected boolean

Returns:

true if the character is a token separator, false otherwise
isTokenSeparator
(final char
the character to check
ch
)

Checks whether a character is a token separator.

protected boolean

Returns:

true if the character is whitespace, false otherwise
isWhitespace
(final char
the character to check
ch
)

Checks whether a character is a whitespace character.

public final Object

Returns:

the next token in this iteration
next
()

Implements java.util.Iterator.next.

Returns the next token.
public String

Returns:

the next token in this iteration
nextToken
()

Implements org.apache.http.TokenIterator.nextToken.

Obtains the next token from this iteration.
public final void
remove()

Overrides default java.util.Iterator.remove.

Removing tokens is not supported.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

currentHeaderback to summary
protected String currentHeader

The value of the current header. This is the header value that includes currentToken. Undefined if the iteration is over.

currentTokenback to summary
protected String currentToken

The token to be returned by the next call to nextToken(). null if the iteration is over.

headerItback to summary
protected final HeaderIterator headerIt

The iterator from which to obtain the next header.

HTTP_SEPARATORSback to summary
public static final String HTTP_SEPARATORS

The HTTP separator characters. Defined in RFC 2616, section 2.2.

searchPosback to summary
protected int searchPos

The position after currentToken in currentHeader. Undefined if the iteration is over.

Constructor Detail

BasicTokenIteratorback to summary
public BasicTokenIterator(final HeaderIterator headerIterator)

Creates a new instance of BasicTokenIterator.

Parameters
headerIterator:HeaderIterator

the iterator for the headers to tokenize

Method Detail

createTokenback to summary
protected String createToken(final String value, final int start, final int end)

Creates a new token to be returned. Called from 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.

Parameters
value:String

the full header value from which to create a token

start:int

the index of the first token character

end:int

the index after the last token character

Returns:String

a string representing the token identified by the arguments

findNextback to summary
protected int findNext(final int pos) throws ParseException

Determines the next token. If found, the token is stored in 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.

Parameters
pos:int

the position in the current header at which to start the search, -1 to search in the first header

Returns:int

the position after the found token in the current header, or negative if there was no next token

Exceptions
ParseException:
if an invalid header value is encountered
findTokenEndback to summary
protected int findTokenEnd(final int from)

Determines the ending position of the current token. This method will not leave the current header value, since the end of the header value is a token boundary.

Parameters
from:int

the position of the first character of the token

Returns:int

the position after the last character of the token. The behavior is undefined if from does not point to a token character in the current header value.

findTokenSeparatorback to summary
protected int findTokenSeparator(final int pos)

Determines the position of the next token separator. Because of multi-header joining rules, the end of a header value is a token separator. This method does therefore not need to iterate over headers.

Parameters
pos:int

the position in the current header at which to start the search

Returns:int

the position of a token separator in the current header, or at the end

Exceptions
ParseException:
if a new token is found before a token separator. RFC 2616, section 2.1 explicitly requires a comma between tokens for #.
findTokenStartback to summary
protected int findTokenStart(final int pos)

Determines the starting position of the next token. This method will iterate over headers if necessary.

Parameters
pos:int

the position in the current header at which to start the search

Returns:int

the position of the token start in the current header, negative if no token start could be found

hasNextback to summary
public boolean hasNext()

Implements org.apache.http.TokenIterator.hasNext.

Doc from org.apache.http.TokenIterator.hasNext.

Indicates whether there is another token in this iteration.

Returns:boolean

true if there is another token, false otherwise

Annotations
@Override
isHttpSeparatorback to summary
protected boolean isHttpSeparator(final char ch)

Checks whether a character is an HTTP separator. The implementation in this class checks only for the HTTP separators defined in RFC 2616, section 2.2. If you need to detect other separators beyond the US-ASCII character set, override this method.

Parameters
ch:char

the character to check

Returns:boolean

true if the character is an HTTP separator

isTokenCharback to summary
protected boolean isTokenChar(final char ch)

Checks whether a character is a valid token character. Whitespace, control characters, and HTTP separators are not valid token characters. The HTTP specification (RFC 2616, section 2.2) defines tokens only for the US-ASCII character set, this method extends the definition to other character sets.

Parameters
ch:char

the character to check

Returns:boolean

true if the character is a valid token start, false otherwise

isTokenSeparatorback to summary
protected boolean isTokenSeparator(final char ch)

Checks whether a character is a token separator. RFC 2616, section 2.1 defines comma as the separator for #token sequences. The end of a header value will also separate tokens, but that is not a character check.

Parameters
ch:char

the character to check

Returns:boolean

true if the character is a token separator, false otherwise

isWhitespaceback to summary
protected boolean isWhitespace(final char ch)

Checks whether a character is a whitespace character. RFC 2616, section 2.2 defines space and horizontal tab as whitespace. The optional preceeding line break is irrelevant, since header continuation is handled transparently when parsing messages.

Parameters
ch:char

the character to check

Returns:boolean

true if the character is whitespace, false otherwise

nextback to summary
public final Object next() throws NoSuchElementException, ParseException

Implements java.util.Iterator.next.

Returns the next token. Same as nextToken, but with generic return type.

Returns:Object

the next token in this iteration

Annotations
@Override
Exceptions
NoSuchElementException:
if there are no more tokens
ParseException:
if an invalid header value is encountered
nextTokenback to summary
public String nextToken() throws NoSuchElementException, ParseException

Implements org.apache.http.TokenIterator.nextToken.

Obtains the next token from this iteration.

Returns:String

the next token in this iteration

Annotations
@Override
Exceptions
NoSuchElementException:
if the iteration is already over
ParseException:
if an invalid header value is encountered
removeback to summary
public final void remove() throws UnsupportedOperationException

Overrides default java.util.Iterator.remove.

Removing tokens is not supported.

Annotations
@Override
Exceptions
UnsupportedOperationException:
always