Package org.htmlparser.lexer
Class StringSource
java.lang.Object
java.io.Reader
org.htmlparser.lexer.Source
org.htmlparser.lexer.StringSource
- All Implemented Interfaces:
Closeable
,Serializable
,AutoCloseable
,Readable
A source of characters based on a String.
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionStringSource
(String string) Construct a source using the provided string.StringSource
(String string, String character_set) Construct a source using the provided string and encoding. -
Method Summary
Modifier and TypeMethodDescriptionint
Get the number of available characters.void
close()
Does nothing.void
destroy()
Close the source.char
getCharacter
(int offset) Retrieve a character again.void
getCharacters
(char[] array, int offset, int start, int end) Retrieve characters again.void
getCharacters
(StringBuffer buffer, int offset, int length) Append characters already read into aStringBuffer
.Get the encoding being used to convert characters.getString
(int offset, int length) Retrieve a string comprised of characters already read.void
mark
(int readAheadLimit) Mark the present position in the source.boolean
Tell whether this source supports the mark() operation.int
offset()
Get the position (in characters).int
read()
Read a single character.int
read
(char[] cbuf) Read characters into an array.int
read
(char[] cbuf, int off, int len) Read characters into a portion of an array.boolean
ready()
Tell whether this source is ready to be read.void
reset()
Reset the source.void
setEncoding
(String character_set) Set the encoding to the given character set.long
skip
(long n) Skip characters.void
unread()
Undo the read of a single character.Methods inherited from class java.io.Reader
nullReader, read, transferTo
-
Field Details
-
mString
The source of characters. -
mOffset
protected int mOffsetThe current offset into the string. -
mEncoding
The encoding to report. Only used bygetEncoding()
. -
mMark
protected int mMarkThe bookmark.
-
-
Constructor Details
-
StringSource
Construct a source using the provided string. Until it is set, the encoding will be reported as ISO-8859-1.- Parameters:
string
- The source of characters.
-
StringSource
Construct a source using the provided string and encoding. The encoding is only used bygetEncoding()
.- Parameters:
string
- The source of characters.character_set
- The encoding to report.
-
-
Method Details
-
getEncoding
Get the encoding being used to convert characters.- Specified by:
getEncoding
in classSource
- Returns:
- The current encoding.
-
setEncoding
Set the encoding to the given character set. This simply sets the encoding reported bygetEncoding()
.- Specified by:
setEncoding
in classSource
- Parameters:
character_set
- The character set to use to convert characters.- Throws:
ParserException
- Not thrown.
-
close
Does nothing. It's supposed to close the source, but use destroy() instead.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in classSource
- Throws:
IOException
- not used- See Also:
-
read
Read a single character.- Specified by:
read
in classSource
- Returns:
- The character read, as an integer in the range 0 to 65535
(0x00-0xffff), or
EOF
if the source is exhausted. - Throws:
IOException
- If an I/O error occurs.
-
read
Read characters into a portion of an array.- Specified by:
read
in classSource
- Parameters:
cbuf
- Destination bufferoff
- Offset at which to start storing characterslen
- Maximum number of characters to read- Returns:
- The number of characters read, or
EOF
if the source is exhausted. - Throws:
IOException
- If an I/O error occurs.
-
read
Read characters into an array.- Specified by:
read
in classSource
- Parameters:
cbuf
- Destination buffer.- Returns:
- The number of characters read, or
EOF
if the source is exhausted. - Throws:
IOException
- If an I/O error occurs.
-
ready
Tell whether this source is ready to be read.- Specified by:
ready
in classSource
- Returns:
- Equivalent to a non-zero
available()
, i.e. there are still more characters to read. - Throws:
IOException
- Thrown if the source is closed.
-
reset
Reset the source. Repositions the read point to begin at zero.- Specified by:
reset
in classSource
- Throws:
IllegalStateException
- If the source has been closed.
-
markSupported
public boolean markSupported()Tell whether this source supports the mark() operation.- Specified by:
markSupported
in classSource
- Returns:
true
.
-
mark
Mark the present position in the source. Subsequent calls toreset()
will attempt to reposition the source to this point.- Specified by:
mark
in classSource
- Parameters:
readAheadLimit
- Not used.- Throws:
IOException
- Thrown if the source is closed.
-
skip
Skip characters. Note: n is treated as an int- Specified by:
skip
in classSource
- Parameters:
n
- The number of characters to skip.- Returns:
- The number of characters actually skipped
- Throws:
IllegalArgumentException
- Ifn
is negative.IOException
- If the source is closed.
-
unread
Undo the read of a single character.- Specified by:
unread
in classSource
- Throws:
IOException
- If no characters have been read or the source is closed.
-
getCharacter
Retrieve a character again.- Specified by:
getCharacter
in classSource
- Parameters:
offset
- The offset of the character.- Returns:
- The character at
offset
. - Throws:
IOException
- If the source is closed or an attempt is made to read beyondoffset()
.
-
getCharacters
Retrieve characters again.- Specified by:
getCharacters
in classSource
- Parameters:
array
- The array of characters.offset
- The starting position in the array where characters are to be placed.start
- The starting position, zero based.end
- The ending position (exclusive, i.e. the character at the ending position is not included), zero based.- Throws:
IOException
- If the source is closed or an attempt is made to read beyondoffset()
.
-
getString
Retrieve a string comprised of characters already read. Asking for characters ahead ofoffset()
will throw an exception.- Specified by:
getString
in classSource
- Parameters:
offset
- The offset of the first character.length
- The number of characters to retrieve.- Returns:
- A string containing the
length
characters atoffset
. - Throws:
IOException
- If the source is closed or an attempt is made to read beyondoffset()
.
-
getCharacters
Append characters already read into aStringBuffer
. Asking for characters ahead ofoffset()
will throw an exception.- Specified by:
getCharacters
in classSource
- Parameters:
buffer
- The buffer to append to.offset
- The offset of the first character.length
- The number of characters to retrieve.- Throws:
IOException
- If the source is closed or an attempt is made to read beyondoffset()
.
-
destroy
Close the source. Once a source has been closed, furtherread
,ready
,mark
,reset
,skip
,unread
,getCharacter
orgetString
invocations will throw an IOException. Closing a previously-closed source, however, has no effect.- Specified by:
destroy
in classSource
- Throws:
IOException
- Not thrown
-
offset
public int offset()Get the position (in characters). -
available
public int available()Get the number of available characters.
-