Package org.htmlparser.lexer
Class StringSource
- java.lang.Object
-
- java.io.Reader
-
- org.htmlparser.lexer.Source
-
- org.htmlparser.lexer.StringSource
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Serializable,java.lang.AutoCloseable,java.lang.Readable
public class StringSource extends Source
A source of characters based on a String.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description StringSource(java.lang.String string)Construct a source using the provided string.StringSource(java.lang.String string, java.lang.String character_set)Construct a source using the provided string and encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Get the number of available characters.voidclose()Does nothing.voiddestroy()Close the source.chargetCharacter(int offset)Retrieve a character again.voidgetCharacters(char[] array, int offset, int start, int end)Retrieve characters again.voidgetCharacters(java.lang.StringBuffer buffer, int offset, int length)Append characters already read into aStringBuffer.java.lang.StringgetEncoding()Get the encoding being used to convert characters.java.lang.StringgetString(int offset, int length)Retrieve a string comprised of characters already read.voidmark(int readAheadLimit)Mark the present position in the source.booleanmarkSupported()Tell whether this source supports the mark() operation.intoffset()Get the position (in characters).intread()Read a single character.intread(char[] cbuf)Read characters into an array.intread(char[] cbuf, int off, int len)Read characters into a portion of an array.booleanready()Tell whether this source is ready to be read.voidreset()Reset the source.voidsetEncoding(java.lang.String character_set)Set the encoding to the given character set.longskip(long n)Skip characters.voidunread()Undo the read of a single character.
-
-
-
Field Detail
-
mString
protected java.lang.String mString
The source of characters.
-
mOffset
protected int mOffset
The current offset into the string.
-
mEncoding
protected java.lang.String mEncoding
The encoding to report. Only used bygetEncoding().
-
mMark
protected int mMark
The bookmark.
-
-
Constructor Detail
-
StringSource
public StringSource(java.lang.String string)
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
public StringSource(java.lang.String string, java.lang.String character_set)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 Detail
-
getEncoding
public java.lang.String getEncoding()
Get the encoding being used to convert characters.- Specified by:
getEncodingin classSource- Returns:
- The current encoding.
-
setEncoding
public void setEncoding(java.lang.String character_set) throws ParserExceptionSet the encoding to the given character set. This simply sets the encoding reported bygetEncoding().- Specified by:
setEncodingin classSource- Parameters:
character_set- The character set to use to convert characters.- Throws:
ParserException- Not thrown.
-
close
public void close() throws java.io.IOExceptionDoes nothing. It's supposed to close the source, but use destroy() instead.
-
read
public int read() throws java.io.IOExceptionRead a single character.
-
read
public int read(char[] cbuf, int off, int len) throws java.io.IOExceptionRead characters into a portion of an array.
-
read
public int read(char[] cbuf) throws java.io.IOExceptionRead characters into an array.
-
ready
public boolean ready() throws java.io.IOExceptionTell whether this source is ready to be read.- Specified by:
readyin classSource- Returns:
- Equivalent to a non-zero
available(), i.e. there are still more characters to read. - Throws:
java.io.IOException- Thrown if the source is closed.
-
reset
public void reset() throws java.lang.IllegalStateExceptionReset the source. Repositions the read point to begin at zero.
-
markSupported
public boolean markSupported()
Tell whether this source supports the mark() operation.- Specified by:
markSupportedin classSource- Returns:
true.
-
mark
public void mark(int readAheadLimit) throws java.io.IOExceptionMark the present position in the source. Subsequent calls toreset()will attempt to reposition the source to this point.
-
skip
public long skip(long n) throws java.io.IOException, java.lang.IllegalArgumentExceptionSkip characters. Note: n is treated as an int
-
unread
public void unread() throws java.io.IOExceptionUndo the read of a single character.
-
getCharacter
public char getCharacter(int offset) throws java.io.IOExceptionRetrieve a character again.- Specified by:
getCharacterin classSource- Parameters:
offset- The offset of the character.- Returns:
- The character at
offset. - Throws:
java.io.IOException- If the source is closed or an attempt is made to read beyondoffset().
-
getCharacters
public void getCharacters(char[] array, int offset, int start, int end) throws java.io.IOExceptionRetrieve characters again.- Specified by:
getCharactersin 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:
java.io.IOException- If the source is closed or an attempt is made to read beyondoffset().
-
getString
public java.lang.String getString(int offset, int length) throws java.io.IOExceptionRetrieve a string comprised of characters already read. Asking for characters ahead ofoffset()will throw an exception.- Specified by:
getStringin classSource- Parameters:
offset- The offset of the first character.length- The number of characters to retrieve.- Returns:
- A string containing the
lengthcharacters atoffset. - Throws:
java.io.IOException- If the source is closed or an attempt is made to read beyondoffset().
-
getCharacters
public void getCharacters(java.lang.StringBuffer buffer, int offset, int length) throws java.io.IOExceptionAppend characters already read into aStringBuffer. Asking for characters ahead ofoffset()will throw an exception.- Specified by:
getCharactersin classSource- Parameters:
buffer- The buffer to append to.offset- The offset of the first character.length- The number of characters to retrieve.- Throws:
java.io.IOException- If the source is closed or an attempt is made to read beyondoffset().
-
destroy
public void destroy() throws java.io.IOException
-
offset
public int offset()
Get the position (in characters).
-
-