I know that java I/O uses decorator pattern. But I feel that I understand its wrong.
Please clarify difference between two code snippets:
snippet 1:
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
objectOutputStream.writeObject("this is my string");
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);
System.out.println(objectInputStream.readObject());
This application works according my expectations and I see result in console.
snippet 2:
I try to wrap ObjectInputStream and ObjectOutputStream twice:
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
ObjectOutputStream objectOutputStreamWrapper = new ObjectOutputStream(objectOutputStream); //double wrapping
objectOutputStreamWrapper.writeObject("this is my string");
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);
ObjectInputStream objectInputStreamWrapper = new ObjectInputStream(objectInputStream);
System.out.println(objectInputStreamWrapper.readObject());
This code just hangs up. I don't understand why. please clarify.
P.S.
It is theoretical question only.
UPDATE
Really hangs up behaviour happens because of I use pipes (According EJP answer).
for example this code works according expectations.
OutputStream outputStream = new FileOutputStream("2.txt");
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
ObjectOutputStream objectOutputStreamWrapper = new ObjectOutputStream(objectOutputStream); //double wrapping
objectOutputStreamWrapper.writeObject("this is my string");
objectOutputStream.close();
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("2.txt"));
ObjectInputStream objectInputStreamWrapper = new ObjectInputStream(objectInputStream);
System.out.println(objectInputStreamWrapper.readObject());
objectInputStream.close();
looks like if I wrap input by 10 decorators I should wrap output by 10 decorators at the same order. Is it true ?
UPDATE + 1
I noticed that problem with flushing only:
PipedInputStream pipedInputStream = new PipedInputStream();
PipedOutputStream pipedOutputStream = new PipedOutputStream();
pipedOutputStream.connect(pipedInputStream);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(pipedOutputStream);
ObjectOutputStream objectOutputStreamWrapper = new ObjectOutputStream(objectOutputStream); //double wrapping
objectOutputStreamWrapper.writeObject("this is my string");
objectOutputStreamWrapper.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(pipedInputStream);
ObjectInputStream objectInputStreamWrapper = new ObjectInputStream(objectInputStream);
System.out.println(objectInputStreamWrapper.readObject());