I think any book about the mode can't help but mention its practical application when talking about the decorator mode-applications in the Java/IO library, <Java and mode> This book is no exception, but it is a bit different. This book has a special topic during introduction. It looks at the Java/IO library in two modes, after completing this topic, I personally feel that I have a new understanding of the Java/IO library, while also deepening the understanding of the decorator mode and adapter mode, I would like to share with you the great achievements I have made. At the same time, I would like to explain that most of the following texts and images come from <Java and mode>.
I. Introduction (General Introduction to Java I/O)
Regardless of the programming language, input and output are an important part. Java is no exception, and Java greatly expands the input/output functions and application scope. It uses the stream mechanism to implement input/output. The so-called stream is the orderly arrangement of data, and the stream can be from a source (called streams or source of stream, to a destination (called a streaming or sink of stream. A program reads data from the input stream and writes data to the output stream.
For example, a program can use the fileinputstream class to read data from a disk file, as shown in:
A stream processor like fileinputstream is called a stream processor. Like a Stream pipeline, it sucks some type of data from a queue and outputs some type of data. The above is called a Stream pipeline diagram.
Similarly, you can use the fileoutputstream class to write data to a disk file, as shown in:
In practice, this mechanism is not very useful, and the program needs to write very structured information. Therefore, the byte data is actually numerical values, text, source code, and so on. The Java I/O Library provides a chaining mechanism that connects a stream processor to the beginning and end of another stream processor and uses the output of one of them as the input, form a Stream pipeline link.
For example, the datainputstream stream processor can use the output of the fileinputstream Stream object as the input, and convert the byte type data to the original Java type and string type data. As shown in:
Similarly, writing data of the byte type to a file is not a simple process. The data that a program needs to write to a file is often structured, while the byte type is the original type. Therefore, it must be converted during writing. The dataoutputstream stream processor receives the raw data type and the string data type, while the output data of this stream processor is of the byte type. That is to say, dataoutputstream can convert the source data to byte data and then input it.
In this way, you can link dataoutputstream to fileoutputstream, so that the program can write source data of the original data type and string type into the dual pipeline of this link, to write structured data to a disk file, as shown in:
This is the major role of the link.
The stream processing process by the stream processor must all have streams. If the streams processed by the stream class are classified, they can be basically divided into two categories:
The first array, String, file, etc. This is called the original struct.
Second, a stream of the same type is used as a link stream class, called a link pipeline.
Design Principles of Java I/O Libraries
The Java I/O Library abstracts a variety of common tokens, streams, and processing processes. The client's Java program does not need to know the final scheme, whether the streaming is a file on the disk or an array, or whether the data is buffered, and whether the data can be read by row number or other processing details.
As mentioned in the book, people who first met the Java/IO library are all confused by the complexity of the library, and those who are familiar with the library, however, it is often difficult to argue whether the library is designed properly. The author of the book puts forward his own opinions. To understand the huge and complex library of Java I/O, the key is to master two symmetry and two design pattern modes.
Java I/O libraries have two symmetry:
1. Input-Output symmetry. For example, inputstream and outputstream occupy the root of two parallel hierarchical structures of the input and output of the byte stream. Reader and writer occupy the root of the two parallel hierarchical structures of the input and output of the char stream.
2 byte-Char symmetry. inputstream and reader sub-classes are responsible for the input of byte and char streams respectively. outputstream and writer sub-classes are responsible for the output of byte and char streams respectively, they form a parallel hierarchical structure.
Two design modes of the Java I/O Library:
The overall design of the Java I/O Library conforms to the decorator and adapter modes. As mentioned above, the class for processing a stream in this database is called a stream class. The fileinputstream, fileoutputstream, datainputstream, and dataoutputstream mentioned in the Introduction are examples of stream processors.
1. modifier mode: in the hierarchical structure represented by inputstream, outputstream, reader, and writer, some stream processors can play a decorative role on other stream processors to form a new one, stream processor with improved features. The modifier mode is the overall design mode of the Java I/O library. This principle complies with the modifier mode, as shown in:
2. Adapter mode: in the hierarchical structure represented by inputstream, outputstream, reader, and writer, some stream processors adapt to other types of processors. This is the application of the adapter mode, as shown in.
The adapter mode is applied to the original stream processor design and forms the starting point of all stream processors in the I/O library.
---------------------------------------
The middle part is missing!
---------------------------------------
Application in four-adapter Mode
The adapter mode is the second most important design mode in the Java I/O library.
Inputstream
Adapter mode in original stream processorThe inputstream Type Raw stream processor is an application in the adapter mode. Bytearrayinputstream is an adapter class bytearrayinputstream that inherits the inputstream interface and encapsulates a byte array. In other words, it adapts a byte array interface to an inputstream stream processor interface. We know that JAVA supports four types: Java interfaces, Java classes, Java arrays, and primitive types (INT, float, etc ). The first three types are reference types. The instances of classes and arrays are objects, and the values of the original type are not objects. That is, the Java array is the same as all other objects, regardless of the element type stored in the array. In this way, bytearrayinputstream conforms to the adapter mode description and is an object-form adapter class.
Fileinputstream
Is an adapter class
Fileinputstream inherits the inputstrem type and holds a reference to filediscriptor. This is an adapter mode that adapts a filediscriptor object to an inputstrem object, as shown in:
View the source code of jdk1.4. We can see that:
Public class{ FileInputStream extends InputStream
/* File Descriptor - handle to the open file */
private FileDescriptor fd;
public FileInputStream(FileDescriptor fdObj) {
SecurityManager security = System.getSecurityManager();
if (fdObj == null) {
throw new NullPointerException();
}
if (security != null) {
security.checkRead(fdObj);
}
fd = fdObj;
}
public FileInputStream(File file) throws FileNotFoundException {
String name = file.getPath();
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(name);
}
fd = new FileDescriptor();
open(name);
}
// Other code
}
Stringbufferinputstring inherits the inputstring type and holds a reference to the string object. This is an adapter mode that adapts the string object to the inputstring type, as shown in:
Outputstream
Adapter mode in original stream processor
Similarly, in the outputstream type, all the original stream processors are adapter classes. Bytearrayoutputstream inherits the outputstream type and holds a reference to the byte array. Its byte array interface is adapted to the outputstring type interface, so it is also an object-form adapter mode application.
Fileoutputstream
Is an adapter classFileoutputstream inherits the outputstream type and holds a reference to the filediscriptor object. This is an object-form adapter mode that adapts the filediscriptor interface to the outputstream interface form.
Reader
Adapter mode in original stream processor
Reader-Type Raw stream processors are used in adapter mode.
Stringreader
Is an adapter class
The stringreader class inherits the reader type and holds a reference to the string object. It matches the string interface with the reader interface, as shown in:
Slave
Byte
Flow
Char
Stream adaptationIn the Java I/O library, inputstreamreader and outputstreamwriter are frequently used. inputstreamreader is an adapter that flows from byte input to Char input streams. Shows the structure of inputstreamreader, reader, inputstream, and other classes. When inputstreamreader is linked to a specific subclass of any inputstream, data of the byte type can be read from the output of inputstream, convert data of the char type, as shown in: view the inputstreamreader source code of jdk1.4:
public class InputStreamReader extends Reader {
private final StreamDecoder sd;
/**
* Create an InputStreamReader that uses the default charset.
*
* @param in An InputStream
*/
public InputStreamReader(InputStream in) {
super(in);
try {
sd = StreamDecoder.forInputStreamReader(in, this, (String)null); // ## check lock object
} catch (UnsupportedEncodingException e) {
// The default encoding should always be available
throw new Error(e);
}
// Other code
}
Streamdecoder is a class in the sun. NiO. CS package.
Outputstreamwriter is an adapter class.
Similarly, we can conclude that outputstringwriter is an adapter class from outputstream to writer. That is to say, when linked to any specific outputstream subclass, outputstringwriter can adapt the byte stream of the outputstream type to a char stream.
The source code of jdk1.4 is similar to that of inputstreamreader. If you are interested, check the online source code of jdk1.4.
This book is followed by a small example with some explanations. I will not list it. You can check it if you have any books.
Summary
In these three articles, there are three knowledge points:
Knowledge Point 1: The four major hierarchical structures of the Java I/O Library the I/O library of the Java language provides four class levels: inputstream, outputstream, reader, and writer. Inputstream and outputstream PROCESS 8-bit stream data. Reader and writer process 16-bit bytes stream data. Inputstream and reader process input, and outputstream and writer process output. Knowledge Point 2: decorator mode in Java I/O Library application knowledge point 3: adapter mode in Java I/O Library application to: http://dev.csdn.net/author/lin_bei/8b332118ef1241d3a35ece6a2dcb05f6.html
Http://dev.csdn.net/author/lin_bei/3a479bcf2afd4576ac9fd6c65bc11522.html