IO Family class hierarchy Architecture
Horizontal Matching
The previous article mainly introduced the Javaio flow family's overall design idea, the simple review basic logic involves the data origin direction, as well as the stream data form these three parts combination according to the flow data form and the flow direction, the combination comes four families, respectively is: inputstream/ The combination of OutputStream reader/writer data source and four families forms the basic function of Io stream.
data source form |
|
outputstream |
reader |
writer |
ByteArray (byte array) |
Bytearrayinputstream |
Bytearrayoutputstream |
No |
No |
File (Files) |
FileInputStream |
FileOutputStream |
FileReader |
FileWriter |
Piped (pipe) |
PipedInputStream |
PipedOutputStream |
Pipedreader |
PipedWriter |
Object (Objects) |
ObjectInputStream |
ObjectOutputStream |
No |
No |
String |
StringBufferInputStream |
No |
StringReader |
StringWriter |
Chararray (character array) |
No |
No |
CharArrayReader |
Chararraywriter |
The expansion function is basically implemented by the adorner mode.
extended function point |
|
outputstream |
reader |
writer |
Data (Basic type) |
DataInputStream |
DataOutputStream |
No |
No |
Buffered (buffered) |
Bufferedinputstream |
Bufferedoutputstream |
BufferedReader |
BufferedWriter |
LineNumber (line number) |
Linenumberinputstream |
No |
LineNumberReader |
No |
Pushback (fallback) |
Pushbackinputstream |
No |
Pushbackreader |
No |
Print (printing) |
No |
PrintStream |
No |
PrintWriter |
From the list above it should be possible to see that for the main classes and interfaces in the IO system we only need to be clear two points, we can get a deeper understanding of their
1. For various data sources, four family of processing logic |
2. Meaning of the extended function point |
Note: Many IO members do not manipulate files on disk such as Bytearrayinputstream and Bytearrayoutputstream next we will describe in detail that they do not directly manipulate persisted data (stored on disk), There's a lot of other stuff. What do they have to do with IO? Why do they want to implement the interface of the stream? Here I would like to remind that, for our programming language, IO represents the manipulation of data, the data read and write IO represents a kind of readable and writable behavior similar to things, Instead of reading a file from disk why not have a simple class to do with an array of bytes, why should it be related to an IO hook? First, this is not a no, it is possible to construct a byte array that does not matter to the IO architecture to manipulate classes to read and write byte arrays
However, his behavior is obviously similar to Io, in defining a different set of interfaces obviously increases the cost of developer use Moreover, regardless of where to read, itself is still the input and output problems and, for different data sources to provide a consistent interface, which is also very consistent with the interface-oriented programming specifications so, in a word, Do not narrow the IO to the file on the operating disk. Data. IO is the input and output, is the pronoun of reading and writing
IO data Source application
ByteArray (byte array)
Byte array, no doubt, is not applied within the character family he applies to Bytearrayinputstream and bytearrayoutputstream his interior contains a byte array of byte Buf[]bytearrayinputstream and Bytearrayoutputstream internally maintains a byte buf[] that reads data into this byte array (buffer) or writes data to this byte array (buffer) that they maintain is the internal byte array itself and does not write to the file |
These two classes are essentially manipulating byte arrays, providing read and write to byte arrays. It's essentially like a file, it's used to store data. It's just that the data exists in memory. The interface for IO consistency can be provided by encapsulating the data into an internal character array |
ByteArray only Application and byte stream |
file (Files)
As I said earlier, File is the most common form of data Therefore, it is very reasonable to provide file-based operation to IO. We know that all data storage is ultimately byte form But the operation of the file is so frequent and important Therefore, the input and output for the character also provides the corresponding processing Still, the final file is stored in bytes, so for character files, it's natural to encode and decode FilterReader Every read means a single decoding. Filterwriter Each write means that the encoding
|
Since it is a file, we have described the files class earlier The construction of the file class is mainly by the path name or the file descriptor Therefore, for the input and output of the file-related IO operations, the path name can naturally be the file descriptor or the files themselves as the target object Which means that the parameters of the constructor are generally one of these three.
|
The operation of the file is the actual operation of the file itself File Four families have been applied |
Piped (pipe)
The concept of piping, not from Java IO, had this concept earlier The meaning is very clear, just like his name, the pipe, as if two pipes were connected, forming a passage This channel is directly connected, and will not run anywhere else to bend around The main function of pipeline flow is that it can communicate between two threads. Since the main role of the communication between the threads, he is the transmission of data using In byte array cache data, out using in object
|
Pipes have been used in four families. |
Object
The role of ObjectInputStream and ObjectOutputStream is to serialize operations on basic data and objects to support ObjectOutputStream objects that provide access to "basic data or objects" Persistent storage ObjectInputStream, read out these "basic data or objects" only objects that support java.io.Serializable or java.io.Externalizable interfaces can be objectinputstream/ Operated by ObjectOutputStream |
It is not possible to serialize only characters, so object is only for the byte family |
String
Provides support for string types Reader reads to string Writer writes to StringBuffer |
|
StringBufferInputStream has not been recommended for use So, later, you can think of string only supporting the character family |
Chararray
Similar to ByteArray, also provides support for character arrays Manipulating Memory data |
|
Character arrays support only character families |
In fact, it can be seen that only file is a real disk file related to the other data source form is the operation of memory data
IO extension Feature application
Data (basic type)
Data is support for basic data types Data files written for DataOutputStream Can be read using DataInputStream Which means it's a special form of file. |
They rely on the bottom of the byte stream by inheriting FilterInputStream and Filteroutputstream Using InputStream in and outputstream out These two objects are passed through the construction method. |
Buffered (buffered)
Buffering is also to reduce the frequency of reading, set a buffer The concept of buffering is everywhere, so buffers are used in four families |
linenumber (line number)
The linenumber is for the input So it exists in Linenumberinputstream and LineNumberReader. But the linenumberinputstream for the byte stream has been deprecated.
|
LineNumberReader is a buffered character input stream that tracks line numbers It is also clear that streams are sequential reads that cannot be rolled back, so it is natural to use the cache to read line numbers His realization of inheritance BufferedReader also very well understood |
Pushback
Pushback fallback, which is to read a character and then put it back in the stream again So it's for the input. Pushbackinputstream Pushbackreader Also with the help of the internal cache
|
print (printing)
Mainly to provide the convenience of data printing |
Printing is naturally directed to the output PrintStream PrintWriter |
This paper analyzes the overall design of IO Class library from the point of view of data source and extension function, although the previous article has made a brief introduction to all the basic function points and the extension function points in this paper. The importance of data sources and extension function points in the class hierarchy only a thorough understanding of the data source and the logic of the extended function point can fully understand the entire IO Class library architecture design
[Iv.] Horizontal alignment of hierarchical architectures such as Javaio