Home >Java >javaTutorial >How to construct the InputStreamReader stream in JAVA

How to construct the InputStreamReader stream in JAVA

WBOY
WBOYforward
2023-05-17 22:43:341575browse

1. InputStreamReader class

API documentation description: The InputStreamReader class is a bridge from a byte stream to a character stream: it reads bytes using the specified character set and decodes them into characters. The character set it uses can be set by specifying a name, explicitly specifying it, or accepting the platform default character set. Each call to an InputStreamReader's read() method may result in one or more bytes being read from the underlying byte input stream. To achieve efficient conversion of bytes to characters, more bytes can be extracted from the underlying stream than required to satisfy the current read operation. In order to operate more efficiently, please consider using InputStreamReader as the basis and packaging it in BufferedReader

It inherits the Reader class

public class InputStreamReader extends Reader {}

1) How to understand the bridge from byte stream to character stream ?

1. The unit of computer storage is bytes. For example, although there are characters such as Chinese characters in the txt text, to the computer, they exist in the form of bytes

2. Byte stream reading is single-byte reading, but different character sets require different numbers to decode into characters, so byte stream reading will report an error

Read from the byte stream for caching Bytes and decoded into characters through the character set are returned, which needs to be implemented using a stream. This is the form of the character stream

4. The InputStreamReader stream plays this role, realizing the conversion from a byte stream to a character stream.

2) How do you understand using the specified character set to read bytes and decode them into characters?

Bytes are essentially 8 binary bits, and different character sets decode the same byte to produce different character results. Therefore, you must specify the appropriate character set when reading characters, otherwise The read content will produce garbled characters

3) The character set it uses can be specified by name, or it can be specified explicitly, or it can accept the default character set of the platform. How to understand?

means that the InputStreamReader class has multiple methods or multiple constructors to set the character set

4) Each time the read() method of an InputStreamReader is called, it may cause How to understand reading one or more bytes from the underlying byte input stream?

read() method will try to read 2 characters from the underlying byte stream into the character buffer. Note that this is a try. If it encounters the last character of the file, it can only read 1 character is obtained, so the number of bytes read by each read() method is variable

5) In order to achieve effective conversion of bytes to characters, the ratio can be extracted from the basic stream To meet more bytes required for the current read operation, please consider wrapping the InputStreamReader in BufferedReader

I don’t understand this yet. You need to understand the BufferedReader class and compare the reading efficiency to get the answer

2. InputStreamReader construction method

1) Use the default character set to construct the InputStreamReader stream: the essence is to initialize a variable in its instance domain, and no settings about the character set are seen

 public InputStreamReader(InputStream in) {
        super(in);
        try {
            sd = StreamDecoder.forInputStreamReader(in, this, (String)null);
        } catch (UnsupportedEncodingException e) {
           
            throw new Error(e);
        }
    }

2) Use the specified character set name to construct the InputStreamReader stream: the essence is to initialize a variable in its instance domain. You can find that the character set is the third parameter of the initialization method

 public InputStreamReader(InputStream in, String charsetName)
        throws UnsupportedEncodingException
    {
        super(in);
        if (charsetName == null)
            throw new NullPointerException("charsetName");
        sd = StreamDecoder.forInputStreamReader(in, this, charsetName);
    }

3) sd variable: It is essentially an object of the StreamDecoder class. The construction method of InputStreamReader is to initialize this object.

private final StreamDecoder sd;

3. InputStreamReaderAPI

1. We can find that all APIs of the InputStreamReaderAPI class use sd variable, so it can be seen that the essence of the method of the InputStreamReader class is to call the StreamDecoder class method

2. Therefore, we need to understand the StreamDecoder class in order to understand how the methods of the InputStreamReader class play a substantial role

    /**
     * 获取设置的字符集
     */
    public String getEncoding() {
        return sd.getEncoding();
    }
 
    /**
     * 读取流并返回一个字符,遇到文件末尾返回-1
     */
    public int read() throws IOException {
        return sd.read();
    }
 
    /**
     * 读取字符到字符数组的部分中,遇到文件末尾返回-1
     */
    public int read(char cbuf[], int offset, int length) throws IOException {
        return sd.read(cbuf, offset, length);
    }
 
    /**
     * 检测流是否准备好呗读取
     */
    public boolean ready() throws IOException {
        return sd.ready();
    }
 
    /**
    * 关闭流并释放资源
    */
    public void close() throws IOException {
        sd.close();
    }

4. The relationship between the InputStreamReader class and the FileReader class

1. The FileReader class is just a simple derivative of InputStreamReader and does not extend any functions

2. The essence of the data read by the FileReader class is that of the InputStreamReader class. Reading, and the data read by InputStreamReader is actually read by the StreamDecoder class

3. Therefore, when using the character input stream, the StreamDecoder class is actually at play

The above is the detailed content of How to construct the InputStreamReader stream in JAVA. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete