Package org.htmlparser.lexer
Class Source
- java.lang.Object
-
- java.io.Reader
-
- org.htmlparser.lexer.Source
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Serializable,java.lang.AutoCloseable,java.lang.Readable
- Direct Known Subclasses:
InputStreamSource,StringSource
public abstract class Source extends java.io.Reader implements java.io.SerializableA buffered source of characters. A Source is very similar to a Reader, like:new InputStreamReader (connection.getInputStream (), charset)
It differs from the above, in three ways:- the fetching of bytes may be asynchronous
- the character set may be changed, which resets the input stream
- characters may be requested more than once, so in general they will be buffered
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intEOFReturn value when the source is exhausted.
-
Constructor Summary
Constructors Constructor Description Source()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract intavailable()Get the number of available characters.abstract voidclose()Does nothing.abstract voiddestroy()Close the source.abstract chargetCharacter(int offset)Retrieve a character again.abstract voidgetCharacters(char[] array, int offset, int start, int end)Retrieve characters again.abstract voidgetCharacters(java.lang.StringBuffer buffer, int offset, int length)Append characters already read into aStringBuffer.abstract java.lang.StringgetEncoding()Get the encoding being used to convert characters.abstract java.lang.StringgetString(int offset, int length)Retrieve a string comprised of characters already read.abstract voidmark(int readAheadLimit)Mark the present position.abstract booleanmarkSupported()Tell whether this source supports the mark() operation.abstract intoffset()Get the position (in characters).abstract intread()Read a single character.abstract intread(char[] cbuf)Read characters into an array.abstract intread(char[] cbuf, int off, int len)Read characters into a portion of an array.abstract booleanready()Tell whether this source is ready to be read.abstract voidreset()Reset the source.abstract voidsetEncoding(java.lang.String character_set)Set the encoding to the given character set.abstract longskip(long n)Skip characters.abstract voidunread()Undo the read of a single character.
-
-
-
Field Detail
-
EOF
public static final int EOF
Return value when the source is exhausted. Has a value of -1.- See Also:
- Constant Field Values
-
-
Method Detail
-
getEncoding
public abstract java.lang.String getEncoding()
Get the encoding being used to convert characters.- Returns:
- The current encoding.
-
setEncoding
public abstract void setEncoding(java.lang.String character_set) throws ParserExceptionSet the encoding to the given character set. If the current encoding is the same as the requested encoding, this method is a no-op. Otherwise any subsequent characters read from this source will have been decoded using the given character set.If characters have already been consumed from this source, it is expected that an exception will be thrown if the characters read so far would be different if the encoding being set was used from the start.
- Parameters:
character_set- The character set to use to convert characters.- Throws:
ParserException- If a character mismatch occurs between characters already provided and those that would have been returned had the new character set been in effect from the beginning. An exception is also thrown if the character set is not recognized.
-
close
public abstract void close() throws java.io.IOExceptionDoes nothing. It's supposed to close the source, but usedestroy()instead.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein classjava.io.Reader- Throws:
java.io.IOException- not used- See Also:
destroy()
-
read
public abstract int read() throws java.io.IOExceptionRead a single character. This method will block until a character is available, an I/O error occurs, or the source is exhausted.- Overrides:
readin classjava.io.Reader- Returns:
- The character read, as an integer in the range 0 to 65535
(0x00-0xffff), or
EOFif the source is exhausted. - Throws:
java.io.IOException- If an I/O error occurs.
-
read
public abstract int read(char[] cbuf, int off, int len) throws java.io.IOExceptionRead characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the source is exhausted.- Specified by:
readin classjava.io.Reader- Parameters:
cbuf- Destination bufferoff- Offset at which to start storing characterslen- Maximum number of characters to read- Returns:
- The number of characters read, or
EOFif the source is exhausted. - Throws:
java.io.IOException- If an I/O error occurs.
-
read
public abstract int read(char[] cbuf) throws java.io.IOExceptionRead characters into an array. This method will block until some input is available, an I/O error occurs, or the source is exhausted.- Overrides:
readin classjava.io.Reader- Parameters:
cbuf- Destination buffer.- Returns:
- The number of characters read, or
EOFif the source is exhausted. - Throws:
java.io.IOException- If an I/O error occurs.
-
ready
public abstract boolean ready() throws java.io.IOExceptionTell whether this source is ready to be read.- Overrides:
readyin classjava.io.Reader- Returns:
trueif the next read() is guaranteed not to block for input,falseotherwise. Note that returning false does not guarantee that the next read will block.- Throws:
java.io.IOException- If an I/O error occurs.
-
reset
public abstract void reset()
Reset the source. Repositions the read point to begin at zero.- Overrides:
resetin classjava.io.Reader
-
markSupported
public abstract boolean markSupported()
Tell whether this source supports the mark() operation.- Overrides:
markSupportedin classjava.io.Reader- Returns:
trueif and only if this source supports the mark operation.
-
mark
public abstract void mark(int readAheadLimit) throws java.io.IOExceptionMark the present position. Subsequent calls toreset()will attempt to reposition the source to this point. Not all sources support the mark() operation.- Overrides:
markin classjava.io.Reader- Parameters:
readAheadLimit- The minimum number of characters that can be read before this mark becomes invalid.- Throws:
java.io.IOException- If an I/O error occurs.
-
skip
public abstract long skip(long n) throws java.io.IOExceptionSkip characters. This method will block until some characters are available, an I/O error occurs, or the source is exhausted. Note: n is treated as an int- Overrides:
skipin classjava.io.Reader- Parameters:
n- The number of characters to skip.- Returns:
- The number of characters actually skipped
- Throws:
java.io.IOException- If an I/O error occurs.
-
unread
public abstract void unread() throws java.io.IOExceptionUndo the read of a single character.- Throws:
java.io.IOException- If the source is closed or no characters have been read.
-
getCharacter
public abstract char getCharacter(int offset) throws java.io.IOExceptionRetrieve a character again.- Parameters:
offset- The offset of the character.- Returns:
- The character at
offset. - Throws:
java.io.IOException- If the source is closed or the offset is beyondoffset().
-
getCharacters
public abstract void getCharacters(char[] array, int offset, int start, int end) throws java.io.IOExceptionRetrieve characters again.- 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 the start or end is beyondoffset().
-
getString
public abstract java.lang.String getString(int offset, int length) throws java.io.IOExceptionRetrieve a string comprised of characters already read.- 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.
-
getCharacters
public abstract void getCharacters(java.lang.StringBuffer buffer, int offset, int length) throws java.io.IOExceptionAppend characters already read into aStringBuffer.- 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 the offset or (offset + length) is beyondoffset().
-
destroy
public abstract void destroy() throws java.io.IOExceptionClose the source. Once a source has been closed, furtherread,ready,mark,reset,skip,unread,getCharacterorgetStringinvocations will throw an IOException. Closing a previously-closed source, however, has no effect.- Throws:
java.io.IOException- If an I/O error occurs.
-
offset
public abstract int offset()
Get the position (in characters).- Returns:
- The number of characters that have already been read, or
EOFif the source is closed.
-
available
public abstract int available()
Get the number of available characters.- Returns:
- The number of characters that can be read without blocking.
-
-