Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
InputStreamReader, OutputStreamWriter and adapter pattern in Java IO are introduced in detail.
1 InputStreamReader Bridges input streams
public class InputStreamReader
extends Reader
Copy the code
InputStreamReader is a bridge from byte flow to character stream: it reads bytes with the specified charset and internally decodes them into characters.
Each call to a read() method in InputStreamReader results in one or more bytes being read from the underlying input stream. To enable efficient byte-to-character conversion, more bytes can be read from the underlying stream ahead of time than is needed to satisfy the current read operation.
BufferedReader is usually used in conjunction with BufferedReader to improve conversion efficiency.
1.1 the constructor
public InputStreamReader(InputStream in)
Create an InputStreamReader that uses the default character set, that is, a byte input stream character input stream.
Pass an InputStream, and of course pass system. in
public InputStreamReader(InputStream in,String charsetName)
throws UnsupportedEncodingException
Creates an InputStreamReader to convert with the specified character set.
1.2 API methods
Most methods inherit or override methods from the parent Reader class
Unique methods:
public String getEncoding()
Returns the name of the character encoding used by this stream, or NULL if the stream is closed
2 OutputStreamWriter Bridges the output stream
public class OutputStreamWriter
extends Writer
Copy the code
OutputStreamWriter is a bridge between character flow and byte stream: characters written to the stream can be encoded into bytes using the specified charset.
Each call to the write() method causes the encoding converter to be called on the given character (or character set). The resulting bytes accumulate in the buffer before writing to the underlying output stream. You can specify the size of this buffer, but the default buffer is large enough for most purposes. Note that the characters passed to the write() method are not buffered.
For maximum efficiency, consider wrapping OutputStreamWriter into BufferedWriter.
2.1 the constructor
new OutputStreamWriter(OutputStream out);
Create an OutputStreamWriter that uses the default character encoding. The default utf-8
new OutputStreamWriter(OutputStream out, String charsetName);
Creates an OutputStreamWriter that uses the specified character set.
2.2 API methods
Most methods inherit or override methods from the parent Writer class
Unique methods:
public String getEncoding()
Returns the name of the character encoding used by this stream.
All other methods inherit or override methods from the parent Writer class.
How does a 3-byte stream turn into a character stream
Byte InputStream character input flows through the InputStreamReader implementation, whose constructor can pass in an InputStream object.
Byte OutputStream character output flows through the OutputStreamWriter implementation, whose constructor can pass in an OutputStream object.
4 Adapter Mode
Adapter is a kind of adaptive middleware, it exists between the two mismatch, used to connect the two, the mismatch becomes a match, simple point of understanding is commonly seen adapters, converters and so on. By converting the interface of one class into the desired interface of another, the adapter pattern allows classes that are incompatible with the superinterface to work together, allowing the adapted classes to use the methods of the target class!
There are class adapter patterns, object adapter patterns, and interface adapter patterns.
The adapter mode is used in many places in IO streams. A typical example is byte flow character stream. The target interface character stream Reader and the class InputStream that needs to be adapted are originally two unrelated classes. There is the adaptor InputStreamReader (delegated to an internal StreamDecoder). InputStreamReader converts a byte input stream into a character input stream that can read characters.
Similarly, OutputStreamWriter (delegated to an internal StreamEncoder) uses the adapter pattern, converting character streams into byte streams and writing characters into writing sections. All of these are object-based adapters that convert a stream of characters into a stream of bytes.
If you need to communicate, or the article is wrong, please leave a message directly. In addition, I hope to like, collect, pay attention to, I will continue to update a variety of Java learning blog!