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.InputStreamInputStreamimplementation that reads a character stream from aReaderand transforms it to a byte stream using a specified charset encoding. The stream is transformed using aCharsetEncoderobject, 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
Readerto satisfy a read request on theReaderInputStream, all reads from theReaderare buffered. There is therefore no well defined correlation between the current position of theReaderand that of theReaderInputStream. This also implies that in general there is no need to wrap the underlyingReaderin aBufferedReader.ReaderInputStreamimplements the inverse transformation ofInputStreamReader; in the following example, reading fromin2would return the same byte sequence as reading fromin(provided that the initial byte sequence is legal with respect to the charset encoding):To build an instance, use
ReaderInputStream.Builder.InputStream inputStream = ... Charset cs = ... InputStreamReader reader = new InputStreamReader(inputStream, cs); ReaderInputStream in2 = ReaderInputStream.builder() .setReader(reader) .setCharset(cs) .get();
ReaderInputStreamimplements the same transformation asOutputStreamWriter, except that the control flow is reversed: both classes transform a character stream into a byte stream, butOutputStreamWriterpushes data to the underlying stream, whileReaderInputStreampulls 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 aReaderinstance. An example of a situation where this problem may appear is when implementing thejavax.activation.DataSourceinterface from the Java Activation Framework.The
InputStream.available()method of this class always returns 0. The methodsInputStream.mark(int)andInputStream.reset()are not supported.Instances of
ReaderInputStreamare not thread safe.- Since:
 - 2.0
 - See Also:
 ReaderInputStream.Builder,WriterOutputStream
 
- 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description static classReaderInputStream.BuilderBuilds a newReaderInputStream. 
- 
Constructor Summary
Constructors Constructor Description ReaderInputStream(java.io.Reader reader)Deprecated.Usebuilder()insteadReaderInputStream(java.io.Reader reader, java.lang.String charsetName)Deprecated.Usebuilder()insteadReaderInputStream(java.io.Reader reader, java.lang.String charsetName, int bufferSize)Deprecated.Usebuilder()insteadReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset)Deprecated.Usebuilder()instead, will be protected for subclasses.ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder charsetEncoder)Deprecated.Usebuilder()insteadReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder charsetEncoder, int bufferSize)Deprecated.Usebuilder()insteadReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset, int bufferSize)Deprecated.Usebuilder()instead 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static ReaderInputStream.Builderbuilder()Constructs a newReaderInputStream.Builder.voidclose()Closes the stream.intread()Reads a single byte.intread(byte[] b)Reads the specified number of bytes into an array.intread(byte[] array, int off, int len)Reads the specified number of bytes into an array. 
 - 
 
- 
- 
Constructor Detail
- 
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader)
Deprecated.Usebuilder()insteadConstructs a newReaderInputStreamthat uses the default character encoding with a default input buffer size of 8192 characters.- Parameters:
 reader- the targetReader
 
- 
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset)Deprecated.Usebuilder()instead, will be protected for subclasses.Constructs a newReaderInputStreamwith a default input buffer size of 8192 characters.The encoder created for the specified charset will use
CodingErrorAction.REPLACEfor malformed input and unmappable characters.- Parameters:
 reader- the targetReadercharset- the charset encoding
 
- 
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader, java.nio.charset.Charset charset, int bufferSize)Deprecated.Usebuilder()insteadConstructs a newReaderInputStream.The encoder created for the specified charset will use
CodingErrorAction.REPLACEfor malformed input and unmappable characters.- Parameters:
 reader- the targetReader.charset- the charset encoding.bufferSize- the size of the input buffer in number of characters.
 
- 
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder charsetEncoder)Deprecated.Usebuilder()insteadConstructs a newReaderInputStream.This constructor does not call
reseton the provided encoder. The caller of this constructor should do this when providing an encoder which had already been in use.- Parameters:
 reader- the targetReadercharsetEncoder- the charset encoder- Since:
 - 2.1
 
 
- 
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader, java.nio.charset.CharsetEncoder charsetEncoder, int bufferSize)Deprecated.Usebuilder()insteadConstructs a newReaderInputStream.This constructor does not call
reseton the provided encoder. The caller of this constructor should do this when providing an encoder which had already been in use.- Parameters:
 reader- the targetReadercharsetEncoder- the charset encoder, null defaults to the default Charset encoder.bufferSize- the size of the input buffer in number of characters- Since:
 - 2.1
 
 
- 
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader, java.lang.String charsetName)Deprecated.Usebuilder()insteadConstructs a newReaderInputStreamwith a default input buffer size of 8192 characters.The encoder created for the specified charset will use
CodingErrorAction.REPLACEfor malformed input and unmappable characters.- Parameters:
 reader- the targetReadercharsetName- the name of the charset encoding
 
- 
ReaderInputStream
@Deprecated public ReaderInputStream(java.io.Reader reader, java.lang.String charsetName, int bufferSize)Deprecated.Usebuilder()insteadConstructs a newReaderInputStream.The encoder created for the specified charset will use
CodingErrorAction.REPLACEfor malformed input and unmappable characters.- Parameters:
 reader- the targetReadercharsetName- the name of the charset encoding, null maps to the default Charset.bufferSize- the size of the input buffer in number of characters
 
 - 
 
- 
Method Detail
- 
builder
public static ReaderInputStream.Builder builder()
Constructs a newReaderInputStream.Builder.- Returns:
 - a new 
ReaderInputStream.Builder. - Since:
 - 2.12.0
 
 
- 
close
public void close() throws java.io.IOExceptionCloses the stream. This method will cause the underlyingReaderto be closed.- Specified by:
 closein interfacejava.lang.AutoCloseable- Specified by:
 closein interfacejava.io.Closeable- Overrides:
 closein classjava.io.InputStream- Throws:
 java.io.IOException- if an I/O error occurs.
 
- 
read
public int read() throws java.io.IOExceptionReads a single byte.- Specified by:
 readin classjava.io.InputStream- Returns:
 - either the byte read or 
-1if 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.IOExceptionReads the specified number of bytes into an array.- Overrides:
 readin classjava.io.InputStream- Parameters:
 b- the byte array to read into- Returns:
 - the number of bytes read or 
-1if the end of the stream has been reached - Throws:
 java.io.IOException- if an I/O error occurs.
 
- 
read
public int read(byte[] array, int off, int len) throws java.io.IOExceptionReads the specified number of bytes into an array.- Overrides:
 readin 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 
-1if the end of the stream has been reached - Throws:
 java.io.IOException- if an I/O error occurs.
 
 - 
 
 -