0

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 (e.g. 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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ruluk
  • 376
  • 3
  • 13

1 Answers1

1

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"));
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • Indeed many streams inherit from these abstract classes. However, without using any inherited class, both `inputstream` and `outputstream` work and do something. They can indeed send and receive bytes. I'm asking about that core phase of the process, without any extended classes. :) Any ideas? – Ruluk Apr 30 '13 at 20:46
  • @Ruluk if you mean to use plain `InputStream` and `OutputStream`, you simply can't since they are `abstract` classes. You **always** need to use an implementation of these classes. Note that the implementation class contains the real logic/work. – Luiggi Mendoza Apr 30 '13 at 21:26
  • Thanks for the info! I thought it did something because I used them directly in one tutorial, but now I analyzed further and they are just base streams that reference an offspring class. – Ruluk May 01 '13 at 12:39
  • @Ruluk if the answer solves your problem then click on the check below the arrows to mark this post as the answer – Luiggi Mendoza May 01 '13 at 12:41