public class ReaderUTF8InputStream
Imagine that you have a Reader object, and you want to call a method
that requires an InputStream. You don't always have access to the underlying
InputStream that the Reader is reading from. This class allows you to
"down-convert" from a character oriented Reader back to a byte oriented
InputStream with correct UTF-8 encoding characters into the bytes.
This is a *proper* conversion from UTF-16 string to UTF-8 byte stream. A
string with full Unicode characters can be streamed out as UTF-8 bytes. It
performs the conversion on the fly, without needing any large byte buffer
Supports UTF-8, the ONLY encoding that guarantees that all characters in the UTF-16
can be expressed without loss of characters.
In spite of that logic, some might still insist that this class is more
general if it had a parameters to specify the encoding to use. This would
greatly slow down the operation of the class, because knowledge of UTF-8
encoding is used to keep the amount of memory low, and to make the conversion
Mark is not supported because you might try to mark in the middle of a
multi-byte sequence, and the sequence would have to be stored away. Have not
worked through all the implications of this, so for now not supported.
Author: Keith Swenson
Copyright: Keith Swenson, all rights reserved
License: This code is made available under the GNU Lesser GPL license.
public ReaderUTF8InputStream(java.io.Reader source)
Composes an InputStream stream from any other Reader you may have around.
source - The Reader object to read from. Must not be
public ReaderUTF8InputStream(java.lang.String source)
Composes a stream from a String. This is a convenience constructor
that constructs a StringReader, and then wraps that Reader.
source - The string to read from. Must not be null.
public int read()
Reads from the Stringreader. If the value is less than 128 it is returned
directly, because UTF-8 encoding is this way. If more than 127 the
character is broken into a multibyte sequence which will be returned over
2 or more subsequent reads.
read in class java.io.InputStream
the value of the next byte of the UTF-8 stream, or -1 if there is
nothing left in the original string.
java.io.IOException - if the original StringReader fails to be read
public void close()
Closes the Stringreader.
close in interface java.io.Closeable
close in interface java.lang.AutoCloseable
close in class java.io.InputStream
java.io.IOException - if the original Reader fails to be closed
public void mark(int limit)
Marks the read limit of the StringReader.
throws an UnsupportedOperationException.
This method is not supported in this class.
It is difficult to figure how to translate the mark in bytes
to a mark in characters, and to return to it correctly.
So simply not supported in this version of the class.
mark in class java.io.InputStream
public void reset()
Resets to the beginning by resetting the wrapped Reader.
reset in class java.io.InputStream
java.io.IOException - if the StringReader fails to be reset