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!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.