简体   繁体   中英

InputStream and OutputStream abstract methods

I've been exploring the java.io package a lot lately, and I've been wondering where can I find the exact processes that the abstract classes inside InputStream and OutputStream do.

I'm dazzled because, at least according to the source code, the core methods are all abstract (eg InputStream.read() and OutputStream.write(int b) ). I'm especially concerned by the close methods, whose javadocs explicitly say they do nothing:

Closes this input stream and releases any system resources associated with the stream.

The close method of InputStream does nothing.

Well, input streams obviously write while output streams read, and definitely close() has to do the flushing and resource releasing.

Somebody able to give me an explanation?

These classes are abstract since in that way they can be easily extended and the implementor classes could use the Decorator Pattern (as shown here ). With the decorator pattern, the implementor class can add dynamic functionality at runtime. For example: have an InputStream that can read a File using FileInputStream that can read serialized objects using ObjectInputStream . How to accomplish this?

ObjectInputStream ois = new ObjectInputStream(
    new FileInputStream(new File("/path/to/file.dat"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM