A source of characters based on an InputStream such as from a URLConnection.
InputStreamSource
public InputStreamSource(InputStream stream)
throws UnsupportedEncodingException
Create a source of characters using the default character set.
stream
- The stream of bytes to use.
InputStreamSource
public InputStreamSource(InputStream stream,
String charset)
throws UnsupportedEncodingException
Create a source of characters.
stream
- The stream of bytes to use.charset
- The character set used in encoding the stream.
InputStreamSource
public InputStreamSource(InputStream stream,
String charset,
int size)
throws UnsupportedEncodingException
Create a source of characters.
stream
- The stream of bytes to use.charset
- The character set used in encoding the stream.size
- The initial character buffer size.
available
public int available()
Get the number of available characters.
- available in interface Source
- The number of characters that can be read without blocking or
zero if the source is closed.
close
public void close()
throws IOException
Does nothing.
It's supposed to close the source, but use destroy() instead.
- close in interface Source
destroy
public void destroy()
throws IOException
- destroy in interface Source
fill
protected void fill(int min)
throws IOException
Fetch more characters from the underlying reader.
Has no effect if the underlying reader has been drained.
min
- The minimum to read.
getCharacter
public char getCharacter(int offset)
throws IOException
Retrieve a character again.
- getCharacter in interface Source
offset
- The offset of the character.
getCharacters
public void getCharacters(StringBuffer buffer,
int offset,
int length)
throws IOException
Append characters already read into a StringBuffer
.
- getCharacters in interface Source
buffer
- The buffer to append to.offset
- The offset of the first character.length
- The number of characters to retrieve.
getCharacters
public void getCharacters(char[] array,
int offset,
int start,
int end)
throws IOException
Retrieve characters again.
- getCharacters in interface Source
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.
getEncoding
public String getEncoding()
Get the encoding being used to convert characters.
- getEncoding in interface Source
getStream
public InputStream getStream()
Get the input stream being used.
- The current input stream.
getString
public String getString(int offset,
int length)
throws IOException
Retrieve a string.
- getString in interface Source
offset
- The offset of the first character.length
- The number of characters to retrieve.
- A string containing the
length
characters at offset
.
mark
public void mark(int readAheadLimit)
throws IOException
Mark the present position in the source.
Subsequent calls to
reset()
will attempt to reposition the source to this point.
- mark in interface Source
readAheadLimit
- Not used.
markSupported
public boolean markSupported()
Tell whether this source supports the mark() operation.
- markSupported in interface Source
offset
public int offset()
Get the position (in characters).
- offset in interface Source
- The number of characters that have already been read, or
EOF
if the source is closed.
read
public int read()
throws IOException
Read a single character.
This method will block until a character is available,
an I/O error occurs, or the end of the stream is reached.
- read in interface Source
- The character read, as an integer in the range 0 to 65535
(0x00-0xffff), or
EOF
if the end of the stream has
been reached
read
public int read(char[] cbuf)
throws IOException
Read characters into an array.
This method will block until some input is available, an I/O error occurs,
or the end of the stream is reached.
- read in interface Source
cbuf
- Destination buffer.
- The number of characters read, or
EOF
if the end of
the stream has been reached.
read
public int read(char[] cbuf,
int off,
int len)
throws IOException
Read characters into a portion of an array. This method will block
until some input is available, an I/O error occurs, or the end of the
stream is reached.
- read in interface Source
cbuf
- Destination bufferoff
- Offset at which to start storing characterslen
- Maximum number of characters to read
- The number of characters read, or
EOF
if the end of
the stream has been reached
ready
public boolean ready()
throws IOException
Tell whether this source is ready to be read.
- ready in interface Source
true
if the next read() is guaranteed not to block
for input, false
otherwise.
Note that returning false does not guarantee that the next read will block.
reset
public void reset()
throws IllegalStateException
Reset the source.
Repositions the read point to begin at zero.
- reset in interface Source
setEncoding
public void setEncoding(String character_set)
throws ParserException
Begins reading from the source with 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 page will have been decoded using the given character set.
Some magic happens here to obtain this result if characters have already
been consumed from this source.
Since a Reader cannot be dynamically altered to use a different character
set, the underlying stream is reset, a new Source is constructed
and a comparison made of the characters read so far with the newly
read characters up to the current position.
If a difference is encountered, or some other problem occurs,
an exception is thrown.
- setEncoding in interface Source
character_set
- The character set to use to convert bytes into
characters.
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 underlying stream won't put up with
these shenanigans.
skip
public long skip(long n)
throws IOException,
IllegalArgumentException
Skip characters.
This method will block until some characters are available,
an I/O error occurs, or the end of the stream is reached.
Note: n is treated as an int
- skip in interface Source
n
- The number of characters to skip.
- The number of characters actually skipped
unread
public void unread()
throws IOException
Undo the read of a single character.
- unread in interface Source