Class InputStreamReader in Java
InputStreamReader class is a character input stream that uses a byte input stream as its data source: it reads bytes from a specified InputStream and translates them into Unicode characters according to a particular platform – and locale-dependent character encoding. This is a very important internationalization feature in Java.
So The InputStreamReader class is a bridge between the byte-oriented world of the InputStream class and the character-oriented world of the Reader class. The InputStreamReader represents a character stream, but it gets its input from an underlying byte stream. An encoding scheme is responsible for translating the bytes to Unicode characters. An InputStreamReader can be created using an explicit encoding scheme or a default encoding scheme.
To read an ISO-8859-5 byte stream as a Unicode character stream, we can construct an InputStreamReader with the encoding “8859_5” as follows:
InputStreamReader myReader= new InputStreamReader(in, "8859_5");
When we create an InputStreamReader, we specify an InputStream from which the InputStreamReader is to read bytes, and we also optionally specify the name of the character encoding used by those bytes. If we do not specify an encoding name, the InputStreamReader uses the default encoding for the default locale.
The InputStreamReader supports the standard Reader methods. It also has a getEncoding() method that returns the name of the encoding being used to convert bytes to characters.
Each time we read from an InputStreamReader object, bytes may be read from the underlying byte stream. To improve efficiency, we can wrap the InputStreamReader in a
BufferedReader.
The Class Structure of the InputStreamReader is given as
public class java.io.InputStreamReader extends java.io.Reader{
// Public Constructors
public InputStreamReader(InputStream in);
public InputStreamReader(InputStream in, String enc) throws UnsupportedEncodingException;
// Public Instance Methods
public void close() throws IOException;// Defines Reader
public String getEncoding();
public int read() throws IOException;// Overrides Reader
public int read(char [ ] cbuf, int off, int len) throws IOException;//Defines Reader
public boolean ready() throws IOException;// Overrides Reader
}
The details of the structure are given as:
public InputStreamReader(InputStream in);
public InputStreamReader(InputStream in) constructor creates an InputStreamReader that gets its data from in and translates bytes to characters using the system’s default encoding scheme.
Parameter
in-The input stream to use.
public InputStreamReader(InputStream in, String enc) throws UnsupportedEncodingException;
public InputStreamReader(InputStream in, String enc) throws UnsupportedEncodingException constructor creates an InputStreamReader that gets its data from in and translates bytes to characters using the given encoding scheme.
Parameter
in-The input stream to use.
enc–
public void close() throws IOException;// Defines Reader
public void close() throws IOException method calls the close() method of the underlying input stream, which releases any system resources associated with this object.
public String getEncoding();
public String getEncoding() method returns the name of the character encoding scheme this InputStreamReader is currently using.
This method returns a String that contains the name of the character encoding scheme of this reader.
public int read() throws IOException;// Overrides Reader
public int read() throws IOException method reads a character of input. The method returns the next character that has been read and converted from the underlying byte-oriented InputStream.
The InputStreamReader class uses buffering internally, so this method returns immediately unless the buffer is empty. If the buffer is empty, a new block of bytes is read from the InputStream and converted to characters.
The method blocks until the character is read, the end of the stream is encountered, or an exception is thrown.
This method returns the next character of data or -1 if the end of the stream is encountered
public int read(char [ ] cbuf, int off, int len) throws IOException;//Defines Reader
public int read(char [ ] cbuf, int off, int len) throws IOException method reads up to len characters of input into the given array starting at index off.
The InputStreamReader class uses buffering internally, so this method returns immediately if there is enough data in the buffer.
If there is not enough data, a new block of bytes is read from the InputStream and converted to characters. The method blocks until some data is available.
This method returns the actual number of characters read or -1 if the end of the stream is encountered immediately.
Parameter
cbuf – An array of characters to be filled from the stream.
off – An offset into the array.
len – The number of characters to read.
public boolean ready() throws IOException;// Overrides Reader
public boolean ready() throws IOException method returns a boolean value that indicates whether or not the reader is ready to be read.
If there is data available in the internal buffer or if there are bytes available to be read from the underlying byte stream, the method returns true. Otherwise it returns false.
This method returns true if the reader is ready to be read; false otherwise.
Apart from these InputStreamReader class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)
InputStreamReader class also has inherited methods from Reader. They are as follows:
- markSupported()
- reset()
- mark()
- read(char[])
- skip(long)