I tried to figure out the relationship between classes such as java.io.InputStream

Introduction

Since Java SE 7, there are more useful classes, and less direct touching of java.io [] classes. However, when I used InputStream [] live for the first time in a while the other day, I googled because I didn't know how to use it, so I made a diagram.

There are many classes I haven't used, so if you have a story about how to use it like this, please comment.

The result of organizing

It became as follows. However, this is for clarity and is not accurate. The reason will be described later.

Read (InputStream, Reader)

読み込み.png

Write (OutputStream, Writer)

書き込み.png

Why I didn't write it correctly

For various reasons, I dare to make it inaccurate.

Class not entered

The following classes are excluded.

Deprecated class

These two have been deprecated due to a design mistake and have been excluded.

LineNumberInputStream

This class assumes a false perception that characters are properly represented by bytes.

StringBufferInputStream

Character to byte conversion is not done correctly in this class.

filter

These two are excluded because they are never used alone. I wondered why I didn't design it to be abstract, but it seems that there are other people who have the same question.

java - Why FilterInputStream is not Abstract Class - Stack Overflow

I don't think I need this FilterOutputStream [] in the first place.

I just didn't get into the figure

These are excluded because I couldn't find a suitable position. Perhaps the pipe should be a separate diagram.

I haven't used SequenceInputStream [], but it's a special class that concatenates InputStream [], so even if I put it in the figure, I wonder if it will be lonely in the corner.

Inheritance relationship is not described

For example, DataInputStream [], ObjectInputStream [] inherit from InputStream [], but omit it. DataOutputStream [], ObjectOutputStream [] inherits from OutputStream [], but omits it.

The reason is that these are used to read and write primitive types and objects, respectively, and there seems to be no other scene to pass them as InputStream.

If this is PushbackInputStream [], for example, it is supposed to be used as "read the first few bytes to automatically determine the file format, and then read the contents as InputStream []".

Also, ObjectInputStream [] directly inherits InputStream [], but I think the reason why DataInputStream [] is sandwiched between FilterInputStream [] is also difficult to understand. It seems that there are other people who have similar questions.

What I noticed after organizing

Naming convention is difficult to understand

All of these have a naming convention of XXXInputStream, XXXOutputStream, XXXReader, XXXWriter. Therefore, it is difficult to distinguish and it is a source of confusion.

The handling of encoding is different

Encoding is subtle for the three classes with colored backgrounds in the figure.

FileReader [], FileWriter [] do not have a constructor to specify the encoding. So I put it in the figure, but you should ignore it. Some say that FileReader [] is slow, and now there is [Files # newBufferedReader] [].

PrintStream [] has PrintWriter [], so I'm not sure how to handle it.

The PrintWriter class is used in situations where it is necessary to write characters instead of bytes.

It says, but I wonder if it's just left for System.out [] and System.err [].

in conclusion

Looking at the figure, it was surprisingly simple (´ω`)

Also, I thought it would take some time to put the link (´ ・ ω ・ `)

Recommended Posts

I tried to figure out the relationship between classes such as java.io.InputStream
[Metal] I tried to figure out the flow until rendering using Metal
I tried to explain the method
I tried to summarize the methods used
I tried to implement the Iterator pattern
I tried to summarize the Stream API
I tried to figure out the flow when performing image analysis with Vision Framework and Core ML
Ruby: I tried to find out where Nokogiri goes to see the encoding himself
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to set tomcat to run the Servlet.
I tried to organize the cases used in programming
I tried to summarize the state transition of docker
I tried to decorate the simple calendar a little
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
Java classes and instances to understand in the figure
I tried to implement the Euclidean algorithm in Java
I tried to find out what changed in Java 9
I had to figure out where the eclipse plugins folder was on my Mac. (Memo)
I tried to implement the like function by asynchronous communication
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
I tried using the GitHub repository as a library server
I tried to increase the processing speed with spiritual engineering
I tried to summarize the basics of kotlin and java
[Swift] I tried to implement the function of the vending machine
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment little by little using docker
I tried to build the environment of WSL2 + Docker + VSCode
I tried validation to unify the way hashtags are written
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried the Docker tutorial!
I tried the VueJS tutorial!
I tried the FizzBuzz problem
I tried to verify yum-cron
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to summarize the words that I often see in docker-compose.yml
Java14 came out, so I tried record for the time being
I tried to summarize what was asked at the site-java edition-
I tried to illuminate the Christmas tree in a life game
I tried to sort the data in descending order, ascending order / Rails
[Ruby] Tonight, I tried to summarize the loop processing [times, break ...]
I tried to implement the image preview function with Rails / jQuery
Special Lecture on Multi-Scale Simulation: I tried to summarize the 5th
I tried to understand how the rails method "redirect_to" is defined
Special Lecture on Multi-Scale Simulation: I tried to summarize the 8th
I tried to check the operation of gRPC server with grpcurl
I tried to understand how the rails method "link_to" is defined
[Java] I tried to make a maze by the digging method ♪
I tried to move the Java compatible FaaS form "Fn Project"
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I tried to display the calendar on the Eclipse console using Java.
Special Lecture on Multi-Scale Simulation: I tried to summarize the 7th
I tried to solve the problem of Google Tech Dev Guide
I don't really understand the difference between swift Error and NSError, so I tried to summarize it myself.