Monday, July 24, 2017

Decorator Pattern for Java IO

InputStream is an abstract class. Most concrete implementations like BufferedInputStream, GzipInputStream, ObjectInputStream, etc. have a constructor that takes an instance of the same abstract class. That's the recognition key of the decorator pattern (this also applies to constructors taking an instance of the same interface).
When such a constructor is used, all methods will delegate to the wrapped instance, with changes in the way the methods behave. For example, buffering the stream in memory beforehand, decompressing the stream beforehand or interpreting the stream differently. Some even have additional methods that finally also delegate further to the wrapped instance. Those methods decorate the wrapped instance with extra behaviour.
Let's say that we have a bunch of serialized Java objects in a Gzipped file and that we want to read them quickly.
First open an inputstream of it:
FileInputStream fis = new FileInputStream("/objects.gz");
We want speed, so let's buffer it in memory:
BufferedInputStream bis = new BufferedInputStream(fis);
The file is gzipped, so we need to ungzip it:
GzipInputStream gis = new GzipInputStream(bis);
We need to unserialize those Java objects:
ObjectInputStream ois = new ObjectInputStream(gis);
Now we can finally use it:
SomeObject someObject = (SomeObject) ois.readObject();
// ...
The benefit is that you have a lot of freedom to decorate the stream using one or more various decorators to suit your needs. That's much better than having a single class for every possible combination like ObjectGzipBufferedFileInputStream, ObjectBufferedFileInputStream, GzipBufferedFileInputStream, ObjectGzipFileInputStream, ObjectFileInputStream, GzipFileInputStream, BufferedFileInputStream, etc.
Note that when you're about to close the stream, just closing the outermost decorator is sufficient. It will delegate the close call all the way to the bottom.
ois.close();
@reference_1_stackoverflow
Decorator Pattern for IO
@reference_2_stackoverflow
Examples of GoF Design Patterns in Java's core libraries


Does it make sense to always wrap an InputStream as BufferedInputStream, when I know whether the given InputStream is something other than buffered?
No.
It makes sense if you are likely to perform lots of small reads (one byte or a few bytes at a time), or if you want to use some of the higher level functionality offered by the buffered APIs; for example the BufferedReader.readLine() method.
However, if you are only going to perform large block reads using the read(byte[]) and / or read(byte[], int, int) methods, wrapping the InputStream in a BufferedInputStream does not help.

@reference_3_stackoverflow
Should I always wrap an InputStream as BufferedInputStream?





No comments:

Post a Comment