java.io.InputStream implementation that reads a character stream from a java.io.Reader and transforms it to a byte stream using a specified charset encoding. The stream is transformed using a java.nio.charset.CharsetEncoder object, guaranteeing that all charset encodings supported by the JRE are handled correctly. In particular for charsets such as UTF-16, the implementation ensures that one and only one byte order marker is produced.
Since in general it is not possible to predict the number of characters to be read from the java.io.Reader to satisfy a read request on the ReaderInputStream, all reads from the java.io.Reader are buffered. There is therefore no well defined correlation between the current position of the java.io.Reader and that of the ReaderInputStream. This also implies that in general there is no need to wrap the underlying java.io.Reader in a java.io.BufferedReader.
ReaderInputStream implements the inverse transformation of java.io.InputStreamReader; in the following example, reading from in2 would return the same byte sequence as reading from in (provided that the initial byte sequence is legal with respect to the charset encoding):
InputStream in = ... Charset cs = ... InputStreamReader reader = new InputStreamReader(in, cs); ReaderInputStream in2 = new ReaderInputStream(reader, cs);ReaderInputStream implements the same transformation as java.io.OutputStreamWriter, except that the control flow is reversed: both classes transform a character stream into a byte stream, but java.io.OutputStreamWriter pushes data to the underlying stream, while ReaderInputStream pulls it from the underlying stream.
Note that while there are use cases where there is no alternative to using this class, very often the need to use this class is an indication of a flaw in the design of the code. This class is typically used in situations where an existing API only accepts an java.io.InputStream, but where the most natural way to produce the data is as a character stream, i.e. by providing a java.io.Reader instance. An example of a situation where this problem may appear is when implementing the javax.activation.DataSource interface from the Java Activation Framework.
Given the fact that the java.io.Reader class doesn't provide any way to predict whether the next read operation will block or not, it is not possible to provide a meaningful implementation of the java.io.InputStream#available() method. A call to this method will always return 0. Also, this class doesn't support java.io.InputStream#mark(int).
Instances of ReaderInputStream are not thread safe.
| Constructor and description |
|---|
ReaderInputStream
(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder)Construct a new ReaderInputStream. |
ReaderInputStream
(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder, int bufferSize)Construct a new ReaderInputStream. |
ReaderInputStream
(java.io.Reader reader, java.nio.charset.Charset charset, int bufferSize)Construct a new ReaderInputStream. |
ReaderInputStream
(java.io.Reader reader, java.nio.charset.Charset charset)Construct a new ReaderInputStream with a default input buffer size of 1024 characters. |
ReaderInputStream
(java.io.Reader reader, java.lang.String charsetName, int bufferSize)Construct a new ReaderInputStream. |
ReaderInputStream
(java.io.Reader reader, java.lang.String charsetName)Construct a new ReaderInputStream with a default input buffer size of 1024 characters. |
ReaderInputStream
(java.io.Reader reader)Construct a new ReaderInputStream that uses the default character encoding with a default input buffer size of 1024 characters. |
| Methods inherited from class | Name |
|---|---|
class java.io.InputStream |
java.io.InputStream#read([B, int, int), java.io.InputStream#read([B), java.io.InputStream#read(), java.io.InputStream#close(), java.io.InputStream#mark(int), java.io.InputStream#skip(long), java.io.InputStream#available(), java.io.InputStream#markSupported(), java.io.InputStream#reset(), java.io.InputStream#wait(long, int), java.io.InputStream#wait(long), java.io.InputStream#wait(), java.io.InputStream#equals(java.lang.Object), java.io.InputStream#toString(), java.io.InputStream#hashCode(), java.io.InputStream#getClass(), java.io.InputStream#notify(), java.io.InputStream#notifyAll() |
Construct a new ReaderInputStream.
reader - the target java.io.Readerencoder - the charset encoderConstruct a new ReaderInputStream.
reader - the target java.io.Readerencoder - the charset encoderbufferSize - the size of the input buffer in number of charactersConstruct a new ReaderInputStream.
reader - the target java.io.Readercharset - the charset encodingbufferSize - the size of the input buffer in number of charactersConstruct a new ReaderInputStream with a default input buffer size of 1024 characters.
reader - the target java.io.Readercharset - the charset encodingConstruct a new ReaderInputStream.
reader - the target java.io.ReadercharsetName - the name of the charset encodingbufferSize - the size of the input buffer in number of charactersConstruct a new ReaderInputStream with a default input buffer size of 1024 characters.
reader - the target java.io.ReadercharsetName - the name of the charset encodingConstruct a new ReaderInputStream that uses the default character encoding with a default input buffer size of 1024 characters.
reader - the target java.io.ReaderClose the stream. This method will cause the underlying java.io.Reader to be closed.
Read the specified number of bytes into an array.
b - the byte array to read intooff - the offset to start reading bytes intolen - the number of bytes to read-1
if the end of the stream has been reachedRead the specified number of bytes into an array.
b - the byte array to read into-1
if the end of the stream has been reachedRead a single byte.
-1 if the end of the stream
has been reached