Class ReaderInputStream
- java.lang.Object
-
- java.io.InputStream
-
- org.apache.commons.io.input.ReaderInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.lang.AutoCloseable
public class ReaderInputStream extends java.io.InputStream
InputStream
implementation that reads a character stream from aReader
and transforms it to a byte stream using a specified charset encoding. The stream is transformed using aCharsetEncoder
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
Reader
to satisfy a read request on theReaderInputStream
, all reads from theReader
are buffered. There is therefore no well defined correlation between the current position of theReader
and that of theReaderInputStream
. This also implies that in general there is no need to wrap the underlyingReader
in aBufferedReader
.ReaderInputStream
implements the inverse transformation ofInputStreamReader
; in the following example, reading fromin2
would return the same byte sequence as reading fromin
(provided that the initial byte sequence is legal with respect to the charset encoding):InputStream inputStream = ... Charset cs = ... InputStreamReader reader = new InputStreamReader(inputStream, cs); ReaderInputStream in2 = new ReaderInputStream(reader, cs);
ReaderInputStream
implements the same transformation asOutputStreamWriter
, except that the control flow is reversed: both classes transform a character stream into a byte stream, butOutputStreamWriter
pushes data to the underlying stream, whileReaderInputStream
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
InputStream
, but where the most natural way to produce the data is as a character stream, i.e. by providing aReader
instance. An example of a situation where this problem may appear is when implementing thejavax.activation.DataSource
interface from the Java Activation Framework.Given the fact that the
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 theInputStream.available()
method. A call to this method will always return 0. Also, this class doesn't supportInputStream.mark(int)
.Instances of
ReaderInputStream
are not thread safe.- Since:
- 2.0
- See Also:
WriterOutputStream
-
-
Constructor Summary
Constructors Constructor Description ReaderInputStream(java.io.Reader reader)
Deprecated.2.5 useReaderInputStream(Reader, Charset)
insteadReaderInputStream(java.io.Reader reader, java.lang.String charsetName)
Construct a newReaderInputStream
with a default input buffer size of 1024 characters.ReaderInputStream(java.io.Reader reader, java.lang.String charsetName, int bufferSize)
Construct a newReaderInputStream
.ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset)
Construct a newReaderInputStream
with a default input buffer size of 1024 characters.ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder)
Construct a newReaderInputStream
.ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder, int bufferSize)
Construct a newReaderInputStream
.ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset, int bufferSize)
Construct a newReaderInputStream
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the stream.int
read()
Read a single byte.int
read(byte[] b)
Read the specified number of bytes into an array.int
read(byte[] array, int off, int len)
Read the specified number of bytes into an array.
-
-
-
Constructor Detail
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder)
Construct a newReaderInputStream
.- Parameters:
reader
- the targetReader
encoder
- the charset encoder- Since:
- 2.1
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder encoder, int bufferSize)
Construct a newReaderInputStream
.- Parameters:
reader
- the targetReader
encoder
- the charset encoderbufferSize
- the size of the input buffer in number of characters- Since:
- 2.1
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset, int bufferSize)
Construct a newReaderInputStream
.- Parameters:
reader
- the targetReader
charset
- the charset encodingbufferSize
- the size of the input buffer in number of characters
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset)
Construct a newReaderInputStream
with a default input buffer size of 1024 characters.- Parameters:
reader
- the targetReader
charset
- the charset encoding
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, java.lang.String charsetName, int bufferSize)
Construct a newReaderInputStream
.- Parameters:
reader
- the targetReader
charsetName
- the name of the charset encodingbufferSize
- the size of the input buffer in number of characters
-
ReaderInputStream
public ReaderInputStream(java.io.Reader reader, java.lang.String charsetName)
Construct a newReaderInputStream
with a default input buffer size of 1024 characters.- Parameters:
reader
- the targetReader
charsetName
- the name of the charset encoding
-
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader)
Deprecated.2.5 useReaderInputStream(Reader, Charset)
insteadConstruct a newReaderInputStream
that uses the default character encoding with a default input buffer size of 1024 characters.- Parameters:
reader
- the targetReader
-
-
Method Detail
-
read
public int read(byte[] array, int off, int len) throws java.io.IOException
Read the specified number of bytes into an array.- Overrides:
read
in classjava.io.InputStream
- Parameters:
array
- the byte array to read intooff
- the offset to start reading bytes intolen
- the number of bytes to read- Returns:
- the number of bytes read or
-1
if the end of the stream has been reached - Throws:
java.io.IOException
- if an I/O error occurs.
-
read
public int read(byte[] b) throws java.io.IOException
Read the specified number of bytes into an array.- Overrides:
read
in classjava.io.InputStream
- Parameters:
b
- the byte array to read into- Returns:
- the number of bytes read or
-1
if the end of the stream has been reached - Throws:
java.io.IOException
- if an I/O error occurs.
-
read
public int read() throws java.io.IOException
Read a single byte.- Specified by:
read
in classjava.io.InputStream
- Returns:
- either the byte read or
-1
if the end of the stream has been reached - Throws:
java.io.IOException
- if an I/O error occurs.
-
close
public void close() throws java.io.IOException
Close the stream. This method will cause the underlyingReader
to be closed.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Overrides:
close
in classjava.io.InputStream
- Throws:
java.io.IOException
- if an I/O error occurs.
-
-